2017-08-08 19 views
1

我想動態地插入一些閃亮的應用程序中的情節。我已經將這個簡化爲一個簡單的例子,它顯示了三個具有相同數量的箱(48)的直方圖,即使它們應該不同(16,32,48)。可能我做了一些非常愚蠢的事情,或者錯過了更微妙的東西!代碼附加。預先感謝任何暗示。Shiny動態用戶界面中的多個地塊

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

    }) 

}) 

}) 
+0

做了我解決方案? – AEF

回答

0

這裏是導致問題的懶惰評估。直方圖的代碼不會立即執行,而只會在渲染圖時才執行。這是for -loop已完成並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')) 
    }) 
} 
+0

謝謝 - 這工作得很好! – jxf