【问题标题】:Exporting tables to csv- separating tables in a list- shiny将表导出到 csv- 列表中的分隔表-闪亮
【发布时间】:2015-07-07 07:44:47
【问题描述】:

我有一个表格列表 (df_list)。我想以闪亮的方式将它们导出到 csv。将表列表导出到 csv 时,我在 excel 中打开它们。在 excel 中,表列表被放入列中,并且列由 tablename.colname 引用。我希望以 tble 名称为标题的表具有单独的 col 名称,如下所示。

 Cy3 control
          Cy5     Cy3    Cy5   Cy3
    Min.    0   14170   6043    15
    1st Qu. 1   14710   6329    16
    Median  1   14960   6833    16
    Mean    3.2 15190   6679    16.47
    3rd Qu. 4.5 15830   7008    17
    Max.    15  16070   7309    19

    Cy3.control.Cy5 Cy3.control.Cy3 Cy5.control.Cy5 Cy5.control.Cy3
Min.              0             170        6043              15
1st Qu.           1           14710        6329              16
Median            1           14960        6833              16
Mean            3.2           15190        6679           16.47
3rd Qu.         4.5           15830        7008              17
Max.             15           16070        7309              19

是否还可以在上方添加一个单元格,该单元格提供有关表格的信息,例如 - 实验名称等。它不与其他单元格交互。

我用来生成文件的代码如下:

UI.R:

shinyUI(fluidPage(
  fluidRow(
    column(4,
             fileInput("rawdata", "Enter your .csv file"),
             br,
             textInput('table_name', 'Data table name to save'),
             downloadButton('downloadtable', 'Save data table to .csv')
             ),

  ) #end fluidrow
 )  ### Fluid page end 
)    #### Shiny UI end

Server:


  #### Initiate shinyServer
  shinyServer(function(input, output) {

    ### This reactive function will take the input "inFile" from the UI.R and store it as data
    inFile<-reactive({
      file1<-input$rawdata
      if(is.null(file1)) stop ("Please Upload a Valid .csv file")
      datas<-fread(file1$datapath,)


      dtableInput<- reactive({
        if(is.null(inFile())) 
          return()

        datas<-inFile()        

        ## apply the following functions over the list

        df_list<-lapply(df_list,function(x){

          ## store the summaries of the Cy5 and Cy3 by block

          Cy5<-summary(x$y1)

          Cy3<-summary(x$y2)

          cbind(y1,y2)    

        })


      })

      output$downloadtable<-downloadHandler(                             #### called from UI
        filename = function() {paste(input$table_name, '.csv', sep='')},
        content = function (file){
          write.csv(dtableInput(),file)
        })

    }) ## end of app

【问题讨论】:

  • 仅凭那段代码有点难以回答。我会注意几点: - 1. 你只是定义一个函数来编写一个 csv,而不是在我能看到的任何地方调用它。所以什么都不会发生。 - 2. 尝试编写一个非常小的示例,在一个闪亮的小程序中编写一个玩具 csv 并将其发布。然后我们可以提供更好的帮助。
  • 是的,你是对的,代码没有意义我已经编辑为一个工作示例。谢谢迈克
  • 好的,让我看看。

标签: r csv shiny export-to-csv


【解决方案1】:

好的,花了一些时间,因为我以前从未使用过 Shiny 的 fileInputdownLoad 按钮。基本上我在下面发布的内容可能接近您想要的内容。它允许您选择要上传的文件,然后选择下载位置。

我没有建立你的汇总表,我认为你可以自己处理它,它会混淆文件处理问题。

一个警告是它在普通的 RStudio 查看器中不起作用(可能是一个错误?它不保存文件),您必须使用浏览器(单击 Run App 按钮旁边的下拉菜单并选择 Run External (见下面的截图)

ui.R:

shinyUI(fluidPage(
  fluidRow(
     column(4,
            fileInput("rawdata", "Enter your .csv file"),
            br(),
            textInput('table_name', 'Data table name to save'),
            downloadButton('downloadtable', 'Save data table to .csv')
      )

  ) #end fluidrow
)  ### Fluid page end 
)    #### Shiny UI end

服务器.R:

#### Initiate shinyServer
shinyServer(function(input, output) {


  inFile<-reactive({
    file1<-input$rawdata
    if(is.null(file1)) stop ("Please Upload a Valid .csv file")
    datas<-read.csv(file=file1$datapath)
  })

  dtableInput<- reactive({
    if(is.null(inFile())) 
      return()
    datas<-inFile()        
  })

    output$downloadtable<-downloadHandler(        #### called from UI
      filename = function() {paste(input$table_name, '.csv', sep='')},
      content = function(file) {
        write.csv(dtableInput(), file)
      }
    )
  })

【讨论】:

  • 嗨,马克是的,这确实适用于上传文件。这是我关心的下载。例如 file.choose() 的下载版本。除了您的代码中的 read.csv 被我的 fread 替换之外,我看不到直接的功能差异。有什么想法吗?哦,是的,我应该说,与“外部运行”相关的错误是一种痛苦....
  • 嗯,它适用于一件事。我不得不重新排列你的代码来解决这个问题。你有很多嵌套的东西,它不会编译。 ui.R 也不会编译。这会同时进行上传和下载,所以我不知道您要的是什么。
  • 是的,谢谢迈克。我已经意识到了这个问题。是的,它确实在外部工作。我正在通过有问题的浏览器运行我的浏览器。这是chrome现在的问题。感谢您的帮助。
猜你喜欢
  • 2017-07-20
  • 1970-01-01
  • 1970-01-01
  • 2018-06-26
  • 2019-11-09
  • 2015-08-05
  • 2018-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多