R 下载反应性数据帧/数据表问题

R 下载反应性数据帧/数据表问题,r,shiny,download,handler,reactive,R,Shiny,Download,Handler,Reactive,我是R新手,但我的任务是创建一个工作下载按钮,将查询表中的数据下载到.csv文件中。目前,我很难理解应该传递给write.csv()函数的内容。我知道我必须使查询的表成为被动的,但我也不确定如何做到这一点。我当前代码的错误是“ 无法将类“c”(“datatables”、“htmlwidget”)”强制为data.frame 我尝试将df()函数作为参数传递,但它不起作用 #ui column(3, conditionalPanel("input

我是R新手,但我的任务是创建一个工作下载按钮,将查询表中的数据下载到.csv文件中。目前,我很难理解应该传递给write.csv()函数的内容。我知道我必须使查询的表成为被动的,但我也不确定如何做到这一点。我当前代码的错误是“ 无法将类“c”(“datatables”、“htmlwidget”)”强制为data.frame

我尝试将df()函数作为参数传递,但它不起作用

#ui
column(3, 
                         conditionalPanel("input.table.period != '1980'", 
                                          # only prompt for rcp if a future period (not historical)
                                          # input: select rcp
                                          selectInput("table.rcp", "Emissions Scenario:", 
                                                      c("Medium" = "45",
                                                        "High" = "85")),
                                          downloadButton('downloadData', 'Download')
                                          )


#server  
server <- function(input, output) {
currentdf <- reactive({


# build query based on user-selections
if (input$table.stype == "ann") {
  col.name <- columns.annual[grep(input$table.var, columns.annual)]
  if (input$table.period == "1980") {
    query <- paste0("SELECT ", col.name, ", subbasin, gcm_id FROM hydro_ann WHERE (period = ", input$table.period,
                    ")")
  } else {
    query <- paste0("SELECT ", col.name, ", subbasin, gcm_id FROM hydro_ann WHERE (period = ", input$table.period,
                    ") AND (rcp = ", input$table.rcp, ")")
  }
} else {
  col.name <- columns.month[grep(input$table.var, columns.month)]
  if (input$table.period == "1980") {
    query <- paste0("SELECT ", col.name, ", subbasin, gcm_id, calendar_month FROM hydro_month WHERE (period = ", input$table.period,
                    ")")
  } else {
    query <- paste0("SELECT ", col.name, ", subbasin, gcm_id, calendar_month FROM hydro_month WHERE (period = ", input$table.period,
                    ") AND (rcp = ", input$table.rcp, ")")
  }
}


df <- dbGetQuery(db, query)

DT::df


  })
  output$querytable <- renderTable({ DT::datatable(currentdf()) })
  output$downloadData <- downloadHandler(
filename = function() {
  paste("QueriedData", "csv", sep = ".")
},
content = function(file) {
  write.csv(as.data.frame(currentdf()),file)
}
#用户界面
第(3)栏,
conditionalPanel(“input.table.period!=“1980”,
#仅在未来期间(非历史期间)提示rcp
#输入:选择rcp
选择输入(“table.rcp”,“排放情景:”,
c(“中等”=“45”,
“高”=“85”)),
downloadButton('下载数据','下载')
)
#服务器

服务器我尝试了一个解决方案宽度Iris数据集,它可以让您选择要保存的数据类型,然后再根据您选择的选项下载数据

library(shiny)
library(shinydashboard)
library(DT)
library(datasets)
library(xlsx)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    # Boxes need to be put in a row (or column)
    fluidRow(
      box(
        DTOutput("dtable")
          )
    )
  )
)

server <- function(input, output) {
  myModal <- function() {
    div(id = "Download_DATA",
        modalDialog(downloadButton("download1","Download iris as CSV"),
                    br(),
                    br(),
                    downloadButton("download2","Download iris as XLSX"),
                    easyClose = TRUE, title = "Download Table")
    )
  }

  output$dtable <- renderDT(
    datatable(iris,
              extensions = 'Buttons',
              options = list(
                dom = 'Bfrtip',
                buttons = list(
                  list(
                    extend = "collection",
                    text = 'Download',
                    action = DT::JS("function ( e, dt, node, config ) {
                                    Shiny.setInputValue('Download_DATA', true, {priority: 'event'});
}")
                  )
                    )
                  )
                )
              )

  observeEvent(input$Download_DATA, {
    showModal(myModal())
  })


  output$download1 <- downloadHandler(
    filename = function() {
      paste("data-", Sys.Date(), ".csv", sep="")
    },
    content = function(file) {
      write.csv(iris, file)
    }
  )

  output$download2 <- downloadHandler(
    filename = function() {
      paste("data-", Sys.Date(), ".xlsx", sep="")
    },
    content = function(file) {
      write.xlsx(iris, file)
    }
  )

}

shinyApp(ui, server)
库(闪亮)
图书馆(shinydashboard)
图书馆(DT)
图书馆(数据集)
图书馆(xlsx)
用户界面