【问题标题】:How to use a reactive dataset while exporting datatable to csv from Shiny?如何在将数据表从 Shiny 导出到 csv 时使用反应式数据集?
【发布时间】:2019-01-14 15:23:33
【问题描述】:

我正在尝试在我的 Shiny 应用程序中将反应式数据表导出到 csv,但似乎遇到了错误,即应用程序在下载时找不到创建反应式数据集的函数,但似乎在应用程序。

我的代码如下:

ui <- 
  fluidRow(
    column(3, style = "padding:10px",
           downloadButton("downloadData", "Export current table to Excel")
    )
  ),
DT::dataTableOutput("customertable")

server <- function(input,output,session){
  dataExpTable <- reactive({
    all_statesClean %>%
      filter(
        is.null(input$states) | ship_to_region %in% input$states,
        is.null(input$counties) | county %in% input$counties,
        as.numeric(gsub(",", "", avg_opioid)) >= input$custminopVol,
        as.numeric(gsub(",", "", avg_opioid_perc)) >= input$custminopp,
        as.numeric(gsub(",", "", avg_oxy_hydro_perc)) >= -1,
        as.numeric(gsub(",", "", avg_oxy_hydro)) >= 0,
        c_avg_opioid >= input$minopVol,
        c_avg_opioid_perc >= input$minopp,
        c_avg_opioid_ppp >= input$minopppp,
        c_avg_oxy_hydro_perc >= -1,
        c_avg_oxy_hydro >= 0,
        c_avg_oxy_hydro_ppp >= 0
      )
  })

  output$customertable <- DT::renderDataTable({
    DT::datatable(
      dataExpTable(),
      escape = FALSE
    )
  })

  output$downloadData <- downloadHandler(

    filename = function(){
      paste(gsub("-", ".", Sys.Date()), " - ", "County level"," data",".csv", sep = "")
    }

    content = function(file){
      write.csv(dataExpTable(),file, row.names = FALSE)
    }
  )
}

下载按钮并没有从中获取 csv 文件,而是仅打印整个页面的 HTML 版本并返回错误:

Error in is.data.frame(x) : could not find function "dataExpTable"

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    只是在filename 函数后面少了一个逗号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-20
      • 2018-07-23
      • 2014-09-20
      • 2015-10-18
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      相关资源
      最近更新 更多