Use Data by Downloading it into the R Shinylive App
This example demonstrates how we can retrieve and load data from a URL into the R Shinylive app. Interested in knowing more? Read on!
Example App
We’ll be exploring the following application:
#| standalone: true
#| viewerHeight: 600
## file: app.R
library(shiny)
ui <- fluidPage(
titlePanel("Hello Shiny!"),
h3("Data URL"),
verbatimTextOutput("urlText"),
h3("Working Directory"),
verbatimTextOutput("workingDirectory"),
h3("File System Information"),
verbatimTextOutput("fileSystem"),
h3("Downloaded Data by Relative URL"),
verbatimTextOutput("retrievedData")
)
server <- function(input, output, session) {
data_url <- "https://tutorials.thecoatlessprofessor.com/r-shinylive-data-include/fruit-data.csv"
output$urlText <- renderText({
data_url
})
output$retrievedData <- renderPrint({
download.file(data_url, "fruit-data.csv")
read.csv("fruit-data.csv")
})
output$fileSystem <- renderPrint({
list.files()
})
output$workingDirectory <- renderPrint({
getwd()
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)
Data Location
Ensure where the data is found can be accessed using the HTTPS
protocol. For example, we’ll use the data at:
https://tutorials.thecoatlessprofessor.com/r-shinylive-data-include/fruit-data.csv
This website is being hosted from GitHub Pages with Enforce HTTPS enabled.
Including the URL in the Application
We recommend setting the URL in the server
portion of the Shiny app. For instance, we have:
<- function(input, output, session) {
server
<- "https://tutorials.thecoatlessprofessor.com/r-shinylive-data-include/fruit-data.csv"
data_url
# Rest of application
}
Specifying Data to Publish
When publishing an R Shinylive application through Quarto, include the resources
key in the document header with shinylive-sw.js
and the data file name (e.g., fruit-data.csv
).
---
title: "Your Quarto Page Title"
format:
html:
resources:
- shinylive-sw.js # Required to publish the shinylive service worker
- fruit-data.csv # Required to make sure the data is uploaded alongside the application
filters:
- shinylive
---
Embedding the R Shinylive in Quarto
Embed the complete R Shinylive app into a {shinylive-r}
code cell. Ensure to set standalone: true
and consider adjusting the app height with viewerHeight: 600
. For example, the standard cell would be:
```{shinylive-r}
#| standalone: true
#| viewerHeight: 600
## file: app.R
library(shiny)
# Rest of the app code
```
The full code for the example shown at the top of the document is:
```{shinylive-r}
#| standalone: true
#| viewerHeight: 600
## file: app.R
library(shiny)
ui <- fluidPage(
titlePanel("Hello Shiny!"),
h3("Data URL"),
verbatimTextOutput("urlText"),
h3("Working Directory"),
verbatimTextOutput("workingDirectory"),
h3("File System Information"),
verbatimTextOutput("fileSystem"),
h3("Downloaded Data by Relative URL"),
verbatimTextOutput("retrievedData")
)
server <- function(input, output, session) {
data_url <- "https://tutorials.thecoatlessprofessor.com/r-shinylive-data-include/fruit-data.csv"
output$urlText <- renderText({
data_url
})
output$fileSystem <- renderPrint({
list.files()
})
output$workingDirectory <- renderPrint({
getwd()
})
output$retrievedData <- renderPrint({
download.file(data_url, "fruit-data.csv")
read.csv("fruit-data.csv")
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)
```