This vignette demonstrates how you might use pins and Posit 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.
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 <- board_connect()
board %>% pin_write(bbc_news)
```
```{r}
bbc_news
```
Then publish it to Posit Connect, and schedule it to run as often as
you like. Assuming that you have Posit Connect 1.9.0 or later you don’t
need to provide any arguments to board_connect()
; 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 <- board_connect()
ui <- fluidPage(
titlePanel("News from the BBC"),
htmlOutput("news")
)
server <- function(input, output, session) {
news <- board %>% pin_reactive_read("hadley/bbc_news")
output$news <- renderUI({
title <- htmltools::htmlEscape(news()$title)
links <- paste0("<a href='", news()$url, "'>", title, "</a>")
bullets <- paste0(" <li>", links, "</li>", collapse = "\n")
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.