rbind module elements only upon fileInput in R Shiny

gajydyqb  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(89)

我正在开发一个应用程序,用户在ui模块中输入几组文件,在server模块中进行一些数据整理以产生data.frame,然后将这些data. frame的结果保存在主server中并粉碎在一起,形成一个带有rbind()的 Dataframe (是的,data.frames是相同格式,具有相同名称)。
问题是,我似乎不能让Shiny识别什么时候没有fileInput()发生,只有rbind()的数据.frames在 * 那里 *,也就是说,当文件已经上传。我尝试使用is.null()语句,但它不起作用。
下面是我的ui模块:

readCSV_UI <- function(id){
  tagList(
    fileInput(NS(id, "site"),
              "Upload .csv files",
              accept = "csv",
              multiple = T,
              width = "85%")
  )  
}

字符串
这是我的server模块

readCSV_Server <- function(id){
  
  moduleServer(id, function(input, output, session){
  
    files <- reactive(req(input$site))
    csv <- reactive(map(files, read.csv))
    return(list(csv))
  })
}


这是一个闪亮的应用程序:

library(shiny)
library(purrr)

ui <- fluidPage(
 readCSV_UI("a"),
 readCSV_UI("b"),
 readCSV_UI("c"),
 tableOutput("check")
)

server <- function(input, output, session) {

b1 <- readCSV_Server("a")[[1]]
b2 <- readCSV_Server("b")[[1]]
b3 <- readCSV_Server("c")[[1]]

bbind <- reactive(rbind(b1(), !is.null(b2()), !is.null(b3()))) # here is where I tried to recognize it only when files are uploaded

output$check <- renderTable(bbind())

}


上面的代码没有!is.null()语句,但只有当每个fileInput都发生了文件上传时才能工作。我如何只rbind()有数据的文件?

j2cgzkjk

j2cgzkjk1#

您可以使用req()来确保在显示之前加载数据。
试试这个

readCSV_UI <- function(id){
  ns <- NS(id)
  tagList(
    fileInput(ns("site"),
              "Upload .csv files",
              accept = "csv",
              multiple = T,
              width = "85%")
  )
}

readCSV_Server <- function(id){

  moduleServer(id, function(input, output, session){

    mydata <- reactive({
      req(input$site)
      n <- dim(input$site)[1]
     
      lapply(1:n, function(i){
        File <- input$site$datapath[i]
        df <- read.csv(as.character(File), header = TRUE, sep=",")
        if (i==1) all_df <<- df
        else all_df <<- rbind(all_df,df)
      })
     
      return(all_df)
    })

    return(mydata)
  })
}

library(shiny)
library(purrr)

ui <- fluidPage(
  readCSV_UI("a"),
  readCSV_UI("b"),
  readCSV_UI("c"),
  tableOutput("check")
)

server <- function(input, output, session) {

  b1 <- readCSV_Server("a")
  b2 <- readCSV_Server("b")
  b3 <- readCSV_Server("c")

  bbind <- reactive({
    req(b1(),b2(),b3())
    rbind(b1(),b2(), b3())
  }) # here is where I tried to recognize it only when files are uploaded

  output$check <- renderTable(bbind())

}

shinyApp(ui = ui, server = server)

字符串

相关问题