闪亮的R renderPlots在飞行中

时间:2014-11-14 13:32:28

标签: r shiny shiny-server

我正在尝试在选项卡中动态渲染多个绘图(如果可以跨多个选项卡执行此操作,则更好)。经过一番搜索,我发现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)循环中正确调用反应函数。错误消息是:

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

2 个答案:

答案 0 :(得分:5)

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

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 :(得分:-1)

您使用local({})代替isolate({})

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

“在调用环境中计算给予isolate()的表达式。这意味着如果在isolate()中分配一个变量,它的值将在isolate()之外可见。如果你想避免这个,你可以在isolate()中使用local()。“