Creating a static report from a dynamic shiny markdown

I have an application shiny markdownin which I have several digits, for example, for different days of the week. Above these numbers is the text area where I am writing some comments.

I want to be able to export this report to a format static markdown.

I present the (mostly) reproducible example below, the first part is the code that I would like to edit so that it creates the code from the second part in a separate file.

---
title: "WEEKLY REPORT"
runtime: shiny
output: html_document
---

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

```{r header, echo=FALSE}
selectInput("x", label = "x",choices = 1:10,width="100%")
actionButton("button", "Export report")
```

## Monday

```{r monday}
textAreaInput("mon", label = NULL)
renderPlot({
plot(log(1:input$x))
})
```

## Tuesday

```{r tuesday}
textAreaInput("tue", label = NULL)
renderPlot({
plot(sin(1:input$x))
})
```

How can I edit it so that the action button creates a new Rmd file containing the code below (or an Rmd file that would produce similar output)? (change png urls to any existing file to make it playable).

---
#  title: "WEEKLY REPORT"
output: html_document
---

## Monday

The text I would have put on the first box

![](plot_monday.png)    

## Tuesday

The text I would have put on the second box

![](plot_tuesday.png)

, , (, ), , .

Rmd.

+4
2

, .

, sampleRmdReport.Rmd , :

---
title: "Weekly Report"
author: "Moody_Mudskipper"
date: '`r format(Sys.Date(),"%Y-%B-%d")`'
output: html_document
params:
  mondayText: "holder"
  tuesdayText: "holder"
  x: "holder"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, results = "asis")
```
# Monday

```{r}
cat(params$mondayText)
```

```{r}
plot(log(1:params$x))
```

# Tuesday

```{r}
cat(params$tuesdayText)
```

```{r}
plot(sin(1:params$x))
```

:

Download the weekly report:

```{r}
downloadHandler(
  filename = function(){
    paste0("weeklyReport_generated_"
           , format(Sys.Date(), "%Y%b%d")
           , ".html")
  }
  , content = function(file){
    rmarkdown::render(input = "sampleRmdReport.Rmd"
                      , output_file = file
                      , params = list(mondayText = input$mon
                                      , tuesdayText = input$tue
                                      , x = input$x
    ))
  }
  , contentType = "text/html"
)

```

:

---
title: "WEEKLY REPORT"
runtime: shiny
output: html_document
---

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



```

```{r header}
selectInput("x", label = "x",choices = 1:10,width="100%")
```

Download the weekly report:

```{r}
downloadHandler(
  filename = function(){
    paste0("weeklyReport_generated_"
           , format(Sys.Date(), "%Y%b%d")
           , ".html")
  }
  , content = function(file){
    rmarkdown::render(input = "sampleRmdReport.Rmd"
                      , output_file = file
                      , params = list(mondayText = input$mon
                                      , tuesdayText = input$tue
                                      , x = input$x
    ))
  }
  , contentType = "text/html"
)

```




## Monday

```{r monday}
textAreaInput("mon", label = NULL)

renderPlot({
  plot(log(1:input$x))
})


```


## Tuesday

```{r tuesday}
textAreaInput("tue", label = NULL)

renderPlot({
  plot(sin(1:input$x))
})
```

, "", . , RStudio, . , .

, , . downloadHandler ( ) zipped- .

(. , Shiny App, , , . , , , .)

, , Imgur . . , imgur, API ( , , ).

---
title: "Weekly Report"
author: "Moody_Mudskipper"
date: '`r format(Sys.Date(),"%Y-%B-%d")`'
output:
  html_document:
    self_contained: false
params:
  mondayText: "holder"
  tuesdayText: "holder"
  x: 1
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, results = "asis")
library(ggplot2)
tempDir <- tempdir()

uploadImgur <- function(fileName){
  # This function would need to upload with the Imgur API
  # I don't have a key, and don't want to set one up
  # for this example.

  # It would return the url that imgur assigns the image
  # here, I am using a placeholder
  outUrl <- "https://i.imgur.com/WsUV4DK.gif"

  return(outUrl)
}
```

# Monday

```{r}
cat(params$mondayText)
```


```{r}
tempPlot <-
  ggplot(mapping = aes(x = 1:params$x
                       , y = log(1:params$x))) +
  geom_point() +
  xlab("X") +
  ylab("Y")

tempFile <- tempfile("plot_", tempDir, ".png")
ggsave(tempFile, tempPlot, width = 4, height = 4)

imgurURL <- uploadImgur(tempFile)

cat("![](", imgurURL,")", sep = "")
```



# Tuesday

```{r}
cat(params$tuesdayText)
```


```{r}
tempPlot <-
  ggplot(mapping = aes(x = sin(1:params$x)
                       , y = log(1:params$x))) +
  geom_point() +
  xlab("X") +
  ylab("Y")

tempFile <- tempfile("plot_", tempDir, ".png")
ggsave(tempFile, tempPlot, width = 4, height = 4)

imgurURL <- uploadImgur(tempFile)

cat("![](", imgurURL,")", sep = "")

```
+1

ggplot, ggsave, png.

:

---
title: "WEEKLY REPORT"
runtime: shiny
output: html_document
---

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

```{r header, echo=FALSE}
selectInput("x", label = "x",choices = 1:10,width="100%")
actionButton("button", "Export report")
observeEvent(input$button,{
  params <- list(text = input$mon)
  render('report.Rmd',params = params)
})
```

## Monday

```{r monday}
textAreaInput("mon", label = NULL)
renderPlot({
  p <- ggplot(data.frame(x=1:input$x,y=log(1:input$x)),aes(x=x,y=y))+
    geom_point()   
  ggsave("plot.png",plot=p)
  p
})
```

( , .Rmd):

---
#  title: "WEEKLY REPORT"
output: html_document
---

## Monday
```{r}
params$text
```

![plot](plot.png)

, Rmd, RStudio, .

0

Source: https://habr.com/ru/post/1686724/


All Articles