How to download Rhandsontable output into .xlsx and .pdf?

邮差的信 提交于 2020-07-18 08:41:25

问题


I would like to download rhandsontable output object from an R Shiny app into .xlsx and .pdf format. Does anyone have any idea how to do this? For example, I want to download the tables in the results tab in the app below (the codes are copied from my other question earlier):

library(shiny)
library(rhandsontable)

ui <- navbarPage("App",
                 
                 tabPanel("Input",
                          numericInput('num_of_table', "Number of sub tabs: ", value = 1, min = 1, max = 10),
                          uiOutput("input")),
                 
                 tabPanel("Results",
                          uiOutput("results"))
                 
)

server <- function(input, output,session) {
  
  ### Input ### 
  input_table <- reactive({
    list_of_input_table = list()
    
    for (i in c(1:input$num_of_table)){
      mat <- matrix(c(1:25) * i, ncol = 5, nrow = 5)
      list_of_input_table[[i]] = as.data.frame(mat)
    }
    
    index = c(1:i)
    list_of_input_table[index]
  })
  
  observeEvent(input$num_of_table, {
    lapply(seq_len(input$num_of_table), function(i) {
      output[[paste0('input_table_', i)]] <- renderRHandsontable({
        rhandsontable(input_table()[[i]])
      })
    })
  })
  
  output$input <- renderUI({
    
    nTabs = input$num_of_table
    
    myTabs1 = lapply(seq_len(nTabs), function(x){
      tabPanel(paste("Tab", x),
               column(12,
                      rHandsontableOutput(paste0("input_table_", x))))
    })
    do.call(tabsetPanel, myTabs1)
    
  })
  
  ### Results ###
  results_table <- reactive({
    
    list_of_results_table = list()
    for (i in c(1:input$num_of_table)){
      req(input[[paste0("input_table_", i)]])
      list_of_results_table[[i]] <- hot_to_r(input[[paste0("input_table_", i)]])[2:5]/hot_to_r(input[[paste0("input_table_", i)]])[1:4]
      }
    return(list_of_results_table)
  })
  
  
  observeEvent(input$num_of_table, {
    lapply(seq_len(input$num_of_table), function(i) {
      output[[paste0('results_table_', i)]] <- renderRHandsontable({
        
        rhandsontable(results_table()[[i]])
      })
    })
  })
  
  output$results <- renderUI({
    
    nTabs = input$num_of_table
    
    myTabs2 = lapply(seq_len(nTabs), function(x){
      tabPanel(paste("Tab", x),
               column(12,
                      rHandsontableOutput(paste0("results_table_", x))))
    })
    do.call(tabsetPanel, myTabs2)
    
  })
}


shinyApp(ui,server)

Appreciate any help! Thanks!

来源:https://stackoverflow.com/questions/62872777/how-to-download-rhandsontable-output-into-xlsx-and-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!