闪亮的R渲染器飞翔上的插图 [英] Shiny R renderPlots on the fly

查看:16
本文介绍了闪亮的R渲染器飞翔上的插图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一个选项卡中动态呈现多个绘图(如果可以跨多个选项卡进行此操作会更好)。经过一番搜索,我发现这个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.

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

推荐答案

如果任何人仍对答案感兴趣,请尝试此操作:

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渲染器飞翔上的插图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆