R shiny 在反应式中迭代多个输入

R shiny iterating over multiple inputs in a reactive

下面是该问题的一些可重现代码。但基本上我有一个应用程序,可以有许多结构相似的用户输入文件。我想要一个 reactive() 函数,它将遍历所有输入并将数据附加到 return。参见问题代码第 78 行的函数 ReadData()。我可以遍历所有文件,因为我已经使 id 与增量整数相同,例如1 input id = File1, 2 input id = File2 等

library('shiny')
library('shinythemes')

## adding the conditional statements
ui = 
  navbarPage("Page Title",
         tabPanel("Panel 1",
                  sidebarPanel(
                    ## Add Name,
                    ## Number of surveys analysising
                    numericInput("n_values", "Number of columns in next panel:", 1, min = 1, max = 3)
                  ),
                  mainPanel(
                    tags$div(
                      h2("Home Page") 
                    )
                  )
         ),
         tabPanel("Panel 2",
                                   fixedPage(theme = "flatly",
                                             fixedRow(
                                               column(2,"First Column",
                                                      fileInput("File1", "Choose a CSV files", multiple = F),
                                                      actionButton("CheckData", "Validate Input"),                                                          
                                                      p("Click the button to check the data was read in correctly")
                                               ),
                                               conditionalPanel(condition = "input.n_values >= 2",
                                                                column(2,"Second Column",
                                                                       fileInput("File2", "Choose a CSV files", multiple = F),
                                                                       p("Click the button to check the data was read in correctly")
                                                                )
                                               ),
                                               conditionalPanel(condition = "input.n_values >= 3",
                                                                column(2,"Second Column",
                                                                       fileInput("File3", "Choose a CSV files", multiple = F),
                                                                       p("Click the button to check the data was read in correctly")
                                                                )
                                               )                                                   
                                             ),
                                             fixedRow(
                                               column(12,
                                                      verbatimTextOutput("errorText2")
                                               )
                                             )    
                                   )

         )      
)  


server = function(input, output,session) {
  ## Call the error message function and print

  output$errorText2 <- renderText({
    validate(
      if (input$n_values == 1) {
        need(!is.null(input$File1)
             , 'You need to input the files before we can validate the data. Please select all the necessary files.')           
      } else if (input$n_values == 2) {
        need(!is.null(input$File1) & !is.null(input$File2)
             , 'You need to input the files before we can validate the data. Please select all the necessary files.')           
      } else if (input$n_values == 3) {
        need(!is.null(input$File1) & !is.null(input$File2) & !is.null(input$File3)
             , 'You need to input the files before we can validate the data. Please select all the necessary files.')           
      }
    )
    validate(ReadDataAndCheck())
  })      

  ## read in an user defined number of inputs and append them together
  ReadData <- reactive({
    print("enter: CheckData")
    temp_df = NULL;
    for (i in 1:input$n_values) {
      input_name = Paste("File", i)
      print(names(input))
      print(input_name)
      File <- get(input_name, input)  ## get an error
      ## File <- eval(expr = text(x = paste0("input$",input_name)))  ## also tried but get error cannot find object 'input'

      if (!is.null(File)) {
        this_df = read.csv(File$datapath, header = T,stringsAsFactors = F);  
        if (i == 1) {
          temp_df = this_df
        } else {
          temp_df = rbind(temp_df, this_df);
        }                      
      }
    }
    if (!is.null(temp_df)) {
      return(temp_df)
    } 
  }) 

  ReadDataAndCheck <- eventReactive(input$CheckData, {  
    print("enter: Validating data")
    Data = ReadData();
    if(is.null(Data)) {
      return("error couldn't read data")
    }
    return("successfully read in data")
  }) 
}
shinyApp(ui, server)

提前感谢您的任何建议

您快完成了,只需将行 File <- get(input_name, input) 更改为 File <- input[[input_name]]

这是适合我的工作版本

library('shiny')
library('shinythemes')

## adding the conditional statements
ui = 
  navbarPage("Page Title",
             tabPanel("Panel 1",
                      sidebarPanel(
                        ## Add Name,
                        ## Number of surveys analysising
                        numericInput("n_values", "Number of columns in next panel:", 1, min = 1, max = 3)
                      ),
                      mainPanel(
                        tags$div(
                          h2("Home Page") 
                        )
                      )
             ),
             tabPanel("Panel 2",
                      fixedPage(theme = "flatly",
                                fixedRow(
                                  column(2,"First Column",
                                         fileInput("File1", "Choose a CSV files", multiple = F),
                                         actionButton("CheckData", "Validate Input"),                                                          
                                         p("Click the button to check the data was read in correctly")
                                  ),
                                  conditionalPanel(condition = "input.n_values >= 2",
                                                   column(2,"Second Column",
                                                          fileInput("File2", "Choose a CSV files", multiple = F),
                                                          p("Click the button to check the data was read in correctly")
                                                   )
                                  ),
                                  conditionalPanel(condition = "input.n_values >= 3",
                                                   column(2,"Second Column",
                                                          fileInput("File3", "Choose a CSV files", multiple = F),
                                                          p("Click the button to check the data was read in correctly")
                                                   )
                                  )                                                   
                                ),
                                fixedRow(
                                  column(12,
                                         verbatimTextOutput("errorText2")
                                  )
                                )    
                      )

             )      
  )  


server = function(input, output,session) {
  ## Call the error message function and print

  output$errorText2 <- renderText({
    validate(
      if (input$n_values == 1) {
        need(!is.null(input$File1)
             , 'You need to input the files before we can validate the data. Please select all the necessary files.')           
      } else if (input$n_values == 2) {
        need(!is.null(input$File1) & !is.null(input$File2)
             , 'You need to input the files before we can validate the data. Please select all the necessary files.')           
      } else if (input$n_values == 3) {
        need(!is.null(input$File1) & !is.null(input$File2) & !is.null(input$File3)
             , 'You need to input the files before we can validate the data. Please select all the necessary files.')           
      }
    )
    validate(ReadDataAndCheck())
  })      

  ## read in an user defined number of inputs and append them together
  ReadData <- reactive({
    print("enter: CheckData")
    temp_df = NULL;
    for (i in 1:input$n_values) {
      input_name = paste0("File", i)
      print(names(input))
      print(input_name)
      File <- input[[input_name]]  ## get an error
      ## File <- eval(expr = text(x = paste0("input$",input_name)))  ## also tried but get error cannot find object 'input'

      if (!is.null(File)) {
        this_df = read.csv(File$datapath, header = T,stringsAsFactors = F);  
        if (i == 1) {
          temp_df = this_df
        } else {
          temp_df = rbind(temp_df, this_df);
        }                      
      }
    }
    if (!is.null(temp_df)) {
      return(temp_df)
    } 
  }) 

  ReadDataAndCheck <- eventReactive(input$CheckData, {  
    print("enter: Validating data")
    Data = ReadData();
    if(is.null(Data)) {
      return("error couldn't read data")
    }
    return("successfully read in data")
  }) 
}
shinyApp(ui, server)