How to create Shiny R dynamic renderTable, with a number of tables determined by the uploaded CSV files?

前端 未结 1 1716
[愿得一人]
[愿得一人] 2020-12-18 14:49

I am building web app that after uploading csv files transforms the data and then should be able to output few tables. The number of the tables depends strictly on the infor

相关标签:
1条回答
  • 2020-12-18 15:32

    A solution is to put everything inside an 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)
    
    0 讨论(0)
提交回复
热议问题