【问题标题】:Method to Loop Through a List in a Shiny Reactive Render Function在闪亮的反应式渲染函数中循环遍历列表的方法
【发布时间】:2016-07-17 19:19:22
【问题描述】:

我已经生成了一系列图,当在 Shiny 应用程序中生成动态数量的图时,我想循环这些图。在服务器功能中,我有一个具有以下结构的观察功能:

server = function(input, output, session) {

    <Lots of other code>

    plotlist = generate_list_of_plots()
    for(i in seq_len(length(plot list))) {
        plotname = sprintf('ui_plot_%i', i)
        output[[plotname]] = renderPlot(plotlist[[i]])
    }

    <Lots of other code>

}

不幸的是,这并没有像我想要的那样起作用,因为列表中的最后一个图对于在单独的代码块中生成的每个相关的 plotOutput 对象都是重复的。我相信这种行为与以下事实有关:直到当用户单击选项卡并且i 索引变量已前进到其最终位置并且每次渲染图时都是静态的时,才会调用renderPlot 表达式函数被执行,因此我得到了相同的情节。

1:这是问题的真正原因吗? 2:如果是,那么处理这种情况的正确方法是什么?

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    似乎我能够找到问题的解决方案。我的假设是正确的,答案是local 函数。在this 帖子中找到了解决方案。

    更正后的代码如下:

    server = function(input, output, session) {
    
        <Lots of other code>
    
        plotlist = generate_list_of_plots()
        for(i in seq_len(length(plot list))) {
            local({
                my_i = i
                plotname = sprintf('ui_plot_%i', my_i)
                output[[plotname]] = renderPlot(plotlist[[my_i]])
            })
        }
    
        <Lots of other code>
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-17
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-03
      • 2018-04-26
      • 2016-06-12
      • 1970-01-01
      相关资源
      最近更新 更多