简体   繁体   English

在 R shiny 中读写 csv 文件

[英]Read and write csv file in R shiny

I want to make several data sets available for download from the Shiny app.我想让几个数据集可供从 Shiny 应用程序下载。 I don't wan to display them or making any other calculations.我不想显示它们或进行任何其他计算。 Just make them downloadable.只需让它们可下载。 I've created a subdirectory called data, where I placed several csv files.我创建了一个名为 data 的子目录,其中放置了几个 csv 文件。 My current code writes just an empty csv file, and ignores the datasets.我当前的代码只写了一个空的 csv 文件,并忽略了数据集。

ui <- fluidPage( 
 selectInput(inputId = "dataset", label = "Select a dataset",
                             choices = c("",
                                         "a",
                                         "b",
                                         "c")
                             ),
br(),
                 
                 downloadButton("downloadData", "Download") )

and the server side和服务器端

server <- function( input, output, session ) {

output$sample <- renderTable( sampleData() )
  
sampleData <- reactive({
     switch(input$dataset, 
            "a"               = read.csv("/data/a.csv"),
            "b"               = read.csv("/data/a.csv"),
            "c"                = read.csv("/data/a.csv")   )

output$sample <- renderTable( sampleData() )
  
output$downloaData <- downloadHandler(
      filename = function(){
      paste("sample_dataset.csv", sep = "")
    },
    
    content = function(file) {
    write.csv(sampleData(), file, row.names = FALSE)
    })
}

Thank you!谢谢!


EDIT编辑


Here is the second version of the code:这是代码的第二个版本:

ui <- fluidPage( 
fluidRow(
                 column(6,

                 selectInput(inputId = "dataset",
                             label = "Select a sample dataset",
                             choices = c("",
                                         "a",
                                         "b",
                                         "c",
                                         "d",
                                         "e"
                             )),
                 br(),

                 downloadButton("downloadData", "Download")),

                 tableOutput('sample'),
                 )

SERVER:服务器:

server< - function( input, output, session ) { 
 output$sample <- renderTable( sampleData() )
  
  sampleData <- reactive({
        switch(input$dataset,
               "a" = read.csv("data/a.csv"),
               "b" = read.csv("data/b.csv"),
               "c" = read.csv("data/c.csv"),
               "d" = read.csv("data/d.csv"),
               "e" = read.csv("data/e.csv")
        )})
  
  output$sample <- renderTable( sampleData() )
  
  output$downloadData <- downloadHandler( 
    filename = function(){
      paste("sample_dataset.csv", sep = "")
    },
    
    content = function(file) {
      write.csv(sampleData(), file, row.names = FALSE)
    }) 
}

You are missing a few things in your code:您的代码中缺少一些内容:

1 - you are missing )} closing sampleData <- reactive 1 - 你错过了)}关闭sampleData <- reactive

2 - You have an extra / before data in read.csv 2 - 您在read.csv中有一个额外的/ before data

3 - DownloadData is missing a lower-case d in Render 3 - DownloadData在 Render 中缺少小写的 d

server <- function( input, output, session ) {

output$sample <- renderTable( sampleData() )



sampleData <- reactive({
     switch(input$dataset, 
           
            "a"               = read.csv("data/a.csv"), # you had an extra / before data
            "b"               = read.csv("data/a.csv"),
            "c"                = read.csv("data/a.csv") )}) # you were missing `)}` here

output$sample <- renderTable( sampleData() )
  
output$downloadData <- downloadHandler( #Here was DownloadData misspelled
      filename = function(){
      paste("sample_dataset.csv", sep = "")
    },
    
    content = function(file) {
    write.csv(sampleData(), file, row.names = FALSE)
    })
 }

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

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