Trasforma un documento R Markdown in un'esperienza interattiva

R Markdown è una delle cose che preferisco del moderno R. Offre un modo semplice per combinare testo, codice R e risultati del codice R in un unico documento. E quando il documento viene visualizzato come HTML, puoi aggiungere alcune interazioni dell'utente con widget HTML come DT per le tabelle o volantino per le mappe. (Se non hai familiarità con R Markdown, puoi prima controllare il mio tutorial video su R Markdown e poi tornare qui.) 

Ma potresti non sapere che c'è un modo per aumentare ulteriormente l'interattività di R Markdown: aggiungendo runtime: shinyall'intestazione del documento.

Shiny è un framework per applicazioni Web per R. Come framework, ha una struttura abbastanza specifica. Tuttavia, puoi convertire un documento R Markdown in un'app Shiny senza dover seguire gran parte di quella rigida struttura . Invece, puoi saltare subito e iniziare a scrivere codice, senza preoccuparti di alcune tipiche attività di Shiny come assicurarsi che tutte le parentesi e le virgole siano corrette all'interno di funzioni di layout profondamente nidificate.

In effetti, anche se sei uno sviluppatore brillante esperto, un documento R Markdown può comunque essere utile per attività brillanti in cui non hai bisogno di un'applicazione completa o per provare rapidamente il codice. Avrà ancora bisogno di un server Shiny, ma se hai installato RStudio e il pacchetto shiny, ne hai già uno in locale.

Diamo un'occhiata a come funziona il runtime shiny in R Markdown.

1. Markdown R di base

Inizierò con un documento Markdown R convenzionale e non Shiny che presenta una tabella di dati ricercabile in base al codice postale del Massachusetts. Gli utenti possono eseguire ricerche o ordinare in base a qualsiasi colonna della tabella, rispondendo a domande quali "Quali codici postali hanno il reddito familiare medio più alto della contea di Middlesex?" o "Quali codici postali hanno gli alloggi mensili più costosi?"

Sharon Machlis /

Questo documento ha anche un istogramma che mostra la distribuzione dei redditi familiari medi e un testo che indica quali codici postali hanno il reddito più alto e quello più basso. La tabella è interattiva, ma il resto del documento non lo è. Puoi vedere la versione HTML renderizzata su RPubs di RStudio.

Se desideri seguire, puoi vedere il codice per una versione autonoma di questo documento R Markdown, inclusi i dati, su GitHub. Oppure, se desideri vedere come ho ottenuto questi dati demografici in R, c'è il codice R in questo articolo per creare il tuo set di dati (e puoi modificare il codice per scegliere un altro stato). Se crei la tua versione dei dati, anche il codice per un documento R Markdown di base utilizzando un file di dati separato si trova su GitHub.

Qualunque sia il documento R Markdown scelto, vedrai che è un documento per lo più statico con una certa interattività. Ma cosa succede se volessi che l' intero documento fosse interattivo, in questo caso, vedere l'istogramma e il testo cambiare così come la tabella? Come può l'utente essere in grado di selezionare singole città e vedere tutte le informazioni filtrate per essere visualizzate solo per quei luoghi?

Una soluzione è generare una pagina per ogni città, possibile con uno script R se si utilizzano i cosiddetti report parametrizzati. Tuttavia, puoi anche creare un singolo documento R Markdown che funzioni come un'app interattiva.

Aggiungi interattività brillante

Per aggiungere l'interattività Shiny a un documento R Markdown convenzionale, inizia aggiungendo runtime: shinyall'intestazione YAML del documento, ad esempio:

---

titolo: "Reddito familiare medio per CAP"

output: html_document

durata: brillante

---

Una volta che lo fai e premi Salva, l'icona di maglia in RStudio si trasforma in "Esegui documento". Anche se l'output dice ancora "html_document", non sarà più semplice HTML. Ora è una mini applicazione Shiny. 

Sharon Machlis / Sharon Machlis,

Consenti agli utenti di scegliere i dati

Ora ho bisogno di un modo per consentire agli utenti di fare le loro scelte sui dati. Shiny ha una serie di "widget di input" per questo. Userò selectInput(), che crea un elenco a discesa e consente agli utenti di scegliere più di un elemento. Shiny ha altri widget per pulsanti di opzione, input di testo, selettori di data e altro. Puoi vederne una raccolta nella Shiny Widgets Gallery di RStudio. 

Il codice per l' selectInput()elenco a discesa della mia mini-app ha cinque argomenti e ha questo aspetto:

selectInput ("mycities", "Scegli 1 o più città:",

scelte = ordinamento (unico (markdowndata $ City)),

selezionato = "Boston", multiplo = TRUE)

Il primo argomento di  selectInput(), mycitiesè il nome della variabile che ho scelto per memorizzare i valori scelti dall'utente. Il secondo argomento è il testo dell'intestazione che apparirà con l'elenco a discesa. Il terzo argomento, choicesè un vettore di tutti i possibili valori nell'elenco a discesa, in questo caso, valori univoci dei nomi di città nei miei dati, ordinati alfabeticamente. selected = Bostonsignifica che il menu a discesa sarà Boston come città selezionata (la selezione di una selezione predefinita è facoltativa). E, infine, multiple = TRUEconsente agli utenti di scegliere più di una città alla volta.

This code creates the HTML dropdown list. If you run that selectInput() code in your R console, it will generate HTML for the dropdown (assuming you have Shiny loaded and a data frame called markdowndata with a City column). 

Next, I need to write some R so that this dropdown actually does something.

Create dynamic variables

I’ll code this interactivity logic in two parts:

  1. Create a data frame—I’ll call it mydata—that is filtered each time the user chooses a city.
  2. Write code for text, histogram, and data table that will all change based on my dynamic data frame.

The most important thing to keep in mind at this point is that these objects aren't “regular” R variables anymore. They’re dynamic. They change based on user actions. And that means they work slightly differently than variables you’re probably used to.

What’s special about them? Here are the three things you need to know:

  1. To access the value of an input variable that store information from your user, you need the syntax input$myvarname, not simply myvarname. So, for values stored in the mycities dropdown list, use input$mycities
  2. Objects like graphs and tables that depend on values from your user are also dynamic and need to be reactive. That’s as easy as wrapping them in a special function, but you need to remember to do it. They also can't be accessed by just their names, but require parentheses as well: a syntax like myvar() and not myvar.
  3. When you display dynamic content—once again, things like a table, a map, a histogram, or even text—it needs to be rendered in a special way, usually using one of Shiny’s special render functions. The good news is that Shiny takes care of most of the functionality of monitoring for changes and calculating results. You just need to know which function to use, and then include it in your code.

This is all often easier than that may sound. Here’s how I’d create a data frame called mydata that changes each time the user selects a city with the mycities selectInput() dropdown :

mydata <- reactive({

filter(markdowndata, City %in% input$mycities)

})

The mydata object now holds a reactive expression and will change value each time the user makes a change in the dropdown list controlling mycities.

Display dynamic variables

Now I'd like to code a table using that filtered mydata data.

As you might have guessed by now, DT::datatable(mydata) won’t work. And there are two reasons why.

First, because mydata is a reactive expression, you can’t refer to it by name alone. It needs parentheses after it, such as  mydata().

But, second, DT::datatable(mydata()) won’t work as standalone code, either. You’ll get an error message something like this: 

 Operation not allowed without an active reactive context.

(You tried to do something that can only be done from inside

a reactive expression or observer.)

You may see versions of this error message quite often when you’re first starting out. It means that you’re trying to display something dynamic using conventional R syntax.

To fix this, I need a Shiny render function. Several visualization packages have their own special Shiny render functions, including DT. Its render function is renderDT(). If I add renderDT ({  }) around the DT code and run the document again, that should work.

This is my table code:

renderDT({

DT::datatable(mydata(), filter = 'top') %>%

formatCurrency(4:5, digits = 0) %>%

formatCurrency(6, currency = "", digits = 0)

})

Note: In addition to creating and displaying the table, this code also adds some formatting. Columns 4 and 5 show as currency, with a dollar sign and commas. The second formatCurrency() line for column 6 adds the commas to the rounded numbers without a dollar sign, since I specified "" as the currency symbol.

I can use the same mydata() reactive data frame to create a histogram, using another Shiny render function: renderPlot().

renderPlot({

ggplot2::ggplot(mydata(), aes(x = MedianHouseholdIncome)) +

geom_histogram(binwidth = 20000, color = "black", fill = "darkgreen") +

theme_classic() +

xlab("") +

ylab("") +

scale_x_continuous(labels = dollar)

})

That code also includes a little ggplot styling, such as choosing colors for the bar outline and fill and changing the graph's theme. The last line formats the x axis to add dollar signs and commas, and it requires the scales package.

Each one of these blocks of R code needs to be within an R Markdown R code chunk, just like any other R code chunks in a conventional Markdown document. That could look something like the code below, which also names the chunk “histo” (names are optional) and sets the width and height of my plot in inches.

```{r histo, fig.width = 3, fig.height = 2}

renderPlot({

ggplot2::ggplot(mydata(), aes(x = MedianHouseholdIncome)) +

geom_histogram(binwidth = 20000, color = "black", fill = "darkgreen") +

theme_classic() +

xlab("") +

ylab("") +

scale_x_continuous(labels = dollar)

})

```

If I'd like to display interactive text that changes with the user's selection, I need a Shiny render function that's named—surprise!—renderText(). You can put that inside a code chunk, or use alternative R Markdown syntax format outside of code chunks like this:

I have some plain text and then add  `r R CODE WILL BE EVALUATED HERE`

The syntax for this is one backtick followed immediately by a lower-case r, a space, the R code you want evaluated, and ending with another single backtick. So, to add a dynamic headline for the histogram, you could use code like this:

Histogram for `r renderText({input$mycities})`

This works fine for a single city. However, if there's more than one city, that code will just display the names without commas between them, such as Boston Cambridge Amherst. For public-facing code,  you'd want to pretty that up a bit, perhaps using base R's paste() function:

Histogram for `r renderText({paste(input$mycities,

sep = " ", collapse = ", ")})`

You can use similar code to generate text that tells users the ZIP codes with highest and lowest median incomes. For those calculations, I created one reactive data frame containing the row with the highest household income and another with the lowest.

I also discovered that the lowest median income was being suspiciously low—$2,500 in the college-town community of Amherst, Mass.—where the median monthly housing cost is $1,215. My guess is that's a concentration of student housing, so I excluded any ZIP code with median household income of less than $5,000.

Here is code to create those two data frames:

zip_highest_income_row <- reactive({

filter(mydata(), MedianHouseholdIncome == max(MedianHouseholdIncome, na.rm = TRUE))

})

zip_lowest_income_row <- reactive({

filter(mydata(), MedianHouseholdIncome >= 5000) %>%

filter(MedianHouseholdIncome == min(MedianHouseholdIncome, na.rm = TRUE))

})

This should look like typical dplyr filter() code, except that 1) each is wrapped in a reactive({ }) function, and 2) the mydata dynamic data frame which changes based on user input is referred to as mydata() and not simply mydata

To show the value of the first item in the zip_highest_income_row data frame's ZIP column, I can't use usual R code like zip_highest_income_row$Zip[1]. Instead,  I need to refer to the dynamic data frame with parentheses: zip_highest_income_row()$Zip[1] . And then wrap that in a Shiny render() function—in this case renderText():

ZIP code `r renderText(zip_highest_income_row()$ZipCode[1])` in

`r renderText(zip_highest_income_row()$City[1])`

has the highest median income in the place(s) you selected,

`r renderText(scales::dollar(zip_highest_income_row()$MedianHouseholdIncome[1]))`.

ZIP code `r renderText(zip_lowest_income_row()$ZipCode[1])` in

`r renderText(zip_lowest_income_row()$City[1])` has the lowest

median income in the place(s) you selected,

`r renderText(scales::dollar(zip_lowest_income_row()$MedianHouseholdIncome[1]))`.

Run and share your Shiny app

Once you add runtime: shiny to an R Markdown, it’s not an HTML file anymore—it’s a mini Shiny application. And that means it needs a Shiny server to run.

As I mentioned earlier, anyone with R, RStudio, and the shiny package has a Shiny server on their local system. That makes it easy to share any Shiny app with fellow R users. You can send them a document by email or, more elegantly, post it online as a zipped file and use the shiny::runUrl() command. There are special runGitHub() and runGist() functions for apps on GitHub that are convenient if you use GitHub for projects, which will automatically zip additional files in your project, such as data files.

But chances are, at some point you’ll want to show your work to non-R users, and that requires a publicly accessibly Shiny server. Probably the easiest option is RStudio’s shinyapps.io service. It’s free for a few limited public apps with very light usage. Paid accounts are priced based on the number of active hours they offer for your apps. Active hours measure time the application is actively being used—one person on for an hour is the same hour as 100 people in that hour. To ensure 24x7 uptime for a couple of apps, you’d need the $1,100/year standard account with 2,000 hours.

You can also build your own Shiny server on a cloud service like AWS and installations for R and the free version of RStudio’s Shiny server software. There’s a great step-by-step tutorial by Dean Attali showing how to do so at Digital Ocean, where you can build and run a small Shiny server for just $5 per month of hosting costs without worrying about active hours. The trade-off is doing your own patching and R/library updates—and you may need a heftier virtual server than the cheapest 1G droplet for robust applications.

Add an interactive map

Finally, I'll walk you through how I added an interactive map to this document using the leaflet package.

First, you need a file with geospatial data as well as numerical data, so your app knows the shape of each ZIP code. The code below explains how create a spatial data frame using the tidycensus and sf packages.

For interactivity, I'll create a dynamic version of that spatial data, so only the selected cities show up on the map. Below is my code for doing that. It may look a little repetitive, but I'm going for readability instead of brevity. Feel free to tighten your own version.

mapdata <- reactive({

if("All Mass" %in% input$mycities){

ma_appdata_for_map %>%

dplyr::select(ZipCode = GEOID, MedianHouseholdIncome = medincome, MedianMonthlyHousingCost = medmonthlyhousingcost, Population = pop, City, County = county.name, State, Lat, Long, income, housing, Pop, geometry) %>%

mutate(

Highlighted = "Yes"

) %>%

sf::st_as_sf()

} else {

dplyr::filter(ma_appdata_for_map, City %in% input$mycities) %>%

dplyr::select(ZipCode = GEOID, MedianHouseholdIncome = medincome, MedianMonthlyHousingCost = medmonthlyhousingcost, Population = pop, City, County = county.name, State, Lat, Long, income, housing, Pop, geometry) %>%

dplyr::mutate(

Highlighted = ifelse(City %in% input$mycities, "Yes", "No")

) %>%

sf::st_as_sf()

}

})

The reactive function should be familiar by now. My if and else statements take into account whether the user has chosen All Mass or just individual cities. For any choice but All Mass, I filter for just the selected cities. In both cases I'm using a conventional dplyr select() function to choose which columns I want in the map, making sure to include Lat for latitude, Long for longitude, and geometry that holds the ZIP code polygon shape files. The last line in each if() code section makes sure the results are an sf (simple features) geospatial object. While I didn't need that code on my local Mac, the app worked better on shinyapps.io when I included it.

Now it’s time to work on map colors. I'll set up two reactive color palettes for my leaflet map, one for income and the other for housing costs. In both cases I use greens, but you could choose any you'd like. 

incomepal <- reactive({

leaflet::colorNumeric(palette = "Greens",

domain = mapdata()$MedianHouseholdIncome)

})

housingpal <- reactive({

leaflet::colorNumeric(palette = "Greens",

domain = mapdata()$MedianMonthlyHousingCost)

})

I want these to be reactive, too, so they change based on user selections. The domain argument defines the values that the palette will be displaying. In the first case, it’s my reactive mapdata object's MedianHouseholdIncome column—with mapdata coded as mapdata() since it's reactive; in the second case, it's the MedianMonthlyHousingCost column.

I'll also set up exactly how I want my popup text to appear. This can take a mixture of HTML (the

is an HTML line break) and data frame columns. While you can certainly use base R’s paste() or paste0() functions, I find the glue package much easier when dealing with more than one variable mixed in with text. You can see below that I just need to enclose variables I want evaluated within curly braces. Of course, the popup text needs to be reactive as well, so it, too, changes with the user’s selection.

mypopups <- reactive({

glue::glue("ZIP Code: {mapdata()$ZipCode}

Median Household Income: {mapdata()$income}

Median Monthly Housing Cost: {mapdata()$housing}

Population: {mapdata()$Pop}

City: {mapdata()$City}

County: {mapdata()$County}")

})

Finally, code for the leaflet map itself.

leaflet::renderLeaflet({

leaflet(mapdata()) %>%

addProviderTiles("CartoDB.Positron") %>%

addPolygons(fillColor = ~incomepal()(mapdata()$MedianHouseholdIncome),

fillOpacity = 0.7,

weight = 1.0,

color = "black",

smoothFactor = 0.2,

popup = mypopups(),

group = "Household Income"

) %>%

addPolygons(fillColor = ~housingpal()(mapdata()$MedianMonthlyHousingCost),

fillOpacity = 0.7,

weight = 0.2,

color = "black",

smoothFactor = 0.2,

popup = mypopups(),

group = "Housing Costs"

) %>%

addLayersControl(

baseGroups=c("Household Income", "Housing Costs"),

position = "bottomleft",

options = layersControlOptions(collapsed = FALSE)

)

})

renderLeaflet() is the Shiny render function that will display the dynamic dataviz relying on the dynamic mapdata object. Inside that function is "regular" leaflet mapping code. The first line, leaflet(mapdata()), creates an R leaflet object from the reactive mapdata object. It is using the leaflet package, which is an R wrapper to the leaflet.js library. Next line adds a style of background map tiles from CartoDB.

The addPolygons() function tells leaflet how to display the ZIP code polygons. I want it colored by the MideanHouseholdIncome column using the income palette I set up earlier, incomepal. Most of the rest of those arguments are styling. The popup argument sets the popup text to be the mypopups object I created earlier, and the group argument gives a name to the map layer.

Aggiungo un altro livello simile per i costi mensili mediani degli alloggi. E, infine, addLayersControl()mette una legenda cliccabile per ogni livello in basso a sinistra.

Sharon Machlis /

Se desideri saperne di più sulla mappatura in R con depliant, guarda il mio tutorial "Creare mappe in R in 10 (abbastanza) semplici passaggi".

Il file di markdown R finale

Puoi vedere il file R Markdown finale su GitHub. Se guardi attentamente il codice, potresti notare alcune aggiunte. Ho aggiunto All Mass al selectInput()vettore di scelta dell'elenco a discesa, quindi il codice è ora

selectInput ("mycities", "Scegli 1 o più città:",

scelte = c ("All Mass", sort (unique (markdowndata $ City))),

multiplo = VERO, selezionato = "Boston")

And then I tweaked several other lines of code to give a different option if All Mass is selected, such as creating a dynamic variable selected_places that will say "Massachusetts" if "All Mass" is one of the selected cities.

selected_places <- reactive({

if("All Mass" %in% input$mycities){

"Massachusetts"

} else {

paste(input$mycities,

sep = " ", collapse = ", ")

}

})

Note also the new YAML header:

---

title: "Median Household Income by ZIP Code"

output: html_document

resource_files:

- mamarkdowndata.rdata

- zip_mass_appdata_for_map.rds

runtime: shiny

---

That resources_files: option says that this document requires two other files in order to run, mamarkdowndata.rdata and zip_mass_appdata_for_map.rds. This lets shinyapps.io know those files need to be uploaded along with the main R Markdown document when deploying a file with  rsconnect::deployDoc("docname.Rmd").

You can see this interactive R Markdown document with Shiny in action at //idgrapps.shinyapps.io/runtimeshiny/. It may take a little while to load, since I didn't attempt to optimize this code for speed. RStudio has some resources if you want to learn about speeding up Shiny apps.

How is this different from a 'real' Shiny app?

This super-charged-with-Shiny R Markdown document differs from a full-fledged Shiny app in a few key ways.

1. A Shiny app needs to be in one file called app.R or two files ui.R and server.R. The app can source additional files with other names, but that file-naming structure is absolute. In a one-file app.R app, sections are needed for the ui (user interface, which defines what the user sees and interacts with) and the server.

2. Shiny app layouts are built around the Bootstrap page grid framework. You can see more about layout structure at RStudio's Shiny application layout guide.

3. Most dynamic components that you want to render, including things like graphs and tables, need to be specifically placed somewhere on the page with additional Output functions and definitions. For example, an interactive leaflet map would need code such as leafletOutput("mymap") somewhere in the ui to tell the app where it should display,  in addition to server code such as 

output$mymap <- renderLeaflet({ #MAP CODE HERE }) 

to define the logic behind generating the map.

Here is an example of a Shiny app.R file for this app's histogram and table:

library("shiny")

library("dplyr")

library("ggplot2")

library("DT")

options(scipen = 999)

load("mamarkdowndata.rdata") # loads variable markdowndata

ma_appdata_for_map <- readRDS("zip_mass_appdata_for_map.rds")

# Define UI

ui <- fluidPage(

# Application title

titlePanel("Income and Housing Costs by ZIP Code"),

# Sidebar

sidebarLayout(

sidebarPanel(

selectInput("mycities", "Choose 1 or more Massachusetts places: ", choices = c("All Mass", sort(unique(markdowndata$City))), multiple = TRUE, selected = "Boston"),

br(),

strong("Note: some cities may have more than one place name for ZIP codes. For example, Allston, Brighton, Dorchester, and several other neighborhoods are not included in ZIP code place name \"Boston\".")

),

# Show histogram

mainPanel(

h4(htmlOutput("histogramHeadline")),

plotOutput("myhistogram"),

br(),

h4(htmlOutput("tableHeadline")),

DTOutput("mytable")

)

)

)

# Define server logic required to draw a histogram

server <- function(input, output) {

mydata <- reactive({

if("All Mass" %in% input$mycities){

markdowndata

} else {

filter(markdowndata, City %in% input$mycities)

}

})

selected_places <- reactive({

if("All Mass" %in% input$mycities){

"Massachusetts"

} else {

paste(input$mycities,

sep = " ", collapse = ", ")

}

})

output$histogramHeadline <- renderUI({

paste("Histogram for", selected_places(), " income data")

})

output$tableHeadline <- renderUI({

paste("Data for", selected_places())

})

output$myhistogram <- renderPlot({

ggplot(mydata(), aes(x = MedianHouseholdIncome)) +

geom_histogram(binwidth = 20000, color = "black", fill = "darkgreen") +

theme_classic() +

xlab("") +

ylab("") +

scale_x_continuous(labels = dollar)

})

output$mytable <- renderDT({

DT::datatable(mydata(), filter = 'top') %>%

formatCurrency(4:5, digits = 0) %>%

formatCurrency (6, currency = "", digits = 0)

})

}

# Esegui l'applicazione

shinyApp (ui = ui, server = server)

Puoi trovare ulteriori informazioni sulla creazione di questo tipo di app Shiny nei tutorial introduttivi di Shiny di RStudio.

Per ulteriori suggerimenti R, vai alla pagina del video Fai di più con R o alla playlist Fai di più con R su YouTube.