This question is quite basic and related to some questions before How to save reactive plot as png to working directory in a shiny app
I had to change my strategy creating a plot from a shiny app in Rmarkdown.
For this I need to accomplish this simple task:
How can I save this plot to the temp folder as png?
Background: After saving to temp folder I will transfer it to R markdown to create a report.
library(shiny)
ui <- basicPage(
plotOutput("plot1"),
)
server <- function(input, output) {
output$plot1 <- renderPlot({
plot(mtcars$wt, mtcars$mpg)
})
}
shinyApp(ui, server)
Update: My original code looks like this. I can't provide a reproducible example with this because it is to complex:
How can I implement the answer by ismirsehregal to this code:
# plot: Radarplot ------
output$radar <- renderChartJSRadar({
chartJSRadar(PSA_13()[,c(1,2,6)],
main = "XXX",
maxScale = 100, scaleStepWidth = 10, scaleStartValue = 0, labelSize = 12,
addDots = TRUE, showToolTipLabel = TRUE, showLegend = TRUE, lineAlpha = 0.8,
polyAlpha = 0.2, responsive = FALSE,
colMatrix = col2rgb(c("orange", "navy" ,"grey")))
})
# create markdown report with radar plot ----------------------------------
output$report <- downloadHandler(
filename = "report.pdf",
content = function(file) {
td <- tempdir()
tempReport <- file.path(td, "report.Rmd")
tempLogo <- file.path(td, "logo.png")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
file.copy("logo.png", tempLogo, overwrite = TRUE)
params <- list(scores = PSA_13()[,c(1,2,6)])
rmarkdown::render(tempReport, output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
report.Rmd
---
geometry: margin=20truemm
fontfamily: mathpazo
fontsize: 11pt
documentclass: article
classoption: a4paper
urlcolor: blue
output:
pdf_document
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \rhead{\includegraphics[width = .05\textwidth]{logo.png}}
params:
scores: NA
plot_object: NA
---
\pagenumbering{gobble}
```{r setup, include=FALSE}
knitr::opts_chunk$set()
library(draw)
```
```{r rectangle, echo=FALSE}
drawBox(x =1.3, y = 3.7, width = 2.5, height = 1)
```
\vspace{-80truemm}
```{r plotout, echo=FALSE, message=FALSE, out.width='100%'}
params$plot_object
```
<!-- ```{r, echo=FALSE, out.width="100%", } -->
<!-- chartJSRadar(params$scores, width = 700, height = 700, -->
<!-- main = "Peritoneal Surface Calculator Radarchart", -->
<!-- maxScale = 100, -->
<!-- scaleStepWidth = 10, -->
<!-- scaleStartValue = 0, -->
<!-- labelSize = 14, -->
<!-- addDots = TRUE, -->
<!-- showToolTipLabel = FALSE, -->
<!-- showLegend = TRUE, -->
<!-- lineAlpha = 0.8, -->
<!-- polyAlpha = 0.2, -->
<!-- responsive = FALSE, -->
<!-- colMatrix = col2rgb(c("orange", "navy" ,"grey"))) -->
<!-- ``` -->
I feel quite near to the solution and I am very grateful for your time!