如何创建Shiny R动态renderTable,有一些由上传的CSV文件确定的表格? [英] How to create Shiny R dynamic renderTable, with a number of tables determined by the uploaded CSV files?

查看:1033
本文介绍了如何创建Shiny R动态renderTable,有一些由上传的CSV文件确定的表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在构建web应用程序,上传csv文件后,转换数据,然后应该能够输出少数表。表的数量严格取决于csv文件中包含的信息,因此在数据转换过程中计算。我已经创建了一个列表 lst 与必须输出的数据帧。列表的长度是应该创建的表的编号。搜索网络后,我遇到了非常类似的问题(此处),这是不幸的还没有回答。

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 information included in the csv files, therefore is calculated during the data transformation process. I have created a list lst with data frames that have to be outputted. The length of the list is the number of the tables that should be created. After searching the web I encountered very similar question (here), which is unfortunately not answered yet. Does anyone has an idea how to solve it?

我的一些代码(不是全部,由于重要的数据转换),其中我想替换固定的 max_table 与变量长度(data_set())

Some of my code (not all, due to significant data transformation), where I would like to replace fixed max_table with variable 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)

任何帮助将非常感谢!

Any help would be much appreciated!

推荐答案

解决方案是将所有内容放在观察 p>

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)

这篇关于如何创建Shiny R动态renderTable,有一些由上传的CSV文件确定的表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆