library(shiny)library(rmarkdown)library(ggplot2)data <-data.frame(ID =1:10,Name =c("John", "Paul", "George", "Ringo", "Mick", "Keith", "Charlie", "Ronnie", "Roger", "Pete"),Age =c(40, 42, 38, 41, 43, 45, 39, 40, 44, 42))server <-function(input, output) { filtered_data <-reactive({ data[data$Age >= input$ageRange[1] & data$Age <= input$ageRange[2], ] }) output$distPlot <-renderPlot({ggplot(filtered_data(),aes(x=Age)) +geom_histogram() }) output$downloadReport <-downloadHandler(filename =function() {paste('my-report', sep ='.', switch( input$format, PDF ='pdf', HTML ='html', Word ='docx' )) },content =function(file) { src <-normalizePath('child_script.Rmd') owd <-setwd(tempdir())on.exit(setwd(owd))file.copy(src, 'child_script.Rmd', overwrite =TRUE) out <- rmarkdown::render('child_script.Rmd',params =list(text = input$text, outp=input$Try, age=input$ageRange),switch(input$format,PDF =pdf_document(), HTML =html_document(), Word =word_document() ))file.rename(out, file) } )}ui <-fluidPage( tags$textarea(id="text", rows=10, cols=15, placeholder="Some placeholder text"),flowLayout(sliderInput('ageRange', 'Age Range', min =30, max =45, value =c(30, 45)),plotOutput("distPlot"),radioButtons('format', 'Document format', c('HTML', 'Word','PDF'),inline =FALSE),checkboxGroupInput("Try","Let's hope this works",choiceNames =list("include hi","include hey","include hello","include how are you"),choiceValues =list("HI","HEY","HELLO","HOW ARE YOU")),downloadButton('downloadReport')))shinyApp(ui = ui, server = server)
1
Most interactive functionality currently dependent on this filtered_data object (dataframe)
2
Rendered report name and proper file extension (.html, .pdf, or .docx)
3
Locates and renders “child” .Rmd report template – parameters also identified here
4
Our two elements of most immediate interest (in addition to the downloadReport button on line #76)
---title:"Testing with Diego"output: html_documentparams: text:'NULL' outp:'NULL' age:'NULL'---# Fiddling around`r params[["text"]]``r params[["outp"]]`the selected age range is `r params[["age"]]```{r, warning=FALSE, echo=FALSE, message=FALSE}library(ggplot2)ggplot(filtered_data(),aes(x=Age)) +geom_histogram()``the picture above is reflecting ages from `r nrow(filtered_data())` British rockers.
1
Default output type for report – UI has check-box options to override this default
2
One way to “call in” values from dynamic reactive app elements
3
dataname() convention used with reactive dataframes