[英]Multiple plots in Shiny dynamic UI
我正在嘗試在閃亮的應用程序中動態插入許多圖表。 我將其簡化為一個簡單的示例,該示例顯示了三個直方圖,它們具有相同數量的箱(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)
})))
})
})
})
懶惰的評估在這里引起了問題。 直方圖的代碼不會立即執行,而僅在要繪制繪圖時才執行。 這是在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'))
})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.