【问题标题】:How to create a button that will create a pdf file of a table如何创建一个按钮来创建一个表格的pdf文件
【发布时间】:2021-10-19 23:04:38
【问题描述】:

我目前正在生成一个表格,我希望用户能够在单击下载按钮时创建一个 pdf 文件。

我目前遇到一个错误,当我单击下载按钮时,我得到一个下载应用程序整个页面的 html 文件。我认为使用pdf(file) 会起作用,但它忽略了该功能。

这是我目前拥有的。

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)

【问题讨论】:

    标签: r shiny download dashboard


    【解决方案1】:

    这是一个最小的例子:

    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)
    

    另见here

    【讨论】:

      【解决方案2】:

      这是一个带有 latexpdf 包的最小示例。它将在应用程序的文件夹中创建 pdf 表。

      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)
      

      【讨论】:

        猜你喜欢
        • 2017-07-31
        • 1970-01-01
        • 2023-03-24
        • 1970-01-01
        • 2019-09-02
        • 1970-01-01
        • 1970-01-01
        • 2021-12-05
        • 2018-06-27
        相关资源
        最近更新 更多