Shiny:生成可下载报告时将绘图变量传递给 Rmarkdown 文档

发布于 2025-01-09 12:28:32 字数 610 浏览 1 评论 0原文

我想知道在使用闪亮生成可下载报告时是否可以将绘图对象传递到 Rmarkdown 文档?

例如,我有一个 P2 plotly 对象,我想将它传递给 Rmarkdown 文档。

我知道我可以将代码放入 Rmarkdown 并生成绘图,但我不在这里这样做

output$Plot <- plotly::renderPlotly({
p <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width)) +
      geom_point(aes(color=Species, shape=Species)) +
      labs(title = "Iris sepal width vs length")
P2 <- ggplotly(p)
})

如何在 downloadHandler() 中传递它?

output$report <- downloadHandler(

 params <- list(myPlot = p2)

)

I am wondering if it is possible to pass a plot object to Rmarkdown document when generating a downloadable report using shiny?

For instance,I have a P2 plotly object, I want to pass it to Rmarkdown document.

I know I can put the code in Rmarkdown and generate the plot, but I don't to do it here

output$Plot <- plotly::renderPlotly({
p <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width)) +
      geom_point(aes(color=Species, shape=Species)) +
      labs(title = "Iris sepal width vs length")
P2 <- ggplotly(p)
})

How can I pass it in downloadHandler() ?

output$report <- downloadHandler(

 params <- list(myPlot = p2)

)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昨迟人 2025-01-16 12:28:32

reactiveVal 中,以将其传递给 outputdownloadHandler

library(shiny)
library(plotly)

writeLines(con = "report.Rmd", text = "---
title: 'Plotly report'
output: html_document
params:
  plotly_object: NA
---

```{r plotlyout, echo=FALSE, message=FALSE, out.width='100%'}
params$plotly_object
```")

ui = fluidPage(
  plotlyOutput("Plot"),
  downloadButton("report_button", "Generate report")
)

server = function(input, output, session) {
  irisPlot <- reactive({
    p <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width)) +
      geom_point(aes(color=Species, shape=Species)) +
      labs(title = "Iris sepal width vs length")
    ggplotly(p)
  })

  output$Plot <- plotly::renderPlotly({
    irisPlot()
  })

  output$report_button <- downloadHandler(
    filename = "report.html",
    content = function(file) {
      tempReport <- tempfile(fileext = ".Rmd") # make sure to avoid conflicts with other shiny sessions if more params are used
      file.copy("report.Rmd", tempReport, overwrite = TRUE)
      rmarkdown::render(tempReport, output_format = "html_document", output_file = file, output_options = list(self_contained = TRUE),
                        params = list(plotly_object = irisPlot())
      )
    }
  )
}

shinyApp(ui, server)

您可以将生成的绘图对象包装在 reactive方法是使用 htmlwidgets::saveWidget (如果您只需要 html 图):

library(shiny)
library(plotly)

ui = fluidPage(
  plotlyOutput("Plot"),
  downloadButton("report_button", "Generate report") 
)

server = function(input, output, session) {
  irisPlot <- reactive({
    p <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width)) +
      geom_point(aes(color=Species, shape=Species)) +
      labs(title = "Iris sepal width vs length")
    ggplotly(p)
  })
  
  output$Plot <- plotly::renderPlotly({
    irisPlot()
  })
  
  output$report_button <- downloadHandler(
    filename = "report.html",
    content = function(file) {
      htmlwidgets::saveWidget(widget = partial_bundle(irisPlot()), file = file, selfcontained = TRUE)
    }
  )
}

shinyApp(ui, server)

You can wrap the resulting plotly object in reactive or reactiveVal to pass it to the output and the downloadHandler:

library(shiny)
library(plotly)

writeLines(con = "report.Rmd", text = "---
title: 'Plotly report'
output: html_document
params:
  plotly_object: NA
---

```{r plotlyout, echo=FALSE, message=FALSE, out.width='100%'}
params$plotly_object
```")

ui = fluidPage(
  plotlyOutput("Plot"),
  downloadButton("report_button", "Generate report")
)

server = function(input, output, session) {
  irisPlot <- reactive({
    p <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width)) +
      geom_point(aes(color=Species, shape=Species)) +
      labs(title = "Iris sepal width vs length")
    ggplotly(p)
  })

  output$Plot <- plotly::renderPlotly({
    irisPlot()
  })

  output$report_button <- downloadHandler(
    filename = "report.html",
    content = function(file) {
      tempReport <- tempfile(fileext = ".Rmd") # make sure to avoid conflicts with other shiny sessions if more params are used
      file.copy("report.Rmd", tempReport, overwrite = TRUE)
      rmarkdown::render(tempReport, output_format = "html_document", output_file = file, output_options = list(self_contained = TRUE),
                        params = list(plotly_object = irisPlot())
      )
    }
  )
}

shinyApp(ui, server)

Another approach is using htmlwidgets::saveWidget (If you only need the html plot):

library(shiny)
library(plotly)

ui = fluidPage(
  plotlyOutput("Plot"),
  downloadButton("report_button", "Generate report") 
)

server = function(input, output, session) {
  irisPlot <- reactive({
    p <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width)) +
      geom_point(aes(color=Species, shape=Species)) +
      labs(title = "Iris sepal width vs length")
    ggplotly(p)
  })
  
  output$Plot <- plotly::renderPlotly({
    irisPlot()
  })
  
  output$report_button <- downloadHandler(
    filename = "report.html",
    content = function(file) {
      htmlwidgets::saveWidget(widget = partial_bundle(irisPlot()), file = file, selfcontained = TRUE)
    }
  )
}

shinyApp(ui, server)

拥有文件doc.Rmd

---
title: "Untitled"
author: "danlooo"
date: "2/22/2022"
params:
  plot:
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Plot

```{r}
params$plot
```

让我们在同一目录中

library(shiny)
library(plotly)
library(tidyverse)
library(rmarkdown)

ui <- fluidPage(
  plotlyOutput("plot"),
  downloadButton("download")
)

server <- function(input, output, session) {
  plot <- reactive({
    p <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
      geom_point(aes(color = Species, shape = Species)) +
      labs(title = "Iris sepal width vs length")

    ggplotly(p)
  })

  output$plot <- renderPlotly(plot())

  output$download <- downloadHandler(
    filename = "report.html",
    content = function(file) {
      render(input = "doc.Rmd", output_dir = dirname(file), 
       output_file = basename(file), params = list(plot = plot()))
    }
  )
}


shinyApp(ui, server)

和另一个文件app.R:然后您可以在单击按钮后下载报告。

Lets have files doc.Rmd:

---
title: "Untitled"
author: "danlooo"
date: "2/22/2022"
params:
  plot:
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Plot

```{r}
params$plot
```

And another file app.R in the same directory:

library(shiny)
library(plotly)
library(tidyverse)
library(rmarkdown)

ui <- fluidPage(
  plotlyOutput("plot"),
  downloadButton("download")
)

server <- function(input, output, session) {
  plot <- reactive({
    p <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
      geom_point(aes(color = Species, shape = Species)) +
      labs(title = "Iris sepal width vs length")

    ggplotly(p)
  })

  output$plot <- renderPlotly(plot())

  output$download <- downloadHandler(
    filename = "report.html",
    content = function(file) {
      render(input = "doc.Rmd", output_dir = dirname(file), 
       output_file = basename(file), params = list(plot = plot()))
    }
  )
}


shinyApp(ui, server)

Then you can downlaod the report after clicking the button.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文