R Shiny - 如何根据条件显示/隐藏“文件输入”选项卡面板选择

问题描述

I need to show "fileinput"/file upload option when a particular tabpanel is selected.

例如。有 3 个标签面板,如 A、B 和 C

When tab B is selected the "fileinput" option should appear and when A or C is selected,the "fileinput" option should be hidden from the sidebarpanel.

我尝试了以下但不起作用。任何人都可以帮忙吗?谢谢...

sidebarPanel(
conditionalPanel(condition = "input$id == 'B'",fileInput("file","Choose xlsx file",accept = ".xlsx"))

 mainPanel(
          tabsetPanel(
            tabPanel("A",value = 'A',DT::dataTableOutput("Table A")),tabPanel("B",value = 'B',DT::dataTableOutput("Table B")),tabPanel("C",value = 'C',DT::dataTableOutput("Table C")),id ="tabselected"
          )
        )

解决方法

您需要在带有 tabsetPanel 而不是 . 的条件中使用 $ 的适当 ID。试试这个

library(readxl)

runApp(list(
  ui = shinyUI(
    fluidPage(
      
      sidebarLayout(
        sidebarPanel(
          conditionalPanel(condition = "input.tabselected == 'tab2'",fileInput("file","Choose xlsx file",accept = ".xlsx")),selectInput(
            inputId = 'selected.indicator',label = 'Select an option: ',choices = colnames(mtcars)
          )
        ),mainPanel(
          tabsetPanel(
            tabPanel("A",value = 'tab1',DTOutput("t1")),tabPanel("B",value = 'tab2',DTOutput("t2")),tabPanel("C",value = 'tab3',DTOutput("t3")),id ="tabselected"
          )
        )
      )
    )
  ),server = function(input,output,session) {
    output$t1 <- renderDT(cars)
    output$t3 <- renderDT(mtcars)
    
    mydata <- reactive({
      req(input$file)
      inFile <- input$file
      
      df <- read_excel(inFile$datapath)
    })
    
    output$t2 <- renderDT({
      req(mydata())
      mydata()
    })
    
  }
))