【问题标题】:Shiny R renderPlots on the fly闪亮的 R 渲染图
【发布时间】:2015-01-11 21:56:30
【问题描述】:

我正在尝试在一个选项卡中动态呈现多个图(如果可以跨多个选项卡完成则更好)。经过一番搜索,我发现这个post 很有帮助。但就我而言,地块的数量由上传的 CSV 文件决定。所以我认为问题是如何在 for 循环中调用plotInput()$n_plot?我很感激任何建议!

此时,我可以通过调用renderUI 来创建多个<div>s

<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.

服务器.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])
            })
        })
      }
    })

【问题讨论】:

  • 在添加反应式时,没有数字。没有反应,它不起作用。这就是问题所在。
  • 我也遇到了同样的问题,请问您找到解决方案了吗?
  • @Benedikt:这有帮助吗? groups.google.com/forum/…

标签: r shiny shiny-server


【解决方案1】:

如果有人仍然对答案感兴趣,试试这个:

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

))

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2021-06-17
      • 2021-06-19
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      • 2015-10-18
      • 2016-12-15
      • 2016-12-29
      • 1970-01-01
      相关资源
      最近更新 更多