【问题标题】:Multiple plots in Shiny dynamic UIShiny 动态 UI 中的多个绘图
【发布时间】:2017-08-08 04:52:15
【问题描述】:

我正在尝试在闪亮的应用程序中动态插入许多图。我已将其简化为一个简单的示例,它显示三个具有相同数量的箱 (48) 的直方图,即使它们应该不同 (16、32、48)。可能我做了一些非常愚蠢的事情,或者错过了一些更微妙的事情!附上代码。提前感谢您的任何建议。

shinyUI(fluidPage(tagList(
          actionButton("button_id", "Show plot"),
          uiOutput("plot_id"),
          HTML("<div id=\"end\">The end</div>"))))

shinyServer(function(input, output) {

    # A list to hold the plot IDs
ls <- list()

observeEvent(input$button_id,
{
    x <- faithful[, 2]

    for (i in 1:3)
    {
            # generate bins based on input$bins from ui.R
        count <- i * 16
        bins <- seq(min(x), max(x), length.out = count)

        ls[[i]] <<- paste0("plot_", i)

        output[[ls[[i]]]] <- renderPlot(hist(x, breaks = bins, col = 'darkgray', border = 'white'))
    }
    output$plot_id <- renderUI({


        wellPanel(do.call(tagList, lapply(ls, function(x) 
        { 
            plotOutput(x) 
        })))

    })

})

})

【问题讨论】:

  • 我的解决方案有效吗?

标签: r plot dynamic shiny


【解决方案1】:

这是导致问题的惰性评估。直方图的代码不会立即执行,而只会在要渲染绘图时执行。这是在 for 循环完成且 i 的值为 3 之后。

为了强制即时评估,您可以将循环体包装在 local 环境中,如下所示:

for (i in 1:3) {
    local({
    # generate bins based on input$bins from ui.R
    count <- i * 16
    bins <- seq(min(x), max(x), length.out = count)

    ls[[i]] <<- paste0("plot_", i)
    output[[ls[[i]]]] <- renderPlot(hist(x, breaks = bins, col = 'darkgray', border = 'white'))
    })
}

【讨论】:

    猜你喜欢
    • 2015-02-25
    • 2015-03-17
    • 2015-10-31
    • 2014-04-19
    • 1970-01-01
    • 2021-12-14
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多