R Shiny - 如何 show/hide "fileinput" 基于条件(选项卡面板选择)

R Shiny - How to show/hide "fileinput" based on condition (tab panel selection)

我需要在选择特定选项卡面板时显示“文件输入”/文件上传选项。

例如。有 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()
    })
    
  }
))