-1

I currently have a table being generated and I would like the user to be able to create a pdf file when they click the download button.

I am currently getting an error where when I click the download button I get an html file that downloads the entire page of the app. I thought that using pdf(file) would work but it ignores the function.

Here is currently what I have.

library(shiny) library(xlsx) library(shinyWidgets) population <- read.xlsx("population.xlsx", 1) fieldsMandatory <- c("selectedCountry") labelMandatory <- function(label) { tagList( label, span("*", class = "mandatory_star") ) } appCSS <- ".mandatory_star {color: red;}" ui <- fluidPage( navbarPage(title = span("Spatial Tracking of COVID-19 using Mathematical Models", style = "color:#000000; font-weight:bold; font-size:15pt"), tabPanel(title = "Model", sidebarLayout( sidebarPanel( shinyjs::useShinyjs(), shinyjs::inlineCSS(appCSS), div( id = "dashboard", pickerInput( inputId = "selectedCountry", labelMandatory ("Country"), choices = population$Country, multiple = FALSE, options = pickerOptions( actionsBox = TRUE, title = "Please select a country") ), sliderInput(inputId = "agg", label = "Aggregation Factor", min = 0, max = 50, step = 5, value = 10), actionButton("go","Run Simulation"), ) ), mainPanel( tabsetPanel( tabPanel("Input Summary", verbatimTextOutput("summary"), tableOutput("table"), downloadButton(outputId = "downloadSummary", label = "Save Summary")) ) ) ) ) ) ) server <- function(input, output, session){ observeEvent(input$resetAll, { shinyjs::reset("dashboard") }) values <- reactiveValues() values$df <- data.frame(Variable = character(), Value = character()) observeEvent(input$go, { row1 <- data.frame(Variable = "Country", Value = input$selectedCountry) row2 <- data.frame(Variable = "Aggregation Factor", Value = input$agg) values$df <- rbind(row1, row2) }) output$table <- renderTable(values$df) observe({ # check if all mandatory fields have a value mandatoryFilled <- vapply(fieldsMandatory, function(x) { !is.null(input[[x]]) && input[[x]] != "" }, logical(1)) mandatoryFilled <- all(mandatoryFilled) # enable/disable the submit button shinyjs::toggleState(id = "go", condition = mandatoryFilled) }) output$downloadSummary <- downloadHandler( filename = function(file) { paste('my-report.pdf', ) }, content = function(file) { pdf(file) } ) } shinyApp(ui,server) 

2 Answers 2

0

Here's a minimal example:

library(shiny) ui <- fluidPage( downloadButton("savepdf", "Save pdf") ) server <- function(input, output, session) { output$savepdf <- downloadHandler( filename = "test.pdf", content = function(file) { pdf(file) plot(iris$Sepal.Length, iris$Sepal.Width) dev.off() } ) } shinyApp(ui, server) 

Also see here.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is a minimal example with the package latexpdf. It will create the pdf table in the folder of the app.

library(shiny) library(latexpdf) dat <- head(iris, 5) ui <- fluidPage( br(), actionButton("dwnld", "Create pdf"), tableOutput("mytable") ) server <- function(input, output, session){ output[["mytable"]] <- renderTable({ dat }) observeEvent(input[["dwnld"]], { as.pdf(dat) }) } shinyApp(ui, server) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.