具有多个文件输入的 R 闪亮条件面板

R shiny conditional panels with multiple file inputs

我遇到了 R shiny 中条件面板的异常行为。我想要多个文件输入,用户可以根据他们想要的文件数量上传这些文件。以下是可简化的代码。这个问题是如果条件大于 1 我不能用 csv 文件填充所有文件?我可以第二但不是第一

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 = 2)
    ),
    mainPanel(
      tags$div(
        h2("Home Page") 
      )
    )
   ),
    tabPanel("Panel 2",
      conditionalPanel(condition = "input.n_values == 1",
        fixedPage(theme = "flatly",
          fixedRow(
            column(2,"First Column",
              fileInput("File1", "Choose a CSV files", multiple = F),
              p("Click the button to check the data was read in correctly")
            ),
            fixedRow(
              column(12,
                verbatimTextOutput("errorText1")
              )
            )    
          )
        )
      ),
      conditionalPanel(condition = "input.n_values == 2",
        fixedPage(theme = "flatly",
          fixedRow(
            column(2,"First Column",
              fileInput("File1", "Choose a CSV files", multiple = F),
              p("Click the button to check the data was read in correctly")
            ),
            column(2,"Second Column",
              fileInput("File2", "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$errorText1 <- 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.')            
      }
    )
    validate("allgravy")

  })
  output$errorText2 <- renderText({
    validate(
      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.')           
      }
    )
    validate("allgravy")
  })      
} 

shinyApp(ui, server)

当条件"number of columns is 2"我无法在第一列上传文件时,这是编码问题吗?

代码在不在条件面板中时有效,请参阅下面的可重现示例

ui = 
navbarPage("Page Title",
  tabPanel("Panel 1",
    sidebarPanel(
        ## Add Name,
        ## Number of surveys analysising
        numericInput("n_surveys", "Number of surveys analysing:", 2, min = 1, max = 10)
    ),
    mainPanel(
      tags$div(
        h2("Home Page") 
      )
    )
   ),
    tabPanel("Panel 2",
      fixedPage(theme = "flatly",
        fixedRow(
          column(2,h4("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")
          ),
          column(2,h4("Second Column"),
            fileInput("File2", "Choose a CSV files", multiple = F)
          ),          
          fixedRow(
            column(12,
              verbatimTextOutput("errorText")
            )
          )    
        )
      )
    )  
  )

server = function(input, output,session) {
  ## Call the error message function and print
  output$errorText <- renderText({
    validate(
      need(!is.null(input$File1)
             , 'You need to input the files before we can validate the data. Please select all the necessary files.')
      )
    validate("seems allgood")
  })
} 

shinyApp(ui, server)

椅子

问题是您使用了同一个元素两次;您在代码中两次使用行 fileInput("File1", "Choose a CSV files", multiple = F),这是不允许的(我认为这与 this 有关)。

您可以通过仅使用该元素一次并更改您的条件来解决此问题。例如像这样:

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 = 2)
                      ),
                      mainPanel(
                        tags$div(
                          h2("Home Page") 
                        )
                      )
             ),
             tabPanel("Panel 2",
                      conditionalPanel(condition = "input.n_values == 1 | input.n_values == 2",
                                       fixedPage(theme = "flatly",
                                                 fixedRow(
                                                   column(2,"First Column",
                                                          fileInput("File1", "Choose a CSV files", multiple = F),
                                                          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")
                                                                    )
                                                   )
                                                 ),
                                                 fixedRow(
                                                   column(12,
                                                          verbatimTextOutput("errorText2")
                                                   )
                                                 )    
                                       )
                      )
             )      
  )  
)

server = function(input, output,session) {
  ## Call the error message function and print
  output$errorText1 <- 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.')            
      }
    )
    validate("allgravy")

  })
  output$errorText2 <- renderText({
    validate(
      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.')           
      }
    )
    validate("allgravy")
  })      
} 

shinyApp(ui, server)

我并没有真正查看格式或布局,这段代码只是为了说明一个工作示例。希望这对您有所帮助!