从Shiny中的循环渲染带有动态图的动态选项卡 [英] Render Dynamic Tabs with Dynamic Plots from loop in Shiny

查看:284
本文介绍了从Shiny中的循环渲染带有动态图的动态选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用循环制作的绘图填充动态生成的标签,但无法使用该循环中生成的所有绘图填充标签。

I'm trying to populate dynamically generated tabs with plots made from a loop but can't get the tabs to populate with all of the plots generated in the loop.

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
    })
   }

此代码几乎可以正常工作,但它会用<$中的最后一个图填充两个选项卡c $ c> plot_content()索引。我卡为如何从 plot_content()所有地块动态填充创建标签适当数量。

This code almost works but it populates both tabs with the last plot in the plot_content() index. I'm stuck as to how to get all the plots from plot_content() to dynamically fill the appropriate number of tabs created.

推荐答案

您的问题是,闪亮的反应性的计算很懒。结果,在绘制图形时,索引值 i 等于 PageNumber()他们全部。对于这个问题,我的首选解决方案是创建一个模块来管理图形的显示。 (该模块还可以处理任何其他与绘图相关的活动。)模块的不同实例管理每个单独的绘图。结果就是代码通常比其他解决方案更短,更健壮,并且更易于理解和维护。

Your problem is that Shiny reactives evaluate lazily. As a result, when the plots render, the value of the index, i, is equal to PageNumber() for all of them. My preferred solution to this problem is to create a module to manage the display the plots. (The module can also handle any other plot-related activity as well.) A different instance of the module manages each individual plot. The result is code which is usually shorter, more robust, and easier to understand and maintain than other solutions.

尽管OP的主要问题与您的不同,但我的回答是(具体来说是第二个修订版),这篇文章是一篇技术演示。

Although the OP's main question is different to yours, my answer (specifically the second revision) to this post is a demonstration of this technique.

这篇关于从Shiny中的循环渲染带有动态图的动态选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆