R 闪亮的动态UI中的多个绘图
我正在尝试在一个闪亮的应用程序中动态插入一些绘图。我将其简化为一个简单的示例,其中显示了三个具有相同数量的箱(48)的直方图,尽管它们应该不同(16、32、48)。可能我做了一些非常愚蠢的事情,或者我错过了一些更微妙的事情!代码见附件。提前感谢您的建议R 闪亮的动态UI中的多个绘图,r,plot,dynamic,shiny,R,Plot,Dynamic,Shiny,我正在尝试在一个闪亮的应用程序中动态插入一些绘图。我将其简化为一个简单的示例,其中显示了三个具有相同数量的箱(48)的直方图,尽管它们应该不同(16、32、48)。可能我做了一些非常愚蠢的事情,或者我错过了一些更微妙的事情!代码见附件。提前感谢您的建议 shinyUI(fluidPage(tagList( actionButton("button_id", "Show plot"), uiOutput("plot_id"), HTML(
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)
})))
})
})
})
shinyUI(fluidPage(标记列表(
actionButton(“按钮id”,“显示绘图”),
uiOutput(“plot_id”),
HTML(“结尾”))
shinyServer(功能(输入、输出){
#保存打印ID的列表
ls造成此问题的是延迟计算。直方图的代码不会立即执行,而是仅在将要渲染绘图时执行。这是在for
-循环完成且i
的值为3之后执行的
为了强制进行即时计算,您可以将循环体包装在本地
环境中,如下所示:
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'))
})
}
for(1:3中的i){
本地的({
#根据ui.R中的输入$bins生成bins
伯爵,我的解决方案有效吗?