Ubah dokumen R Markdown menjadi pengalaman interaktif

R Markdown adalah salah satu perkara kegemaran saya mengenai R. moden. Ia menawarkan cara mudah untuk menggabungkan teks, kod R, dan hasil kod R dalam satu dokumen. Dan apabila dokumen tersebut diberikan sebagai HTML, anda dapat menambahkan beberapa interaksi pengguna dengan widget HTML seperti DT untuk jadual atau risalah untuk peta. (Sekiranya anda tidak biasa dengan R Markdown, anda boleh melihat tutorial video R Markdown saya terlebih dahulu dan kemudian kembali ke sini.) 

Tetapi anda mungkin tidak tahu bahawa ada cara untuk meningkatkan interaktiviti R Markdown: dengan menambahkan runtime: shinyke tajuk dokumen.

Shiny adalah kerangka aplikasi Web untuk R. Sebagai kerangka, ia mempunyai struktur yang cukup spesifik. Walau bagaimanapun, anda boleh menukar dokumen R Markdown menjadi aplikasi Berkilau tanpa perlu mengikuti banyak struktur kaku itu . Sebagai gantinya, anda boleh melompat masuk dan mula membuat pengekodan — tanpa perlu risau tentang beberapa tugas Berkilat seperti memastikan semua tanda kurung dan koma anda betul dalam fungsi susun atur yang bersarang.

Sebenarnya, walaupun anda seorang pembangun berkilat yang berpengalaman, dokumen R Markdown masih boleh berguna untuk tugas-tugas berkilau di mana anda tidak memerlukan aplikasi lengkap atau untuk mencuba kod dengan cepat. Ia masih memerlukan pelayan Shiny, tetapi jika anda memasang RStudio dan pakej berkilat, anda sudah mempunyai salah satu daripadanya secara tempatan.

Mari kita lihat bagaimana runtime shiny berfungsi di R Markdown.

1. Penurunan Nilai Asas R

Saya akan mulakan dengan dokumen Rdowndown konvensional dan tidak berkilat yang memaparkan jadual data yang boleh dicari oleh kod ZIP Massachusetts. Pengguna boleh mencari atau menyusun mengikut lajur jadual mana pun, menjawab soalan seperti "Poskod mana yang mempunyai pendapatan isi rumah rata-rata tertinggi di Middlesex County?" atau "Poskod apa yang mempunyai perumahan bulanan yang paling mahal?"

Sharon Machlis /

Dokumen ini juga mempunyai histogram yang menunjukkan taburan pendapatan isi rumah dan teks yang menyatakan kod ZIP mana yang mempunyai pendapatan tertinggi dan terendah. Jadualnya interaktif, tetapi dokumen yang lain tidak. Anda dapat melihat versi HTML yang diberikan pada RStubio's RPubs.

Sekiranya anda ingin mengikuti, anda dapat melihat kod untuk versi tersendiri dari dokumen R Markdown ini - termasuk data - di GitHub. Atau, jika anda ingin melihat bagaimana saya memasukkan data demografi ini ke dalam R, ada kod R dalam artikel ini untuk membuat set data anda sendiri (dan anda boleh mengubah kod untuk memilih keadaan lain). Sekiranya anda membuat versi data anda sendiri, kod untuk dokumen R Markdown asas menggunakan fail data yang terpisah juga terdapat di GitHub.

Mana-mana dokumen R Markdown yang anda pilih, anda akan melihat bahawa dokumen itu kebanyakannya statik dengan beberapa interaktiviti. Tetapi bagaimana jika saya mahu keseluruhan dokumen menjadi interaktif - dalam hal ini, lihat histogram dan perubahan teks serta jadualnya? Bagaimana pengguna dapat memilih setiap bandar dan melihat semua maklumat yang disaring untuk dipaparkan hanya untuk tempat-tempat tersebut?

Salah satu penyelesaiannya ialah menghasilkan halaman untuk setiap kota — mungkin dengan skrip R jika anda menggunakan apa yang disebut laporan parameter. Walau bagaimanapun, anda juga boleh membuat satu dokumen R Markdown yang berfungsi seperti aplikasi interaktif.

Tambah interaktiviti Berkilat

Untuk menambahkan interaktiviti Shiny ke dokumen R Markdown konvensional, mulakan dengan menambahkan runtime: shinyke tajuk YAML dokumen, seperti:

---

tajuk: "Pendapatan Isi Rumah Median mengikut Poskod"

output: html_dokumen

runtime: berkilat

---

Setelah anda melakukannya dan tekan Simpan, ikon rajutan di RStudio berubah menjadi "Jalankan dokumen." Walaupun output masih mengatakan "html_document", itu tidak akan menjadi HTML biasa lagi. Kini aplikasi mini-Shiny. 

Sharon Machlis / Sharon Machlis,

Biarkan pengguna membuat pilihan data

Sekarang saya memerlukan kaedah untuk pengguna membuat pilihan data mereka. Shiny memiliki sejumlah "widget input" untuk ini. Saya akan gunakan selectInput(), yang membuat senarai dropdown dan membolehkan pengguna memilih lebih dari satu item. Shiny mempunyai widget lain untuk butang radio, input teks, pemilih tarikh, dan banyak lagi. Anda dapat melihat koleksi mereka di Galeri Stinyio Shiny Widgets. 

Kod untuk selectInput()senarai drop - down aplikasi mini saya mempunyai lima argumen dan kelihatan seperti ini:

selectInput ("mycities", "Pilih 1 atau lebih bandar:",

pilihan = urutkan (unik (markdowndata $ City)),

dipilih = "Boston", berganda = BENAR)

Hujah pertama  selectInput(), mycitiesadalah nama yang berubah-ubah saya telah memilih untuk menyimpan apa sahaja yang menghargai picks pengguna. Argumen kedua adalah teks tajuk yang akan muncul dengan senarai juntai bawah. Argumen ketiga choices, adalah vektor semua nilai yang mungkin ada dalam senarai juntai bawah - dalam hal ini, nilai unik nama bandar dalam data saya, disusun mengikut abjad. selected = Bostonbermaksud dropdown secara lalai menjadikan Boston menjadi bandar terpilih (memilih pilihan lalai adalah pilihan). Dan, akhirnya, multiple = TRUEmembolehkan pengguna memilih lebih dari satu bandar pada satu masa.

Kod ini membuat senarai lungsur HTML. Sekiranya anda menjalankan selectInput()kod tersebut di konsol R anda, ia akan menghasilkan HTML untuk dropdown (dengan andaian anda telah memuatkan Shiny dan bingkai data yang disebut markdowndata dengan lajur City). 

Seterusnya, saya perlu menulis beberapa R supaya dropdown ini benar-benar melakukan sesuatu.

Buat pemboleh ubah dinamik

Saya akan kodkan logik interaktiviti ini dalam dua bahagian:

  1. Buat kerangka data — Saya akan memanggilnya mydata— yang disaring setiap kali pengguna memilih bandar.
  2. Tulis kod untuk teks, histogram, dan jadual data yang semuanya akan berubah berdasarkan kerangka data dinamik saya.

Perkara paling penting yang perlu diingat pada ketika ini adalah bahawa objek-objek ini bukan lagi pemboleh ubah R biasa Mereka dinamik . Mereka berubah berdasarkan tindakan pengguna . Ini bermakna ia berfungsi sedikit berbeza daripada pemboleh ubah yang mungkin anda biasa.

Apa yang istimewa dari mereka? Berikut adalah tiga perkara yang perlu anda ketahui:

  1. Untuk mengakses nilai pemboleh ubah input yang menyimpan maklumat dari pengguna anda, anda memerlukan sintaks input$myvarname, bukan sekadar myvarname. Jadi, untuk nilai yang disimpan dalam mycitiessenarai dropdown, gunakan 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.

Saya menambah satu lagi lapisan yang serupa untuk kos perumahan bulanan rata-rata. Dan, akhirnya, addLayersControl()meletakkan legenda yang dapat diklik untuk setiap lapisan di kiri bawah.

Sharon Machlis /

Sekiranya anda ingin mempelajari lebih lanjut tentang pemetaan dalam R dengan risalah, lihat tutorial saya "Buat peta dalam R dalam 10 langkah mudah

Fail penurunan nilai akhir R

Anda dapat melihat fail R Markdown terakhir di GitHub. Sekiranya anda melihat kodnya dengan teliti, anda mungkin melihat beberapa penambahan. Saya menambahkan All Mass ke selectInput()vektor pilihan senarai dropdown, jadi kodnya sekarang

selectInput ("mycities", "Pilih 1 atau lebih bandar:",

pilihan = c ("Semua Mass", urutkan (unik (markdowndata $ City)))

berganda = BENAR, dipilih = "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, mata wang = "", digit = 0)

})

}

# Jalankan aplikasi

shinyApp (ui = ui, pelayan = pelayan)

Anda boleh mengetahui lebih lanjut mengenai membina aplikasi Shiny semacam ini di tutorial intro RStudio's Shiny.

Untuk lebih banyak petua R, pergi ke halaman video Do More With R di atau senarai main Do More With R di YouTube.