Hi There,
I have this shiny app with a tagList that I'd like to access when I download a Report:
shinyApp(
ui = fluidPage(
sliderInput("slider", "Slider", 1, 100, 50),
textInput("FN", label = h3("File name:"),
value = "File name"),
textInput("YN", label = h3("Your name:"),
value = "Your name"),
radioButtons('format', 'Document format', c('PDF', 'Word'),
inline = TRUE),
downloadButton("report", "Generate report"),
uiOutput("RanInput")
),
server = function(input, output) {
R <- reactive({
sapply(1:5, function(i){paste0("input$Rand",i)})
})
output$RanInput <- renderUI({
output = tagList()
G = letters[1:5]
for(i in seq_along(1:5)){
output[[i]] = tagList()
output[[i]][[1]] = hr(style="height:5px;background-color:blue")
output[[i]][[2]] = G[i]
output[[i]][[3]] = br()
output[[i]][[4]] = selectInput(R()[i], "Treatment or control:",
c("Treatment" = "1", "Control" = "0"))
} ## for loop
output
})
output$report <- downloadHandler(
## Making the filename for the report here. Using two different
## extensions for the file
filename = function() {
paste(input$FN, sep = '.', switch(input$format, PDF = 'pdf',
Word = 'docx'))
},
content = function(file) {
## Copy the report file to a temporary directory before processing it, in
## case we don't have write permissions to the current working dir (which
## can happen when deployed).
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
# Set up parameters to pass to Rmd document
params <- list(n = input$slider,
R = R())
# Knit the document, passing in the `params` list, and eval it in a
# child of the global environment (this isolates the code in the document
# from the code in this app).
library(rmarkdown)
rmarkdown::render(tempReport,
switch(input$format,
PDF = pdf_document(), Word = word_document()),
output_file = file, params = params,
envir = new.env(parent = globalenv())
)
}
)
}
)
and the report file is as follows:
---
title: "Dynamic report"
output: html_document
params:
n: NA
R: NA
---
A plot of `params$n` random points.
```{r}
plot(rnorm(params$n), rnorm(params$n))
\```
Playing with R
```{r}
(data.frame(params$R))
sum(as.numeric(params$R))
\```
The problem I'm having is that the stupid f#@*#%ng pdf file has this as the output:
(data.frame(params$R))
params.R
1 input$Rand1
2 input$Rand2
3 input$Rand3
4 input$Rand4
5 input$Rand5
and
sum(as.numeric(params$R))
Warning: NAs introduced by coercion
[1] NA
Which means that it's not picking up the values. There must be a way around this, but I've not got a clue. Computers are so stupid, luckily we have each other.
gwynn