我正在开发一个我使用的闪亮应用程序tabsetPanel,它是在用户输入某些特定输入时生成的.因此,我想使用renderUI函数使tabsetPanel出现/消失.
我现在的奋斗是,元素的数量 tabPanel(参数tabsetPanel)也取决于用户输入,顺便说一下:有时我需要1个,有时我需要更多tabPanels.
怎么做?我尝试在论证中包含conditionPanel或包含简单if()...条件tabsetPanel,但它(并不奇怪......)不起作用.
我想创建一个使用闪亮的应用程序,动态地将图表添加到页面.它可能是10个地块,它可能只有一个.我在闪亮的主页中使用本教程来获取动态UI.
这是一个简化的例子.该函数showme正在绘制图形
shinyServer(function(input, output) {
# Create an environment for storing data
symbol_env <- new.env()
# Make a chart for a symbol, with the settings from the inputs
make_chart <- function(symbol) {
showme(symbol)
}
display <- c("1083484" , "1101732")
output$MyList <- renderUi({
for (i in i:nrow(display))
renderPlot({make_chart(display[i])})
})
})
Run Code Online (Sandbox Code Playgroud)
shinyUI(pageWithSidebar(
headerPanel("My Plots !"),
sidebarPanel(
wellPanel(
p(strong("Scan1"))))
,mainPanel(
uiOutput("MyList")
)))
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Listening on port 8100
Error in .subset2(x, "impl")$defineOutput(name, value, deparse(substitute(value))) :
Unexpected character output …Run Code Online (Sandbox Code Playgroud) 我想构建一个应用程序,在用户键入正确的密码之前,某些选项卡将对用户隐藏。我知道如何使用shinyjs::hideTab:
library(shiny);library(shinyjs)
ui <- fluidPage(useShinyjs(),
navbarPage("hello", id="hello",
tabPanel("home", br(), h3("this is home"),passwordInput("pass", "enter 'password' to see the tabs: "),actionButton("enter", "enter")),
tabPanel("tab2", br(), h4("this is tab2")),
tabPanel("tab3 with a lot of stuff in it", br(), h4("this is tab3"))))
server <- function(input, output, session) {
hideTab("hello", "tab2"); hideTab("hello", "tab3 with a lot of stuff in it")
observeEvent(input$enter, {
if (input$pass == "password"){showTab("hello", "tab2"); showTab("hello", "tab3 with a lot of stuff in it")}})}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
但是,有一点“东西”。在我的应用程序中,隐藏标签有很多的东西,比如小部件,uiOutputs,图表,图像,global.R文件阅读等的结果是,加载时间更高,在这个应用程序的加载时间(前hideTab指令开始运行),用户实际上会看到隐藏的选项卡,甚至可以单击它们并查看其中的内容。他们保持“可见”状态大约1秒钟,然后被隐藏。
有没有办法让它们在构建UI之前立即隐藏?我更喜欢一个解决方案,而不必将我的所有ui代码放入server.R脚本中...
谢谢
在 Rmarkdown 中,可以创建选项卡,例如:
---
output: html_document
---
# Tabs {.tabset}
## Tab 1
foo
## Tab 2
bar
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以创建任意数量的标签?如何以编程方式创建选项卡?
下面的代码是一个糟糕的尝试,但它会产生一个标题而不是一个选项卡。
---
output: html_document
---
# Tabs {.tabset}
```{r echo=FALSE}
shiny::tags$h2("Tab 1")
```
foo
## Tab 2
bar
Run Code Online (Sandbox Code Playgroud)
感谢@GGamba 提供了一个很好的解决方案。我需要更进一步,并能够将选项卡添加为循环的一部分,因此我需要进行两项更改。首先,我使用这段代码动态添加选项卡(这里唯一的区别是我强制hrefCode在超时内部评估,否则所有超时调用在一起将使用相同的值)
(function(hrefCode){setTimeout(function(){
var tabContent = document.createElement('div');
var tabContainerTarget = document.getElementsByClassName('tab-content')[0];
tabContent.setAttribute('id', 'tab-' + hrefCode);
tabContent.setAttribute('class', 'tab-pane')
tabContent.innerHTML = '", gsub('\n', '', Panel, fixed = TRUE), "';
tabContainerTarget.appendChild(tabContent);
}, 100);
})(hrefCode);
Run Code Online (Sandbox Code Playgroud)
其次,要在循环中添加选项卡,您可以执行以下操作:
tabsToAdd <- list("tab3" = "hello", "tab4" = "world") …Run Code Online (Sandbox Code Playgroud) 这不是关于使用 renderUI 创建模块。使用我理解的 renderUI,您可以在 UI 函数中放置一个占位符,然后在服务器函数中编写控件/小部件。
模块分为两部分。您必须使用 callModule() 将一部分添加到 UI 函数,另一部分添加到服务器函数。
我有一个滑块模块。我想在单击“添加”操作按钮时将其添加到井面板。如果它有帮助,您可以考虑在单击按钮时多次复制模块。重复的模块应该都是独立的。
视觉上
我想知道一个动作按钮如何在 UI 功能内添加模块的 UI 部分,在服务器功能内添加服务器部分。
#Dynamically adding modules
library(shiny)
#slider module ------------------------
sliderUI <- function(id) {
ns <- NS(id)
sliderInput(ns("bins"), "Number of Bins:", min = 1, max = 5, value = 3)
}
slider <- function(input, output, session) {}
#shiny app ------------------------
ui <- fixedPage(
fixedRow(
column(width = 4, wellPanel(
h4("Slider Module"),
sliderUI("slider"),
actionButton("addSliderModule", "Add Slider Module"))
),
column(width = 4, wellPanel(
h4("Dynamic Loading Modules"),
p("Clicking on …Run Code Online (Sandbox Code Playgroud)