簡體   English   中英

實時運行閃亮的R renderPlot

[英]Shiny R renderPlots on the fly

我正在嘗試在一個選項卡中動態呈現多個圖(最好是在多個選項卡之間完成此操作)。 經過一番搜索,我發現這篇文章很有幫助。 但就我而言,地塊數由上傳的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.

服務器

  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 ,則該循環有效,但是該圖丟失了,我猜這是因為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))
        })
      })
    })
  }

))

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

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

“給isolate()的表達式是在調用環境中求值的。這意味着,如果在isolate()內分配一個變量,則其值將在isolate()之外可見。如果要避免這種情況,您可以在isolate()中使用local()。”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM