问题描述
我当前的闪亮代码存在问题.我必须根据给定函数的结果生成动态数量的选项卡(该部分可以正常工作).然后,我想在其他循环(例如renderText)中生成这些选项卡的输入.但是,我生成的renderText的textOutput的最终输出始终是循环的最后一个renderText之一.
I've got an issue with my current shiny code.I have to generate a dynamic number of tabs depending on the results of a given function (that part works fine). Then, I want to generate the input of these tabs in other loops of for example renderText. However, the final output of the textOutput for my generated renderText is always the one of the last renderText of the loops.
这是一个小例子:
library(shiny)
library(shinydashboard)
ui <- pageWithSidebar(
headerPanel("xxx"),
sidebarPanel(),
mainPanel(
uiOutput("multipleUI")
)
)
server <- function(input, output) {
output$multipleUI <- renderUI({
tabs <- list(NULL)
for(i in 1:5){
tabs[[i]] <- tabPanel(title = paste0("tab ",i),
textOutput(paste0("out",i)), # prints j as 5 in all tabs
paste0("local out ",i)) # prints i as current loop value for each tab)
}
do.call(tabBox,tabs)
})
observe({
for(j in 1:5){
txt = paste0("generated out ", j)
print(txt) # print with current j
output[[paste0("out",j)]] <- renderText({txt})
}
})
}
shinyApp(ui, server)
尽管对于renderText而言,我可以解决该问题可能并不那么重要,但我打算呈现许多图表和表格,并且无法想到解决方法.
While it might not be that important for renderText where I can just work around the issue, I intend to render a lot of plots and tables and couldn't think of a workaround there.
我将不胜感激!
我已经更新了代码以显示一个小的工作示例
I've updated the code to show a small working example
推荐答案
这是一个可行的解决方案.我正在使用lapply
创建选项卡.让我知道它是否可以满足您的需求.
Here's a solution that seems to work. I'm using lapply
to create the tabs. Let me know if it works for what you need.
library(shiny)
ui <- pageWithSidebar(
headerPanel("xxx"),
sidebarPanel(),
mainPanel(
do.call(tabsetPanel, c(id='tab',lapply(1:5, function(i) {
tabPanel(
title=paste0('tab ', i),
textOutput(paste0('out',i))
)
})))
)
)
server <- function(input, output) {
lapply(1:5, function(j) {
output[[paste0('out',j)]] <- renderPrint({
paste0('generated out ', j)
})
})
}
shinyApp(ui, server)
这篇关于R闪亮的动态制表符编号和输入生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!