This simple Shiny app has a dropdown of various selectable options (selectInput), each of the options in selectInput have a .Rmd file matching its name. Based on the option selected through the dropdown I'm trying to call its respective .Rmd file, complete code below.
The eventReactive() method seems to get the selected value and is passed as an input to call the respective .Rmd file, however the code executes but does not generate the report.
Alternatively, commenting out eventReactive() and calling out the required .Rmd file as a variable (report_name) generates a downloadable report with the required content. How do I go about fixing this to get the desired functionality?
app.R
library(shiny)
ui <- fluidPage(
selectInput("select_id",
"Select an option",
list(
" "=" ",
"apples"='apples',
"oranges"='oranges')
),
radioButtons('format', 'Document format', c('HTML', 'Word'),
inline = TRUE),
downloadButton('downloadReport')
)
server <- function(input, output, session) {
# download of report selectable format based on button press
output$downloadReport <- downloadHandler(
filename = function() {
paste('my-report', sep = '.', switch(
input$format, PDF = 'pdf', HTML = 'html'
))
},
# get selected option from dropdown and use the input value as the name to call the Rmd file
report_name <- eventReactive(input$select_id, {
paste(input$select_id,'Rmd', sep =".")
}),
# replacing report_name() with report_name [below line] generates report, but is not the desired outcome
# report_name <- "apples.Rmd",
content = function(file) {
cat('The selected fruit report is - - - :',report_name())
src <- normalizePath(report_name())
# temporarily switch to the temp dir, in case you do not have write
# permission to the current working directory
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, report_name(), overwrite = TRUE)
out <- rmarkdown::render(report_name(),
switch(input$format,
PDF = pdf_document(),
HTML = html_document()
))
file.rename(out, file)
}
)
}
shinyApp(ui, server)
apples.Rmd
---
title: "This is a report for Apples"
output: html_document
params:
tbl: NA
include: NA
---
oranges.Rmd
---
title: "This is a report for Oranges"
output: html_document
params:
tbl: NA
include: NA
---