本文介绍了R Shiny - 动态添加 tabPanel 到 tabsetPanel(使用 renderUI)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个闪亮的应用程序,我在其中使用 tabsetPanel,当用户输入某些特定输入时生成该应用程序.因此,我想使用 renderUI 函数使 tabsetPanel 出现/消失.

I am developing a shiny application in which I use tabsetPanel, which is generated when user enters some particular input. Thus, I want to use renderUI function to make a tabsetPanel appear / disappear.

我现在的困难是数量 tabPanel 元素(tabsetPanel 的参数)也取决于用户输入,顺便说一句:有时我需要 1 个,有时我想要更多 tabPanels.

My struggle now is that number of tabPanel elements (arguments of tabsetPanel) also depends on user input, in the way: sometimes I need 1 one, other times I want more tabPanels.

怎么做?我尝试在 tabsetPanel 参数中包含 conditionPanel 或包含简单的 if()... 条件,但它(并不奇怪......)没有用.

How to make it? I tried including conditionPanel or including simple if()... condition in the tabsetPanel argument, but it (rather not surprisingly...) did not work.

推荐答案

给你.代码一目了然.

library(shiny)
runApp(list(
  ui = pageWithSidebar(
    headerPanel('Dynamic Tabs'),
    sidebarPanel(
      numericInput("nTabs", 'No. of Tabs', 5)
    ),
    mainPanel(
      uiOutput('mytabs')
    )
  ),
  server = function(input, output, session){
    output$mytabs = renderUI({
      nTabs = input$nTabs
      myTabs = lapply(paste('Tab', 1: nTabs), tabPanel)
      do.call(tabsetPanel, myTabs)
    })
  }
))

这篇关于R Shiny - 动态添加 tabPanel 到 tabsetPanel(使用 renderUI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 21:17