R闪亮-动态FileInput标签

时间:2018-08-30 19:22:38

标签: r shiny shiny-server shinydashboard

我正在构建一个应用,其中我需要选择单文件上传还是双文件上传。我已经使用conditionalpanel实现了单文件上传和双文件上传,但是我无法更改FileInput标签。 这就是我需要的 1)当用户单击单个文件时。只能有一个带有标签“ Choose Consolidated file”的FileInput 2)当用户单击单独的文件时。应该有两个带有标签“选择测试文件”和“选择控制文件”的FileInputs

以下是有效代码     库(发光)

ui<-shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      radioButtons("p", "separate input files or consolidated?",
                   list("Single file"='a', "Separate files"='b'))
    ),
    mainPanel(
        fileInput("file1","Choose first file",
                  accept = c(
                    "text/csv",
                    "text/comma-separated-values,text/plain",
                    ".csv","sas7bdat")
        ),
        conditionalPanel(
          condition = "output.dual",
          fileInput("file2", "Choose second file",
                    accept = c(
                      "text/csv",
                      "text/comma-separated-values,text/plain",
                      ".csv","sas7bdat")
          )
        ),
        conditionalPanel(
          condition = "output.dual",
          checkboxInput('headercheckbox', 
                        "Files have different headers?", 
                        value = FALSE
          )
        )### bracket close of conditional panel
    )
  )
))

server<-shinyServer(function(input, output) {

  output$dual <- reactive({ input$p == 'b' })
  outputOptions(output, 'dual', suspendWhenHidden = FALSE)


})

shinyApp(ui,server)

让我知道是否有人可以帮助您?

2 个答案:

答案 0 :(得分:0)

我建议为此使用uiOutput和renderUI。如果您想了解更多有关这些功能的信息,那么闪亮的参考材料非常不错。 https://shiny.rstudio.com/reference/shiny/latest/renderUI.html

ui<-shinyUI(
  fluidPage(
    sidebarLayout(
      sidebarPanel(
        radioButtons("p", "separate input files or consolidated?",
                     list("Single file"='a', "Separate files"='b'))
      ),
      mainPanel(
        uiOutput('file_area_1'),
        uiOutput('file_area_2'),
        uiOutput('diff_headers')
        )### bracket close of conditional panel
      )
    )
)

server<-shinyServer(function(input, output) {
  output$dual <- reactive({ input$p == 'b' })
  outputOptions(output, 'dual', suspendWhenHidden = FALSE)
  output$file_area_1 <- renderUI({
    message = 'Choose consolidated file'
    if(input$p == 'b'){
      message = 'Choose test file'
    }else{
      mesage = ''
    }
    fileInput("file1",message,
              accept = c(
                "text/csv",
                "text/comma-separated-values,text/plain",
                ".csv","sas7bdat")
    )
  })
  output$file_area_2 <- renderUI({
    if(input$p == 'b'){
      fileInput("file2","Choose control file",
                accept = c(
                  "text/csv",
                  "text/comma-separated-values,text/plain",
                  ".csv","sas7bdat")
      ) 
    }
  })
  output$diff_headers <- renderUI({
    if(input$p == 'b'){
        checkboxInput('headercheckbox', 
                      "Files have different headers?", 
                      value = FALSE
        )
      }
  })
})

shinyApp(ui,server)

答案 1 :(得分:0)

我认为,如果允许面板根据用户输入来定义条件,则可以获得所需的功能:

ui<-shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      radioButtons("p", "separate input files or consolidated?",
                   list("Single file"='a', "Separate files"='b'))
    ),
    mainPanel(
      conditionalPanel(
        condition ="output.dual == 'a' " ,
        fileInput("file1","Choose Consolodated file",
                  accept = c(
                    "text/csv",
                    "text/comma-separated-values,text/plain",
                    ".csv","sas7bdat")
        )
      ),
      conditionalPanel(
        condition ="output.dual == 'b' " ,
        fileInput("file1","Choose Test file",
                  accept = c(
                    "text/csv",
                    "text/comma-separated-values,text/plain",
                    ".csv","sas7bdat")
        )
      ),
      conditionalPanel(
        condition = "output.dual == 'b'",
        fileInput("file2", "Choose control file",
                  accept = c(
                    "text/csv",
                    "text/comma-separated-values,text/plain",
                    ".csv","sas7bdat")
        )
      ),
      conditionalPanel(
        condition = "output.dual == 'b'",
        checkboxInput('headercheckbox', 
                      "Files have different headers?", 
                      value = FALSE
        )
      )### bracket close of conditional panel
    )
  )
))

server<-shinyServer(function(input, output) {

  output$dual <- reactive({ input$p  })
  outputOptions(output, 'dual', suspendWhenHidden = FALSE)


})

shinyApp(ui,server)