moodymudskipper
moodymudskipper

Reputation: 47300

Create static report from dynamic shiny markdown

I have a shiny markdown app in which I have several figures, say for different days of the week. Above these figures is a text area where I write some comments.

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

I'm presenting a (mainly) reproducible example below, the first part is the code that I would like to have edited 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 the action button creates a new a Rmd file containing the code below (or an Rmd file that would create a similar output)? (change png urls to any existing file to make it reproducible).

---
#  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)

So basically the input selectors have to go, the text areas need to be changed to standard text (possibly containing markdown), and the plots have to be exported as picture files for the relevant inputs and then inserted back as pictures in the report.

Ideally I would also like to be able to export monday and tuesday into different Rmd files.

Upvotes: 4

Views: 1794

Answers (2)

Mark Peterson
Mark Peterson

Reputation: 9560

I think the easiest is to use a report template and pass in the inputs as parameters.

So, you create a new report with the filename "sampleRmdReport.Rmd" in the same directory as your Shiny report with the following contents:

---
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))
```

Then, add the following to your Shiny report:

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"
)

```

making the full file:

---
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))
})
```

Then, clicking on the "Download" button will generate the report and prompt the user to download it. Note that if you test in RStudio, the file name won't work. I suggest opening it in the browser to test that.

Then, if you want to be able to generate separate daily reports, just add a template for the report you want and a download button for each. Or, you can make your downloadHandler generate a report for each day (from templates) and put them in a zipped directory to download together.

(Note: I tend to find this is more flexible in a Shiny App than in a markdown document, particularly as that allows more control of the download button. Depending on your use case, it may be worth considering that as an approach.)

Based on the comments, here is a version that would upload to Imgur and insert the images that way. Replace the other template with this or add a second button. Note that I did not make the imgur upload function work because I do not have an API key (I assume you do, since you are planning to do it this way).

---
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 = "")

```

Upvotes: 3

NicE
NicE

Reputation: 21425

If you are ok with using ggplot you can use ggsave to save the plots locally as png.

You could try this for your main file:

---
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
})
```

And for your static report (needs to be in the same folder as the other .Rmd):

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

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

![plot](plot.png)

I'm not sure how to make a proper download button with a download handler in a shiny Rmd so this only works when run from RStudio and not in a browser.

Upvotes: 0

Related Questions