Skip to content

Commit

Permalink
Switch over to a site to show approaches.
Browse files Browse the repository at this point in the history
  • Loading branch information
coatless committed Feb 19, 2024
1 parent 181432f commit 49f63f3
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 30 deletions.
30 changes: 29 additions & 1 deletion _quarto.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
project:
title: "R-shinylive-embed-data-with-file"
type: website
output-dir: _site

website:
title: "Data inclusion in a shinylive app"
search: true
reader-mode: true
sidebar:
style: "floating"
contents:
- href: index.qmd
text: Home
- href: include-file.qmd
text: File Embed in App
- href: include-download-from-url.qmd
text: Download file into App

page-footer:
left: "Data in shinylive apps"
right:
- icon: github
href: https://github.com/coatless-tutorials/r-shinylive-data-include

# Set the language that should be used for Quarto websites
# https://github.com/quarto-dev/quarto-cli/tree/main/src/resources/language
# lang: en

# Attach shinylive to every page
filters:
- shinylive
46 changes: 46 additions & 0 deletions include-download-from-url.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
title: "Use Data by Downloading it into the R Shinylive App"
format:
html:
resources:
- shinylive-sw.js
- fruit-data.csv
filters:
- shinylive
---

```{shinylive-r}
#| standalone: true
#| components: [editor, viewer]
#| layout: vertical
#| viewerHeight: 400
## file: app.R
library(shiny)
ui <- fluidPage(
titlePanel("Hello Shiny!"),
h3("Relative URL"),
verbatimTextOutput("urlText"),
h3("Downloaded Data by Relative URL"),
verbatimTextOutput("retrievedData")
)
server <- function(input, output, session) {
data_url <- "https://https://tutorials.thecoatlessprofessor.com/r-shinylive-data-include/fruit-data.csv"
output$urlText <- renderText({
data_url
})
output$retrievedData <- renderText({
download.file(data_url, "fruit-data.csv")
read.csv("fruit-data.csv")
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)
```

42 changes: 42 additions & 0 deletions include-file.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: "Include Data By Embedding File Contents Inside Code Cell"
format:
html:
resources:
- shinylive-sw.js
filters:
- shinylive
---

```{shinylive-r}
#| standalone: true
#| components: [editor, viewer]
#| layout: vertical
#| viewerHeight: 400
## file: app.R
library(shiny)
ui <- fluidPage(
titlePanel("Hello Shiny!"),
verbatimTextOutput(outputId = "dataVariables")
)
server <- function(input, output) {
output$dataVariables <- renderPrint({
ds <- read.csv("fruit.csv")
ds
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)
## file: fruit.csv
id,name,count
1,"apple",20
2,"orange",12
3,"grape",100
```

4 changes: 4 additions & 0 deletions include-url.qmd → include-relative-url.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ filters:
- shinylive
---

:::callout-important
This approach does **not** work as the `shinylive` app does not get the local file data attached.
:::

```{shinylive-r}
#| standalone: true
#| components: [editor, viewer]
Expand Down
35 changes: 6 additions & 29 deletions index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,12 @@ filters:
- shinylive
---

```{shinylive-r}
#| standalone: true
#| components: [editor, viewer]
#| layout: vertical
#| viewerHeight: 400
## file: app.R
library(shiny)
How can I include data in an `{r-shinylive}` app? There are two approaches:

ui <- fluidPage(
titlePanel("Hello Shiny!"),
verbatimTextOutput(outputId = "dataVariables")
)
1. Embed the text file contents inside the editor
2. Download the data from an HTTPS

server <- function(input, output) {
output$dataVariables <- renderPrint({
ds <- read.csv("fruit.csv")
ds
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)
## file: fruit.csv
id,name,count
1,"apple",20
2,"orange",12
3,"grape",100
```
::: callout-important
There is no way to use a relative URL or a non-HTTPS URL.
:::

0 comments on commit 49f63f3

Please sign in to comment.