r语言 - 渲染内部观察事件(动态绘图生成)



我试图重新创建https://gist.github.com/wch/5436415/,但通过一个按钮添加情节。我认为这需要我沿着这些行修改链接上的server.r代码(通过reactiveValues创建chartname相关变量)-

observeEvent(
      input$buttonAddData, 
      {
         ...
         newchartname = c(newchartname,newchartname)
         output[[newchartname]] = renderPlot({
            ...
         })
      }
)
output$plots = renderUI(
      {
         plot_output_list <- lapply(
            seq(length(allthechartnames)), 
            function(i) {
               plotname <- paste(
                  isolate(allthechartnames)[i]
               )
               plotOutput(plotname, height = 280, width = 250)
               cat(plotname,'n')
            }
         )
         # Convert the list to a tagList - this is necessary for the list of items
         # to display properly.
         do.call(tagList, plot_output_list)
      }
   )

但这似乎没有进入在observeEvent中创建图表本身的原始循环,这导致renderUI块中没有图表。

提示吗?

我真的不理解你问题中的代码,所以我忽略了它,只是从要点中提取了Winston的代码,并使其使用按钮而不是滑块。我希望你是这个意思?此外,我必须使用渲染内的观察者,我不高兴:/

runApp(shinyApp(
  ui = fluidPage(
    headerPanel("Dynamic number of plots"),
    mainPanel(
      actionButton("addplot", "Add plot"),
      uiOutput("plots")
    )
  ),
  server = function(input, output, session) {
    # A variable that keeps track of the number of plots we have
    values <- reactiveValues(
      numPlots = 1
    )
    # Whenever the "add plot" button is pressed, increment num plots by 1
    observeEvent(input$addplot, {
      values$numPlots <- values$numPlots + 1
    })
    # Dynamically generate the UI that creates all the plots
    output$plots <- renderUI({
      # Create a list of `plotOutput` objects (for each plot, use a unique ID)
      plot_output_list <- lapply(1:values$numPlots, function(i) {
        plotname <- paste("plot", i, sep="")
        plotOutput(plotname, height = 280, width = 250)
      })
      # Place all the plot outputs inside a shiny `tagList()`
      do.call(tagList, plot_output_list)
    })
    # Every time the number of plots changes (button is clicked),
    # re-generate the render functions for all the plots
    observeEvent(values$numPlots, {
      for (i in 1:values$numPlots) {
        local({
          my_i <- i
          plotname <- paste("plot", my_i, sep="")
          output[[plotname]] <- renderPlot({
            plot(1:my_i, 1:my_i,
                 xlim = c(1, values$numPlots),
                 ylim = c(1, values$numPlots),
                 main = paste("1:", my_i, ".  n is ", values$numPlots, sep = "")
            )
          })
        })
      }
    })
  }
))

希望有帮助

最新更新