简体   繁体   English

R闪亮地迭代反应式中的多个输入

[英]R shiny iterating over multiple inputs in a reactive

below is some reproducible code for the issue. 下面是该问题的一些可复制代码。 But basically I have an app that can have many user input files that will be of similar structure. 但基本上我有一个应用程序,可以包含许多结构类似的用户输入文件。 I want to have a reactive() function that will iterate over all the inputs and append the data to return. 我想有一个react()函数,该函数将遍历所有输入并追加数据以返回。 See the function ReadData() at line 78 of the code for the issue. 有关此问题,请参见代码第78行的ReadData()函数。 I can loop over all the files because I have made there id's the same with an incremental integer, eg 1 input id = File1, 2 input id = File2 etc. 我可以遍历所有文件,因为我使用增量整数使id相同,例如1输入id = File1,2输入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)

Thanks in advance for any suggestions 在此先感谢您的任何建议

You are almost there just change the line File <- get(input_name, input) to File <- input[[input_name]] . 您几乎就在那里,只需将File <- get(input_name, input)行更改为File <- get(input_name, input) File <- input[[input_name]]

This is a working version for me 这是我的工作版本

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)

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

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