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

时间:2017-12-13 01:16:01

标签: r shiny conditional

我在R闪亮的条件面板中遇到了不寻常的行为。我希望有多个文件输入,用户可以上传,具体取决于他们想要多少文件。以下是可简化的代码。这个问题是如果条件大于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)

当条件&#34;列数为2&#34;我无法在第一列上传文件,这是编码问题吗?

代码在不在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)

椅子

1 个答案:

答案 0 :(得分:2)

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

我没有看到格式化或布局,这段代码只是为了说明一个工作示例。希望这有帮助!