简体   繁体   English

R闪亮的动态标签号和输入生成

[英]R Shiny dynamic tab number and input generation

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. 然后,我想在其他循环(例如renderText)中生成这些选项卡的输入。 However, the final output of the textOutput for my generated renderText is always the one of the last renderText of the loops. 但是,生成的renderText的textOutput的最终输出始终是循环的最后一个renderText之一。

Here is a small example of the idea: 这是一个小例子:

 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)

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. 虽然对于renderText来说,解决这个问题可能并不那么重要,但我打算渲染很多图表和表格,因此无法想到解决方法。

I'd appreciate any help! 我将不胜感激!

EDIT: I've updated the code to show a small working example 编辑:我更新了代码以显示一个小的工作示例

Here's a solution that seems to work. 这是一个可行的解决方案。 I'm using lapply to create the tabs. 我正在使用lapply创建选项卡。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM