繁体   English   中英

具有多个文件输入的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)

当条件为“列数为2”时,我无法在第一列中上传文件,这是编码问题吗?

该代码在不在conditionalPanel中时有效,请参见下面的可重现示例

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) ,并且这是不允许的(我认为这与此有关 )。

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

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)

我并没有真正看过格式或布局,这段代码只是为了说明一个有效的示例。 希望这可以帮助!

暂无
暂无

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

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