Hello:
I would like to generate a little quiz via Shiny and put the output into a PDF file.
My first question: what is the best way to display the radioButtons in the PDF, please?
Currently, I am using a data frame from rmarkdown, but it looks pretty lame.
Second, when I run the app, it completes successfully, but I get an error of "report.html: Failed - No file".
Here is my Shiny code:
q1c <- c(0,0.1,0.25,0.5)
q1.df <- data.frame(x=rep("- [ ]",4),y=q1c)
q2c <- LETTERS[1:4]
q2.df <- data.frame(x=rep("- [ ]",4),y=q2c)
shinyApp(
ui = fluidPage(
radioButtons("q1","Prob question 1",choices=q1c,
selected=""),
radioButtons("q2","Prob question 2", choices=q2c,
selected=""),
downloadButton("report", "Generate report")
),
server = function(input, output) {
output$report <- downloadHandler(
# For PDF output, change this to "report.pdf"
filename = "report.pdf",
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(),"reportb.Rmd")
tempReport <- "reportb.Rmd"
#file.copy("reportb.Rmd", tempReport, overwrite = TRUE)
# Set up parameters to pass to Rmd document
y1 <- which(q1.df[,2]==input$q1)
q1.df[y1,1] <- "- [x]"
y2 <- which(q2.df[,2]==input$q2)
params <- list(q1=q1.df,q2=q2.df)
# 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).
rmarkdown::render(tempReport, output_file = "erep2.pdf",
output_format="pdf_document",
params = params,
envir = new.env()
)
}
)
}
)
and here is the Rmd file
---
title: "Dynamic report"
output: pdf_document
params:
q1: NA
q2: NA
---
```{r eval = FALSE, echo = FALSE}
# For PDF output, change the header to have "output: pdf_document".
#
# Note that due to an issue in rmarkdown, the default value of a parameter in
# the header cannot be `NULL`, so I used a default of `NA` for the default value
# of `n`.
Question 1:
print(params$q1)
The correct answer is d, 0.5.
Question 2:
print(params$q2)
The correct answer is C.