【问题标题】:R Shiny - How to show/hide "fileinput" based on condition (tab panel selection)R Shiny - 如何根据条件显示/隐藏“文件输入”(选项卡面板选择)
【发布时间】:2021-07-10 03:46:50
【问题描述】:

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"
          )
        )

【问题讨论】:

    标签: r shiny shinydashboard shinyapps shiny-reactivity


    【解决方案1】:

    您需要在带有. 的条件中使用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()
        })
        
      }
    ))
    

    【讨论】:

    • 我还发布了一些其他问题。如果你也能帮助我,那就太好了!!!
    猜你喜欢
    • 2021-07-09
    • 2017-02-20
    • 2021-10-24
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 2021-09-08
    相关资源
    最近更新 更多