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

问题描述

我在 renderPlot()downloadHandler() 中使用相同的反应式表达式时遇到了问题.我想使用相同的反应式表达式来减少以后的维护.我创建了两个示例,它们创建了非常简单的图形(mwe1 创建了一个可以下载的 jpeg,而 mwe2 没有).我更喜欢使用创造性表达的解决方案,它可以被 renderPlot()downloadHander() 使用,就像在 mwe2 中一样.有人能帮我解决这个问题吗?

I have had an issue by using the same reactive expression in renderPlot() and in downloadHandler(). I want to use the same reactive expression to minimize maintenance later. I have created two examples which create very simple figures (mwe1 creates a jpeg which you can download, mwe2 does not). I prefer a solution which uses a creative expression which can be both used by renderPlot() and downloadHander() as in mwe2. Is anybody able to help me with this?

提前致谢!

mwe1 <- 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(rnorm(1000, input$mean, input$sd)))

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

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

    }

)
runApp(app)
}


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(hist(rnorm(1000, input$mean, input$sd)))

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

    }

)
runApp(app)
}

推荐答案

mwel2 更改为:

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(hist(rnorm(1000, input$mean, input$sd)))

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

    }

  )
  runApp(app)
}

因此在 .hist 对象上再次调用 plot.

So call plot again on the .hist object.

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

10-12 19:11