微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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()
    })
    
  }
))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。