Ubah dokumen R Markdown menjadi pengalaman interaktif

R Markdown adalah salah satu hal favorit saya tentang R. modern Ini menawarkan cara mudah untuk menggabungkan teks, kode R, dan hasil kode R dalam satu dokumen. Dan ketika dokumen itu dirender sebagai HTML, Anda dapat menambahkan beberapa interaksi pengguna dengan widget HTML seperti DT untuk tabel atau selebaran untuk peta. (Jika Anda tidak terbiasa dengan R Markdown, Anda dapat melihat tutorial video R Markdown saya terlebih dahulu dan kemudian kembali ke sini.) 

Tetapi Anda mungkin tidak tahu bahwa ada cara untuk lebih meningkatkan interaktivitas Markdown R: dengan menambahkan runtime: shinyke header dokumen.

Shiny adalah framework aplikasi Web untuk R. Sebagai framework, ia memiliki struktur yang cukup spesifik. Namun, Anda dapat mengubah dokumen R Markdown menjadi aplikasi Shiny tanpa harus mengikuti banyak struktur kaku itu . Sebagai gantinya, Anda dapat langsung masuk dan mulai membuat kode — tanpa mengkhawatirkan beberapa tugas Shiny yang umum seperti memastikan semua tanda kurung dan koma Anda benar dalam fungsi tata letak yang sangat bertingkat.

Faktanya, bahkan jika Anda adalah pengembang yang berpengalaman, dokumen R Markdown masih dapat berguna untuk tugas-tugas mengkilap di mana Anda tidak memerlukan aplikasi lengkap atau untuk mencoba kode dengan cepat. Ini masih membutuhkan server Shiny, tetapi jika Anda memiliki RStudio dan paket mengkilap terinstal, Anda sudah memiliki salah satunya secara lokal.

Mari kita lihat cara kerja mengkilap runtime di R Markdown.

1. Markdown R Dasar

Saya akan mulai dengan dokumen konvensional, non-Shiny R Markdown yang menampilkan tabel data yang dapat dicari dengan kode pos Massachusetts. Pengguna dapat menelusuri atau mengurutkan berdasarkan kolom tabel mana pun, menjawab pertanyaan seperti “Kode pos mana yang memiliki median pendapatan rumah tangga tertinggi Middlesex County?” atau “Kode pos apa yang memiliki rumah bulanan termahal?”

Sharon Machlis /

Dokumen ini juga memiliki histogram yang menunjukkan distribusi pendapatan rumah tangga median dan teks yang menyatakan kode pos mana yang memiliki pendapatan tertinggi dan terendah. Tabel ini interaktif, tetapi dokumen lainnya tidak. Anda dapat melihat versi HTML yang di-render di RStudio's RPubs.

Jika Anda ingin mengikutinya, Anda dapat melihat kode untuk versi yang berdiri sendiri dari dokumen Penurunan Harga R ini — termasuk datanya — di GitHub. Atau, jika Anda ingin melihat bagaimana saya mendapatkan data demografis ini menjadi R, ada kode R di artikel ini untuk membuat kumpulan data Anda sendiri (dan Anda dapat mengubah kode untuk memilih status lain). Jika Anda membuat versi data Anda sendiri, kode untuk dokumen Penurunan Harga R dasar menggunakan file data terpisah juga ada di GitHub.

Dokumen R Markdown mana pun yang Anda pilih, Anda akan melihat bahwa itu sebagian besar dokumen statis dengan beberapa interaktivitas. Tetapi bagaimana jika saya ingin seluruh dokumen menjadi interaktif — dalam hal ini, lihat histogram dan teks berubah serta tabel? Bagaimana pengguna dapat memilih kota satu per satu dan melihat semua informasi yang difilter untuk ditampilkan hanya untuk tempat-tempat itu?

Salah satu solusinya adalah membuat halaman untuk setiap kota — dapat dilakukan dengan skrip R jika Anda menggunakan apa yang disebut laporan berparameter. Namun, Anda juga dapat membuat satu dokumen R Markdown yang berfungsi seperti aplikasi interaktif.

Tambahkan interaktivitas Shiny

Untuk menambahkan interaktivitas Shiny ke dokumen R Markdown konvensional, mulailah dengan menambahkan runtime: shinyke header YAML dokumen, seperti:

---

judul: "Pendapatan Rumah Tangga Median menurut Kode Pos"

keluaran: html_document

runtime: mengkilap

---

Setelah Anda melakukannya dan menekan Simpan, ikon rajutan di RStudio berubah menjadi "Jalankan dokumen". Meskipun keluarannya masih bertuliskan "html_document", itu tidak lagi menjadi HTML biasa. Sekarang aplikasi mini-Shiny. 

Sharon Machlis / Sharon Machlis,

Biarkan pengguna membuat pilihan data

Sekarang saya membutuhkan cara bagi pengguna untuk membuat pilihan datanya. Shiny memiliki sejumlah "widget masukan" untuk ini. Saya akan menggunakan selectInput(), yang membuat daftar tarik-turun dan memungkinkan pengguna memilih lebih dari satu item. Shiny memiliki widget lain untuk tombol radio, input teks, pemilih tanggal, dan banyak lagi. Anda dapat melihat koleksi mereka di RStudio's Shiny Widgets Gallery. 

Kode untuk selectInput()daftar tarik - turun aplikasi mini saya memiliki lima argumen dan terlihat seperti ini:

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

options = sort (unique (markdowndata $ City)),

dipilih = "Boston", kelipatan = TRUE)

Argumen pertama  selectInput(), mycitiesadalah nama variabel yang saya pilih untuk menyimpan nilai apa pun yang dipilih pengguna. Argumen kedua adalah teks header yang akan muncul dengan daftar dropdown. Argumen ketiga choices,, adalah vektor dari semua kemungkinan nilai dalam daftar tarik-turun — dalam hal ini, nilai unik nama kota dalam data saya, diurutkan menurut abjad. selected = Bostonberarti dropdown akan default ke Boston sebagai kota yang dipilih (memilih pilihan default adalah opsional). Dan, akhirnya, multiple = TRUEmemungkinkan pengguna untuk memilih lebih dari satu kota dalam satu waktu.

Kode ini membuat daftar dropdown HTML. Jika Anda menjalankan selectInput()kode itu di konsol R Anda, itu akan menghasilkan HTML untuk dropdown (dengan asumsi Anda memiliki Shiny dimuat dan bingkai data disebut markdowndata dengan kolom Kota). 

Selanjutnya, saya perlu menulis beberapa R sehingga dropdown ini benar-benar melakukan sesuatu.

Buat variabel dinamis

Saya akan mengkodekan logika interaktivitas ini dalam dua bagian:

  1. Buat bingkai data — saya akan menyebutnya mydata— yang difilter setiap kali pengguna memilih kota.
  2. Tulis kode untuk teks, histogram, dan tabel data yang semuanya akan berubah berdasarkan bingkai data dinamis saya.

Hal terpenting yang perlu diingat saat ini adalah bahwa objek ini bukan lagi variabel R "biasa". Mereka dinamis . Mereka berubah berdasarkan tindakan pengguna . Dan itu berarti mereka bekerja sedikit berbeda dari variabel yang mungkin biasa Anda gunakan.

Apa yang spesial dari mereka? Inilah tiga hal yang perlu Anda ketahui:

  1. Untuk mengakses nilai variabel input yang menyimpan informasi dari pengguna Anda, Anda memerlukan sintaks input$myvarname, tidak hanya myvarname. Jadi, untuk nilai yang disimpan dalam mycitiesdaftar 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 menambahkan lapisan serupa lainnya untuk biaya perumahan bulanan rata-rata. Dan, terakhir, addLayersControl()menempatkan legenda yang dapat diklik untuk setiap lapisan di kiri bawah.

Sharon Machlis /

Jika Anda ingin mempelajari lebih lanjut tentang pemetaan di R dengan selebaran, lihat tutorial saya "Membuat peta di R dalam 10 (cukup) langkah mudah".

File penurunan harga R terakhir

Anda dapat melihat file R Markdown terakhir di GitHub. Jika Anda memperhatikan kodenya dengan cermat, Anda mungkin melihat beberapa tambahan. Saya menambahkan All Mass ke selectInput()vektor pilihan daftar dropdown, jadi kodenya sekarang

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

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

multiple = TRUE, 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, currency = "", digit = 0)

})

}

# Jalankan aplikasi

shinyApp (ui = ui, server = server)

Anda dapat mengetahui lebih lanjut tentang membuat aplikasi Shiny semacam ini di tutorial intro Shiny RStudio.

Untuk tips R lainnya, buka halaman video Do More With R di atau Do More With R playlist di YouTube.