使用自己的函数时,在 renderPlot 和下载处理程序中使用反应式表达式 [英] Use reactive expressions in renderPlot and download handler when using own functions

查看:101
本文介绍了使用自己的函数时,在 renderPlot 和下载处理程序中使用反应式表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 renderPlot 和下载处理程序中使用反应式表达式

我在闪亮的应用程序中在 renderPlot()downloadHandler() 中使用反应式表达式时遇到了问题.我想这样做是为了减少代码中的维护和冗余.

I have had problems by using reactive expressions both in renderPlot() and in downloadHandler() in shiny applications. I want to do this to reduce maintenance and redundancy in my code.

上述问题适用于正常"绘图函数,例如 plot、hist 等.在我的应用程序中,我使用了一个更复杂的函数来创建一个绘图.我已经创建了它的简化版本

The above issue works with "normal" plot functions like plot, hist, etc. In my application I'm using a more complex function which creates a plot. I have created a simplified version of it

helpfunc <- function(mean, sd) {
    hist(rnorm(1000, mean, sd))
    lines(1:10)
}

如果你现在在你的 shiny app 中使用这个函数,它在创建一个反应式表达式时不起作用.既不使用 plot() 也不使用反应式表达式本身.

If you use this function now in your shiny app, it does not work when creating a reactive expression out of it. Neither by using plot() nor by using the reactive expression itself.

mwe <- function() {
app = list(
    ui = bootstrapPage(
         fluidPage(
            sidebarPanel(
                 sliderInput("mean", "choose mean", -10, 10, 1),
                 sliderInput("sd", "choose sd", 0, 5, 1)),
            mainPanel(
                plotOutput("hist"),
                downloadButton("histDownload")
            )
        )
        ),
server = function(input, output) {

    output$hist <- renderPlot(.hist())

    .hist <- reactive(helpfunc(input$mean, input$sd))

    output$histDownload <- downloadHandler(
        filename = function() {
            paste("hist.jpg")
        }, 
        content = function(file) {
            jpeg(file, quality = 100, width = 800, height = 800)
            .hist() ## works not for plot(.hist()) either
            dev.off()
        }
    )

}

推荐答案

lines 基本上是对 plot.xy 的调用.除了这次您无法分配 lines 的输出之外,您遇到了与以前相同的问题.和以前一样,您可以分配 hist 函数的输出.

lines is basically a call to plot.xy. You have the same problem as you had before except this time you cannot assign the output of lines. As before you can assign the output of the hist function.

helpfunc <- function(mean, sd) {
  hist = hist(rnorm(1000, mean, sd))
  myLines = function(){lines(1:10)}
  myLines()
  list(hist = hist, lines = myLines)
}

mwe2 <- function() {


  app = list(
    ui = bootstrapPage(
      fluidPage(
        sidebarPanel(
          sliderInput("mean", "choose mean", -10, 10, 1),
          sliderInput("sd", "choose sd", 0, 5, 1)),
        mainPanel(
          plotOutput("hist"),
          downloadButton("histDownload")

        )
      )
    ),
    server = function(input, output) {

      output$hist <- renderPlot(.hist())

      .hist <- reactive(helpfunc(input$mean, input$sd))

      output$histDownload <- downloadHandler(
        filename = function() {
          paste("hist.jpg")
        }, 
        content = function(file) {
          myHist <- .hist()
          jpeg(file, quality = 100, width = 800, height = 800)
          plot(myHist$hist)
          myHist$lines()
          dev.off()
        }
      )

    }

  )
  runApp(app)
}

这篇关于使用自己的函数时,在 renderPlot 和下载处理程序中使用反应式表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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