简体   繁体   English

R Shiny - 反应数据帧下载到 csv

[英]R Shiny - Reactive dataframe download to csv

I have produced an app that allows the user to upload multiple csv files.我制作了一个应用程序,允许用户上传多个 csv 文件。

These csvs then 'rbind' together, 'read.csv' and have a column added to the df which is the filename.然后这些 csvs 'rbind' 在一起,'read.csv' 并有一个列添加到 df 中,这是文件名。

The df is then processed to produce various plots which are downloadable.然后处理 df 以生成可下载的各种图。 This works perfectly.这完美地工作。 However when I try download a specific data frame from within the reactive element I can't get it to work.但是,当我尝试从反应元素中下载特定数据框时,我无法让它工作。

In this example I want to download df1 from within the testinput reactive function.在这个例子中,我想从 testinput 反应函数中下载df1

UI:用户界面:

    dashboardPage( skin = "black",
   dashboardHeader(title = "myApp"),
   dashboardSidebar(collapsed = TRUE,
   sidebarMenu(
    menuItem("Home", tabName = "dashboard1", icon = icon("home", lib = 
  "glyphicon"))
    ) 
    ),
   dashboardBody(
     tags$head(tags$style(HTML('
      .main-header .logo {
                          font-family: "Times New Roman", serif;
                          font-weight: bold;
                          font-size: 24px;
                          }
                          '))),

tabItems(
  tabItem(tabName = "dashboard1",
          fileInput("file1",
                    label="Input files:",
                    multiple = TRUE),
          downloadButton('downloadData', 'Download Data')
  )
  )

)
)

Server:服务器:

     library(shiny)
     library(shinydashboard)

     #server start
     function(input, output) {

       testinput<- reactive({
if(is.null(input$file1))
  return ()
else 
{
  nfiles = nrow(input$file1) 
  csv = list()
  for (i in 1 : nfiles)
  {

    csv[[i]] = read.csv(input$file1$datapath[i])

  }

  csv_names <- input$file1[['name']]
    actual_names<-input$statics$name
  df1<-cbind(actual_names, csv_names)
  mydata <- do.call(rbind, lapply(csv_names, function(x) cbind(read.csv(x), name=strsplit(x,'\\.')[[1]][1])))
    df1<-cbind(df1, mydata)    
  }
   })

     output$downloadData <- downloadHandler(
filename = function() { paste(input$downloadData, " ",Sys.Date(),".csv",sep="") },
content = function(file) {
  write.csv(df1,file)
}
    )

 )


   }

Any help would be great as I have searched lots of SO and other forums and I'm pretty stuck.任何帮助都会很棒,因为我已经搜索了很多 SO 和其他论坛,但我很困惑。

@Chabo was correct: @Chabo 是正确的:

I had to create a new reactive() and then render as an output$ to my UI.我必须创建一个新的reactive(),然后将其作为output$ 呈现到我的UI 中。

I could then download reactive event然后我可以下载反应事件

I think you can return the variable you wish to download in the reactive function and then add a downloaddata in the server function like this:我认为您可以在响应式函数中返回您希望下载的变量,然后在服务器函数中添加一个 downloaddata,如下所示:

library(shiny)
library(shinydashboard)

#server start
function(input, output) {

  testinput<- reactive({
    if(is.null(input$file1)){
        return ()
    }
    else{
        nfiles = nrow(input$file1) 
        csv = list()
    }

    for (i in 1 : nfiles){

        csv[[i]] = read.csv(input$file1$datapath[i])

    }

    csv_names <- input$file1[['name']]
    actual_names<-input$statics$name
    df1<-cbind(actual_names, csv_names)
    mydata <- do.call(rbind, lapply(csv_names, function(x) cbind(read.csv(x), name=strsplit(x,'\\.')[[1]][1])))
    df1<-cbind(df1, mydata)  
    return(df1)
  })

  output$downloadData <- downloadHandler(
    filename = function() {
      paste(input$dataset, ".csv", sep = "")
    },
    content = function(file) {
      write.csv(testinput(), file, row.names = FALSE)
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM