This vignette demonstrates how you might use pins and RStudio Connect together to automate ETL for a Shiny app. Pulling expensive data manipulation out of your Shiny app is a great way to improve performance, and pins makes it easy to schedule data updates in a way that automatically flows into your app.
library(pins)
Here the underlying data will be changing regularly, but the pinned
data will only change when you run pin_write()
. Wouldn’t it
be great if we could automate that process? One way to do so is with
RSC’s scheduled
reports.
First, take your code and put it in an Rmd:
---
title: BBC news
---
```{r}
library(pins)
library(xml2)
xml <- read_xml("http://feeds.bbci.co.uk/news/rss.xml")
items <- xml %>% xml_find_all("//item")
bbc_news <- tibble::tibble(
title = items %>% xml_find_first("./title") %>% xml_text(),
date = items %>% xml_find_first("./pubDate") %>% xml_text(),
url = items %>% xml_find_first("./guid") %>% xml_text()
)
board_rsc <- board_rsconnect()
board_rsc %>% pin_write(bbc_news)
```
```{r}
bbc_news
```
Then publish it to RSC, and schedule it to run as often as you like.
Assuming that you have RSC 1.9.0 or later you don’t need to provide any
arguments to board_rsconnect()
; pins will automatically
publish to the same Connect instance that’s running the report.
If you’re using automatically updated data in a Shiny app, you can use
pin_reactive_read()
to create a reactive dependency so that
your app will automatically update shortly after the data changes:
library(shiny)
library(pins)
<- board_rsconnect()
board
<- fluidPage(
ui titlePanel("News from the BBC"),
htmlOutput("news")
)
<- function(input, output, session) {
server <- board %>% pin_reactive_read("hadley/bbc_news")
news
$news <- renderUI({
output<- htmltools::htmlEscape(news()$title)
title <- paste0("<a href='", news()$url, "'>", title, "</a>")
links <- paste0(" <li>", links, "</li>", collapse = "\n")
bullets HTML(paste0("<ul>", bullets, "</ul>"))
})
}
shinyApp(ui, server)
If you deploy this app and watch it while your scheduled reported runs, you’ll see the data update automatically.