【问题标题】:Render Dynamic Tabs with Dynamic Plots from loop in Shiny使用 Shiny 循环中的动态图渲染动态选项卡
【发布时间】:2020-10-09 08:29:50
【问题描述】:

我正在尝试使用循环生成的绘图填充动态生成的选项卡,但无法让选项卡填充循环中生成的所有绘图。

shinyUI(pageWithSidebar(

  headerPanel("Dynamic number of plots"),

sidebarPanel(
),

mainPanel(
# This is the dynamic UI for the plots
uiOutput("IndividualPlots")
 )
))

server <- function(input, output) {

output$IndividualPlots <- renderUI({
  if (is.null(input$data_cqt0)) {
    return()
  }
  plot_content()
  shiny:::buildTabset(
    id = "t",
    lapply(1:PageNumber(), function(i){
      plotname <- paste("plot", i, sep="")
      tabPanel(title = sprintf("Page_%s",i),
               #browser(),
               plotOutput(plotname,width = "800px", height = "600px") 
      )
    }), 
    paste0("nav nav-","tabs")) %>% (function(x){
      tags$div(class = "tabbable", x[[1]], x[[2]])
    })
})


plot_content<- reactive({
  for (i in 1:PageNumber()) {
    local({
      my_i <- i
      plotname <- paste("plot", my_i, sep="")   
      output[[plotname]] <- renderPlot({
          print(Plots()[[i]])
      })
    })
  }
})

PageNumber <- reactive({
  data <- data()
  return(PlotPageNumber(data, input$Class)) ## Returns number of plots
})



Plots <- reactive({
    data <- data()
     return(PlotFunction(data, input$Class)) ## Returns plots in list
    })
   }

此代码几乎可以工作,但它使用plot_content() 索引中的最后一个图填充两个选项卡。我不知道如何从plot_content() 获取所有绘图以动态填充创建的适当数量的选项卡。

【问题讨论】:

    标签: r dynamic shiny reactive


    【解决方案1】:

    您的问题是 Shiny reactives 懒惰地评估。因此,当绘图渲染时,索引的值 i 对于所有绘图都等于 PageNumber()。我对这个问题的首选解决方案是创建一个模块来管理绘图的显示。 (该模块还可以处理任何其他与绘图相关的活动。)该模块的不同实例管理每个单独的绘图。结果是代码通常比其他解决方案更短、更健壮、更易于理解和维护。

    虽然 OP 的主要问题与您的不同,但我对 this post 的回答(特别是第二次修订版)是对这种技术的演示。

    【讨论】:

      猜你喜欢
      • 2021-10-06
      • 2018-01-14
      • 2020-04-04
      • 1970-01-01
      • 1970-01-01
      • 2019-04-28
      • 2018-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多