闪亮应用中多个文件的下载按钮

时间:2019-06-05 02:00:24

标签: r shiny

我正在构建一个Shiny应用程序,并且试图添加一个下载按钮来下载csv文件(从数据库中获取的表)。基本上我有一个带有4个值的checkboxgroupInput,每个值对应一个不同的表,并且按钮应该下载被“选中”的表是单个zip文件。

我尝试了不同版本的代码,即使它没有引发任何错误,当我运行该应用程序时,单击下载按钮时,它也会尝试下载不存在的文件。我认为for永远不会执行,但是我不明白为什么。所以我的想法是用ifelse填充selectedCountry,然后将每个元素都写为csv并使用csv创建ziip。我试图下载单个表,所以ITALY_DB等不为空或为空,它们可以正常工作。

server.R
Italy_tbl <- data.frame(ITALY_DB())
ne_tbl <- data.frame(NE_DB())
turkey_tbl <- data.frame(TURKEY_DB())
usa_tbl <- data.frame(USA_DB())
selectedCountry<- vector()

observeEvent(input$country,{
  ifelse (input$country == "Italy",selectedCountry <- 
  C(selectedCountry,Italy_tbl), selectedCountry<-selectedCountry)

  ifelse (input$country == "Turkey",selectedCountry <- 
  C(selectedCountry,turkey_tbl),selectedCountry<-selectedCountry)

  ifelse (input$country == "Netherlands",selectedCountry <-
  C(selectedCountry,ne_tbl ),selectedCountry<-selectedCountry)

  ifelse (input$country == "USA",selectedCountry <- 
  C(selectedCountry,usa_tbl),selectedCountry<-selectedCountry)

  })

output$download <- downloadHandler(


filename = function() {
  paste0(input$country,".zip")

  #paste(input$country, ".csv", sep = "")
},


content = function(file) {


  #go to a temp dir to avoid permission issues
  owd <- setwd(tempdir())
  print(tempdir())
  on.exit(setwd(owd))
  files <- NULL;

  for(i in selectedCountry){

    fileName <- paste(input$country,"_0",".csv",sep = "")
    write.csv(selectedCountry[i],fileName,sep = ';', row.names = F, 
    col.names = T)
    files <- c(fileName,files)


    }
   zip(file,files)


  }
)




  ui.R

  column(3,inputPanel( 
  sliderInput('yearSlider', 'Select Years', sep="",
  min=2009, max=2018, step=1, value =c(2009,2018)),


  (checkboxGroupInput("country",h4('Select Countries'),
  choices = list("Italy" = 1,
                 "Netherlands" = 2,                                                                   
                 "Turkey" = 3,
                 "USA" = 4),
                selected = 1)),
  radioButtons(
               inputId = 'plotCount'
               , label   = 'Plot Count'
               , choices = as.character(1:2)
               , inline=T
                ),
               downloadButton("download", "Download Data")
     ))

我敢肯定我缺少什么,但我只是不明白什么

0 个答案:

没有答案