【问题标题】:R shiny conditional panels with multiple file inputs具有多个文件输入的 R 闪亮条件面板
【发布时间】:2017-12-13 15:41:24
【问题描述】:

我在 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)

当条件“列数为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)

椅子

【问题讨论】:

    标签: r shiny conditional


    【解决方案1】:

    问题是您使用了两次相同的元素;您在代码中使用了两次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)
    

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

    【讨论】:

    • 谢谢你解决了这个问题,我想知道你是否有任何关于上述情况的 cmets,但是有 10 个条件,它似乎很快就变得一团糟
    猜你喜欢
    • 1970-01-01
    • 2014-03-03
    • 2016-04-10
    • 2017-05-19
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    相关资源
    最近更新 更多