我想知道在使用閃亮生成可下載報告時是否可以將繪圖物件傳遞給 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)
)
uj5u.com熱心網友回復:
您可以將生成的 plotly 物件包裝在reactive或reactiveVal將其傳遞給outputand 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)
另一種方法是使用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)
uj5u.com熱心網友回復:
讓我們有檔案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
```
app.R以及同一目錄中的另一個檔案:
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)
然后您可以在單擊按鈕后下載報告。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/435625.html
