简体   繁体   English

如何通过在R Shiny中循环读取.csv文件来动态生成dataTableOutput?

[英]How to generate dataTableOutput dynamically by reading the .csv files in a loop in R shiny?

I have a function that generates "n" dataframes and saves it in a location as csv files and the function returns the file name of the saved CSVs. 我有一个生成“ n”个数据帧并将其保存为csv文件的函数,该函数返回已保存CSV的文件名。

I wish to take those csv files, read it using read.csv() and then display it on the UI using renderUI and renderDataTable() 我希望获取这些csv文件,使用read.csv()读取它,然后使用renderUI和renderDataTable()在UI上显示它

While the code below has no syntax errors, but nothing is getting displayed on the screen. 尽管下面的代码没有语法错误,但是屏幕上没有任何显示。

Please suggest an appropriate method by which the tables generated in one part of the server.R can be used in output and display those data tables on the UI. 请提出一种适当的方法,以便在服务器的一部分中生成的表可用于输出并在UI上显示这些数据表。

The code for the function is below : 该函数的代码如下:

Function 功能

GenerateData <- function(){
   #********************************************************************
   # some sample data (originally, my data comes from an external souce)
   #--------------------------------------------------------------------
   a <- 1:10
   b<- 21:30
   c<-41:50
   sampleDat1 <- data.frame(a,b,c)
   sampleDat2<- data.frame(c,a,b,a)
   NumOfDataFrames <- 2
   #--------------------------------------------------------------------
   FilePath <- "D:/FolDerName/"
   FullPath<-WriteStatement <- NULL
   for(i in 1:NumOfDataFrames){

      FullPath[i]<-paste0(FilePath,"sampleDat",i,".csv")
      WriteStatement[i]<-paste0("write.csv(sampleDat",i,",file = '",FullPath[i],"')")
      eval(parse(text=WriteStatement[i]))
   }
   return(FullPath)

}

The UI.r UI.r

library(shiny)
shinyUI(

   fluidPage(

      # Application title

      navbarPage("Sample Data Display",
                 tabPanel("Data",
                          sidebarLayout(
                             sidebarPanel(
                                titlePanel("Sample"),
                                numericInput("sample1",label = "Some Label",value = 20),
                                numericInput("sample2",label = "Some Other Label",value = 20)
                             ),
                             mainPanel(
                                uiOutput("result")

                             )
                          )
                 )
      )
   )
)

The server.R 服务器

library(shiny)

GenerateData <- function(){
   #********************************************************************
   # already mentioned above, please copy the contents to server.R
   #--------------------------------------------------------------------


}
shinyServer(function(input, output,session) {
   dataSrc <- reactive({
      paths <- GenerateData()
      return(paths)
   })
   output$result <- renderUI({
      dataTab1<-NULL
      MyFilePath <- dataSrc()
      for (i in 1:length(MyFilePath)){
         dataTab1 <- read.csv(MyFilePath[i])
         # print(dataTab1)
         renderDataTable(dataTab1)
         dataTab1<-NULL

      }

   })
}
)

在此处输入图片说明

You can try 你可以试试

1) use list of df 1)使用df清单

GenerateData <- function(){
  #********************************************************************
  # some sample data (originally, my data comes from an external souce)
  #--------------------------------------------------------------------
  a <- 1:10
  b<- 21:30
  c<-41:50
  sampleDat1 <- data.frame(a,b,c)
  sampleDat2<- data.frame(c,a,b,a)
  NumOfDataFrames <- 2
  ls_df=list(sampleDat1,sampleDat2)
  names(ls_df)=c("sampleDat1","sampleDat2")
  #--------------------------------------------------------------------
  FilePath <- "C:\\12324\\files\\"
  FullPath=character()
  for(i in 1:length(ls_df)){
    FullPath[i]<-paste0(FilePath,names(ls_df)[i],".csv")
    write.csv(x=ls_df[[i]],file = FullPath[[i]])
  }
  return(FullPath)

}

2) Server.R( create dinamic ui and render DT in two step) 2)Server.R(分两步创建dinui ui并渲染DT)

shinyServer(function(input, output,session) {
  dataSrc <- reactive({
    paths <- GenerateData()
    return(paths)
  })
  output$result <- renderUI({
    MyFilePath <- dataSrc()
    lapply(1:length(MyFilePath),function(i)dataTableOutput(paste0('tbl',i)))
  })
  observe({
    MyFilePath <- dataSrc()
    lapply(1:length(MyFilePath),function(i) output[[paste0("tbl",i)]]<-renderDataTable(read.csv(MyFilePath[i])))

  })
}
)

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

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