求助:Shiny应用侧边栏标签选择实现条件输出的报错解决
解决Shiny中
object 'input.tabs' not found报错问题 这个报错的核心原因是你在conditionalPanel的condition参数里用了R语法,但这个参数要求的是JavaScript表达式——Shiny的UI层在渲染时会用JS来判断条件,而非R代码。
具体修正步骤
把conditionalPanel里的条件判断改成JS的语法规则,并且用引号把整个表达式包裹起来:
- 针对「otu」标签的条件:
"input.tabs === 'otu'" - 针对「anova」标签的条件:
"input.tabs === 'anova'"
修改后的完整UI代码
ui <- fluidPage( # Make a title to display in the app titlePanel(" Exploring the Effect of Metarhizium on the Soil and Root Microbiome "), # Make the Sidebar layout sidebarLayout( # Put in the sidebar all the input functions sidebarPanel( tabsetPanel(id="tabs", tabPanel("otu","OTU selection", selectInput('dataset', 'dataset', names(abundance_tables)), uiOutput("otu"), br(), # Add comment p("For details on OTU identification please refer to the original publications")), tabPanel("anova","anova", sliderInput('pval','p-value for significance', value=5,min=0,max=0.5,step=0.00001), selectInput('fact_ofInt','factor of interest',FactorsOfInt)) ) ), # Put in the main panel of the layout the output functions mainPanel( conditionalPanel(condition = "input.tabs === 'otu'", plotOutput('plot') # dataTableOutput("table") ), conditionalPanel(condition = "input.tabs === 'anova'", plotOutput('plot2') # dataTableOutput("table") ) ) ) )
补充说明
- 在
conditionalPanel中,input是前端JS层面的对象,必须遵循JS语法(比如用===做严格相等判断,字符串用单/双引号包裹) - 整个条件表达式必须是字符串格式,这样Shiny才会将其传递给前端JS执行判断逻辑
内容的提问来源于stack exchange,提问作者user2814482
相关产品推荐
相关产品推荐

