R shiny,excelR包怎么下载table/dataframe?

R shiny, excelR package how to download the table/dataframe?

如何从 excelR 包下载 table?我想使用 excelR 包的原因是用户可以直接输入值,但如果要添加更多而不是几行,我想给他们下载列表的选项。

请参阅下面的最小可重现代码。当 运行 下面的代码并单击下载按钮时,它会给我一条错误消息 Warning: Error in as.data.frame.default: cannot coerce class ‘c("jexcel", "htmlwidget")’ to a data.frame [No stack trace available]

library(shiny)
library(excelR)

ui <- fluidPage(
  fluidRow(
    column(6,
           titlePanel("Template")
    ),
    column(2,
           downloadButton("download1", "Download Template")
    )
  ),
  fluidRow(
    excelOutput("template2"))
)

server <- function(input, output, session) {
  core_data = reactive({
    data.frame(Session_Name = rep("",10),
               Description1="", Description2="",
               Weeks_per_year="",
               Patients_per_clinic="")
  })
  
  core_columns = reactive({
    data.frame(title=c('Session_Name','Description1',
                       'Description2',"Weeks_per_year",
                       "Patients_per_clinic"),
               width= c(300,300,100,100,100),
               type=c('text','text','text',"numeric","numeric")) 
  })
  
  core_df = reactive({
    excelTable(data=core_data(), columns = core_columns())
  })
  
  output$template2 <- renderExcel({
    core_df()
  })
  
  output$download1 <- downloadHandler(
    filename = function(){"user_template.csv"}, 
    content = function(file){
      write.csv(core_df(), file,row.names = FALSE)
    }
  )
  
}

shinyApp(ui, server)

问题是 core_df() 不是数据框,而是 class excelObj 的对象。改编 ?excel_to_R 中的示例代码,您可以将模板转换为 R 数据框并将其导出到 css,如下所示:

output$download1 <- downloadHandler(
    filename = function() {
      "user_template.csv"
    },
    content = function(file) {
      write.csv(excel_to_R(input$template2), file, row.names = FALSE)
    }
  )