1

如何从 excelR 包中下载表格?我想使用 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)
4

1 回答 1

2

问题在于它core_df()不是数据框,而是类对象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)
    }
  )
于 2021-06-30T11:32:32.590 回答