【问题标题】:R/Shiny App Writes Plots to Plot View in RStudio Instead of Shiny UIR/Shiny App 将绘图写入 RStudio 中的绘图视图而不是 Shiny UI
【发布时间】:2021-12-14 00:57:43
【问题描述】:

我有一个带有 plotOutput 组件的 Shiny 应用程序:

  plotOutput("plot_data")

使用 vis_dat 库,我写到 plot_data 如下:

  observeEvent(input$missing, { # Click the Missing actionButton
    v$plot_data <- vis_miss(new_data())
    })  

还有……

  output$plot_data <- renderPlot({
    if (is.null(v$plot_data)) return()
    v$plot_data
  })

它工作正常。现在,我也(尝试)使用pairs() 函数写入同一个组件,如下所示:

  observeEvent(input$pairs, {  # Click the Pairs actionButton
    v$plot_data <- pairs(new_data()) 
    })

我使用与前面所示相同的响应式 output$plot_data 调用,但是这一次,pairs() 输出将绘图写入 RStudio 绘图面板而不是 Shiny UI。我在其他类型的情节中也看到了这一点。知道为什么会这样吗?

【问题讨论】:

    标签: r plot shiny rstudio reactive


    【解决方案1】:

    R 中的图表函数,例如 hist()plot()pairs() 渲染图像,而不是绘图。所以我使用renderImage()函数如下:

      output$image_data <- renderImage({
        width  <- session$clientData$output_image_data_width
        height <- session$clientData$output_image_data_height
        
        outfile <- tempfile(fileext='.png')
        png(outfile, width=width, height=height)
        plot(new_data(), pch=20 , cex=1.5 , col="#69b3a2")
        dev.off()
        
        list(
          src = outfile,
          width = width,
          height = height
          )
        }, 
      deleteFile = TRUE
      )
    

    在 RStudio 中,一切看起来都像绘图,但回报不相等。

    【讨论】:

      猜你喜欢
      • 2014-08-03
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2020-07-23
      • 1970-01-01
      • 2014-04-19
      • 2016-07-20
      • 2015-02-25
      相关资源
      最近更新 更多