我正在尝试在一个选项卡中动态呈现多个图(最好是在多个选项卡中都可以这样做)。经过一番搜索,我发现这个post非常有用。但就我而言,地块数由上传的CSV文件确定。所以我认为问题是如何在for循环中调用plotInput()$n_plot?我感谢任何建议!

目前,我可以通过调用<div>s来创建renderUI的倍数。

<div id="plot1" class="shiny-plot-output" style="width: 800px ; height: 800px"></div>
<div id="plot2" class="shiny-plot-output" style="width: 800px ; height: 800px"></div>

但是我无法在for (i in 1:plotInput()$n_plot)循环中正确调用响应式(Reactive)函数。错误消息是:
Error in .getReactiveEnvironment()$currentContext() :
  Operation not allowed without an active reactive context.

服务器
  shinyServer(function(input, output) {

   ### This is the function to break the whole data into different blocks for each page
   plotInput <- reactive({
    get_the_data()
    return (list("n_plot"=n_plot, "total_data"=total_data))
  })

    ##### Create divs######
    output$plots <- renderUI({
      plot_output_list <- lapply(1:plotInput()$n_plot, function(i) {
         plotname <- paste("plot", i, sep="")
         plotOutput(plotname, height = 280, width = 250)
       })
       do.call(tagList, plot_output_list)
    })

    # Call renderPlot for each one.
    ####This is the place caused the error##
    for (i in 1:plotInput()$n_plot) {
        local({
            my_i <- i
            plotname <- paste("plot", my_i, sep="")
            output[[plotname]] <- renderPlot({
                hist(plotInput()$total_data[i])
            })
        })
    }
    })

更新

如果我将for循环包装在reactive函数中,并将其作为renderUI的一部分进行调用,则该循环有效,但是缺少该图,我想这是因为renderUI仅创建HTML标签,因此不会将图像分配给生成的标签。
    output$plots <- renderUI({
      plot_output_list <- lapply(1:plotInput()$n_plot, function(i) {
         plotname <- paste("plot", i, sep="")
         plotOutput(plotname, height = 280, width = 250)
       })
       do.call(tagList, plot_output_list)
       plot_concent()
    })

    # Call renderPlot for each one.
    ####This is the place caused the error##
    plot_concent<-reactive({
      for (i in 1:plotInput()$n_plot) {
        local({
            my_i <- i
            plotname <- paste("plot", my_i, sep="")
            output[[plotname]] <- renderPlot({
                hist(plotInput()$total_data[i])
            })
        })
      }
    })

最佳答案

如果仍然有人对答案感兴趣,请尝试以下操作:

library(shiny)

runApp(shinyApp(

  ui = shinyUI(
    fluidPage(
      numericInput("number", label = NULL, value = 1, step = 1, min = 1),
      uiOutput("plots")
    )
  ),

  server = function(input, output) {

    ### This is the function to break the whole data into different blocks for each page
    plotInput <- reactive({
      n_plot <- input$number
      total_data <- lapply(1:n_plot, function(i){rnorm(500)})
      return (list("n_plot"=n_plot, "total_data"=total_data))
    })

    ##### Create divs######
    output$plots <- renderUI({
      plot_output_list <- lapply(1:plotInput()$n_plot, function(i) {
        plotname <- paste("plot", i, sep="")
        plotOutput(plotname, height = 280, width = 250)
      })
      do.call(tagList, plot_output_list)
    })

    observe({
      lapply(1:plotInput()$n_plot, function(i){
        output[[paste("plot", i, sep="") ]] <- renderPlot({
          hist(plotInput()$total_data[[i]], main = paste("Histogram Nr", i))
        })
      })
    })
  }

))

关于r - 实时运行 Shiny 的R renderPlots,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26931173/

10-12 19:50