使用输入作为数据框名称部分下载闪亮的csv

时间:2018-06-15 11:53:49

标签: r shiny

现有data.frames被称为:active_acc_newaccactive_acc_oldaccinactive_acc_newaccinactive_acc_oldacc。它们都包含很多行和列。我使用Shiny创建了一个代码,用户可以选择单选按钮,如果他们想要下载活动或非活动以及旧的或新的。当他们点击Download时,需要下载csv文件。 csv应该是数据帧的内容,例如用户选择Active和new,然后名为active_acc_newacc的数据框应该作为csv下载,另一个例子是如果他们选择Inactive和old,那么数据框称为{ {1}}数据框应以csv。

的形式下载

在此示例中,inactive_acc_oldacc包含此示例数据:

active_acc_newacc

我在ui.R中有以下内容,请求输入:

structure(list(Alpha = c(0.192491906485068, -1.44670180633351, 
-0.323180534047634, 1.62229611652493, -0.689024123596357, 2.04212222261495, 
0.94377911190294, 2.0819268787991, 1.91711727878331, -0.414812239592928
), Beta = c(1.03285349943413, -1.67856959219527, 0.157549690345431, 
1.48913611644558, -0.0757895625491196, 1.27178094415894, 0.641673407672177, 
0.800761254937157, 1.86265922566283, -0.545356026768875), Gamma = c(1.52068837343838, 
-3.61004798325456, -1.35857038834863, 3.48938862108709, -3.05109504225968, 
6.5047022366346, 2.50727319977214, 5.31673927920108, 3.69096202696173, 
-1.03802874828505)), row.names = c(NA, -10L), class = "data.frame")

我创建了以下server.R

ui = fluidPage(
    titlePanel("Account classification"),
    sidebarLayout(
      sidebarPanel(
          radioButtons("account_status","Select account status", choices=c("Active","Inactive","Include both"),selected = "Active"),
          br(),
          radioButtons("account_age","Select account creation time", choices=c("old","new","Created any time"),selected = "new")

            ),   

      mainPanel(
        downloadButton('downloadData', 'Download')

       )
      )
    )

不幸的是,当我点击"下载时,它不起作用。按钮,它下载一个包含它的csv(如果选择了非活动和旧的):

""" X"

" 1"" Inactive_old”

所以只写出文字。

我修改了代码,有时下载的是将ui.R单选按钮页面下载为html。

如何编写代码以获得成功的csv下载?

1 个答案:

答案 0 :(得分:0)

这个怎么样:

Activeacc_newacc <- structure(list(Alpha = c(0.192491906485068, -1.44670180633351, 
-0.323180534047634, 1.62229611652493, -0.689024123596357, 2.04212222261495, 
0.94377911190294, 2.0819268787991, 1.91711727878331, -0.414812239592928
), Beta = c(1.03285349943413, -1.67856959219527, 0.157549690345431, 
1.48913611644558, -0.0757895625491196, 1.27178094415894, 0.641673407672177, 
0.800761254937157, 1.86265922566283, -0.545356026768875), Gamma = c(1.52068837343838, 
-3.61004798325456, -1.35857038834863, 3.48938862108709, -3.05109504225968, 
6.5047022366346, 2.50727319977214, 5.31673927920108, 3.69096202696173, 
-1.03802874828505)), row.names = c(NA, -10L), class = "data.frame")

ui = fluidPage(
  titlePanel("Account classification"),
  sidebarLayout(
    sidebarPanel(
      radioButtons("account_status","Select account status", choices=c("Active","Inactive","Include both"),selected = "Active"),
      br(),
      radioButtons("account_age","Select account creation time", choices=c("old","new","Created any time"),selected = "new")

    ),   

    mainPanel(
      downloadButton('downloadData', 'Download')

    )
  )
)


server = function(input, output) {
  datasetInput <- reactive({
    switch(input$account_status,
           "Active" = active_acc,
           "Inactive" = inactive_acc,
           "Include both" = NULL)

    switch(input$account_age,
           "old" = oldacc,
           "new" = newacc,
           "Created any time" = all)
  })

  dfname <- reactive({
    test <- paste0(input$account_status, "acc_", input$account_age, "acc", sep='')
    get(test)
  })

  output$downloadData <- downloadHandler(
    filename = function() { paste(input$dataset, '.csv', sep='') },
    content = function(file) {
      write.csv(dfname(), file)
    }
  )
}

shinyApp(ui, server)