R Shiny中excelR包生成的可编辑表格如何下载导出
报错原因
你调用的core_df()是excelTable()生成的jexcel类html组件对象,无法被强制转换为数据框,因此触发类型转换报错。
excelR的可编辑表格数据会自动同步到shiny的input对象中,对应取值ID为你定义的excelOutput的ID,即input$template2。
修复后可运行代码
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){ # 提取用户编辑后的表格数据,转为数据框 edit_data <- data.frame(input$template2) # 对齐列名 colnames(edit_data) <- core_columns()$title write.csv(edit_data, file, row.names = FALSE) # 若仅需下载空白模板,可替换为下方代码 # write.csv(core_data(), file, row.names = FALSE) } ) } shinyApp(ui, server)
内容的提问来源于stack exchange,提问作者Subaru Spirit
相关产品推荐
相关产品推荐

