【问题标题】:How to create Shiny R dynamic renderTable, with a number of tables determined by the uploaded CSV files?如何使用上传的 CSV 文件确定的表数量创建 Shiny R 动态渲染表?
【发布时间】:2016-06-25 03:41:28
【问题描述】:

我正在构建 web 应用程序,在上传 csv 文件后转换数据,然后应该能够输出几个表。表的数量严格取决于 csv 文件中包含的信息,因此是在数据转换过程中计算的。我创建了一个列表lst,其中包含必须输出的数据帧。列表的长度是应该创建的表的数量。在网上搜索后,我遇到了非常相似的问题(here),遗憾的是还没有回答。有谁知道如何解决它?

我的一些代码(不是全部,由于重要的数据转换),我想将固定的max_table 替换为变量length(data_set())

library(shiny)

ui <- fluidPage(
  fluidRow(column(3,
                  wellPanel(
                    fileInput(inputId = "files",
                              label = "Choose cvs files",
                              accept=c('text/csv', 
                                       'text/comma-separated-values,text/plain', 
                                       '.csv'),
                              multiple = TRUE))),
           column(5, offset = 1, 
                  uiOutput("tables")
                  )
           )
  )

max_table <- 5
server <- function(input,output){


  data_set <- reactive({
    if(is.null(input$files)){
      return(NULL)
    }

    lst <- list()
    for(i in 1:length(input$files[,1])){
      lst[[i]] <- read.csv(input$files[[i, 'datapath']], sep = ",", header = TRUE, skip = 4, dec = ".")
    }
    lst
  })

  output$tables <- renderUI({
    plot_output_list <- lapply(1:max_table, function(i) {
      tablename <- paste("tablename", i, sep="")
      tableOutput(tablename)
    })
    do.call(tagList, plot_output_list)
  })


  for (i in 1:max_table){
    local({
      my_i <- i
      tablename <- paste("tablename", my_i, sep="")
      output[[tablename]] <- renderTable({data_set()[[my_i]] 
      })
    })    
  }
}

shinyApp(ui = ui, server = server)

任何帮助将不胜感激!

【问题讨论】:

    标签: r csv shiny


    【解决方案1】:

    解决方案是将所有内容放在observe 中。

    library(shiny)
    
    ui <- fluidPage(
      fluidRow(column(3,
                      wellPanel(
                        fileInput(
                          inputId = "files",
                          label = "Choose cvs files",
                          accept = c('text/csv',
                                     'text/comma-separated-values,text/plain',
                                     '.csv'),
                          multiple = TRUE
                        )
                      )),
               column(5, offset = 1, uiOutput("tables"))))
    
    server <- function(input, output) {
      observe({
        if (!is.null(input$files)) {
          max_table = length(input$files[, 1])
    
          lst <- list()
          for (i in 1:length(input$files[, 1])) {
            lst[[i]] <-
              read.csv(
                input$files[[i, 'datapath']],
                sep = ",",
                header = TRUE,
                skip = 4,
                dec = "."
              )
          }
    
          output$tables <- renderUI({
            plot_output_list <- lapply(1:max_table, function(i) {
              tablename <- paste("tablename", i, sep = "")
              tableOutput(tablename)
            })
            do.call(tagList, plot_output_list)
          })
    
          for (i in 1:max_table) {
            local({
              my_i <- i
              tablename <- paste("tablename", my_i, sep = "")
              output[[tablename]] <- renderTable({
                lst[[my_i]]
              })
            })
          }
        }
      })
    
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-05
      • 2015-11-06
      • 1970-01-01
      • 2017-04-17
      • 2019-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多