Shiny R 实时渲染绘图

9
我正在尝试在选项卡中动态渲染多个图表(如果可以跨多个选项卡进行,则更好)。经过一些搜索,我发现这篇文章非常有帮助。但在我的情况下,图表的数量由上传的CSV文件确定。所以我认为问题是如何在for循环中调用plotInput()$n_plot?感谢任何建议!
此时,我能够通过调用renderUI来创建多个<div>
<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) 循环中正确地调用反应式函数。错误信息如下:

Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context.

Server.R

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

在添加反应式时,没有任何数字。没有反应式就不起作用。这就是问题所在。 - Zhilong Jia
我有完全相同的问题,你找到解决方案了吗? - bgbrink
2个回答

8
如果还有人对此问题感兴趣,请尝试以下方法:

如果您仍然对此问题感兴趣,请尝试以下方法:

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

))

-1

您应该使用 local({}) 而不是 isolate({})

http://www.inside-r.org/packages/cran/shiny/docs/isolate

"

给isolate()的表达式在调用环境中进行评估。这意味着,如果你在isolate()内部分配一个变量,它的值将在isolate()外部可见。如果你想避免这种情况,可以在isolate()内部使用local()。

"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接