从Shiny将数据表导出到csv时,如何使用反应性数据集?

时间:2019-01-14 15:23:34

标签: r shiny dt

我正在尝试在我的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)
    }
  )
}

下载按钮只是打印整个页面的HTML版本并返回错误:

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

1 个答案:

答案 0 :(得分:0)

filename函数之后只是缺少逗号。