knitr::opts_chunk$set( collapse = TRUE, message = FALSE, fig.align = "center", out.width = "80%", class.output = "R", comment = "" ) library(shiny) library(shinymeta) library(dplyr) library(ggplot2) options(shiny.suppressMissingContextError = TRUE) input <- list(package = "ggplot2") output <- list() downloads <- metaReactive({ cranlogs::cran_downloads(..(input$package), from = Sys.Date() - 365, to = Sys.Date()) }) downloads_rolling <- metaReactive2({ validate(need(sum(downloads()$count) > 0, "Input a valid package name")) metaExpr({ ..(downloads()) %>% mutate(count = zoo::rollapply(count, 7, mean, fill = "extend")) }) }) output$plot <- metaRender(renderPlot, { ggplot(..(downloads_rolling()), aes(date, count)) + geom_line() }) # Only show the first few rows library(knitr) knit_print.data.frame <- function(x, ...) { if (nrow(x) > 10) { normal_print(head(x, 6)) cat("[...plus", nrow(x) - 6, "more rows...]\n") } } # register the method registerS3method("knit_print", "data.frame", knit_print.data.frame)
```{css echo=FALSE} pre { border: 1px solid #eee; }
pre.r { background-color: #ffffff; }
pre.r code { background-color: #ffffff; }
pre.R { background-color: #f8f8f8; border-radius: 0px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; }
.sourceCode .R { margin-top: -1em; }
## Motivating example Below is a reduced version of the [cranview](https://github.com/cpsievert/cranview) Shiny app that allows you to enter an R package name to generate a plot of its [CRAN](https://cran.r-project.org/) downloads over the past year. This app provides a nice example of how to modify an existing Shiny app so that it can generate code to reproduce what a user sees in the app: ```r library(shiny) library(tidyverse) ui <- fluidPage( textInput("package", "Package name", value = "ggplot2"), plotOutput("plot") ) server <- function(input, output, session) { downloads <- reactive({ cranlogs::cran_downloads(input$package, from = Sys.Date() - 365, to = Sys.Date()) }) downloads_rolling <- reactive({ validate(need(sum(downloads()$count) > 0, "Input a valid package name")) downloads() %>% mutate(count = zoo::rollapply(count, 7, mean, fill = "extend")) }) output$plot <- renderPlot({ ggplot(downloads_rolling(), aes(date, count)) + geom_line() }) } shinyApp(ui, server)
Below is a modified version of the app that generates code to reproduce output$plot
outside of the shiny session (via shinymeta). In the screencast of the app below, note how both output$plot
and output$code
update dynamically in response to user input. To keep the focus on code generation, we've presented the output$code
as simple as possible here (by using verbatimTextOutput()
and renderPrint()
), but the next article outlines the various options distributing code to users.
library(shiny) library(tidyverse) library(shinymeta) ui <- fluidPage( textInput("package", "Package name", value = "ggplot2"), verbatimTextOutput("code"), plotOutput("plot") ) server <- function(input, output, session) { downloads <- metaReactive({ cranlogs::cran_downloads(..(input$package), from = Sys.Date() - 365, to = Sys.Date()) }) downloads_rolling <- metaReactive2({ validate(need(sum(downloads()$count) > 0, "Input a valid package name")) metaExpr({ ..(downloads()) %>% mutate(count = zoo::rollapply(count, 7, mean, fill = "extend")) }) }) output$plot <- metaRender(renderPlot, { ggplot(..(downloads_rolling()), aes(date, count)) + geom_line() }) output$code <- renderPrint({ expandChain( quote(library(tidyverse)), output$plot() ) }) } shinyApp(ui, server)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.