Rmarkdown document download plot not updating when control changed in modularised app

Hi there,
I am trying to get a downloaded document to update when a control is changed and the download button pressed. When I did this without using modules it worked fine, but now I am having problems as the plot is not updating when the control is changed and the download button clicked.

It does download the Word document, but the plot in the download remains the same as when it is first downloaded. Does anyone know why the plot is not updating in the document, even though it is on screen? I have included (my attempt at) some minimal code below to demonstrate the issue...

library(shiny)

commonUI <- function(id) {
  ns <- NS(id)
  tagList(
    downloadButton(ns('report')),
    selectInput(ns('samples'), 'Samples to plot', c(1:100)),
    plotOutput('splotout')
  )
}

rep <- function(input, output, session, fname, plot, pheight){
  output$report <- downloadHandler(
    filename = fname,
    content = function(file) {
      td <-tempdir()
      tempReport <- file.path(td, "report.Rmd")
      file.copy("report.Rmd", tempReport, overwrite = TRUE)
      params <- list(plot = plot, height = pheight)
      render(tempReport, output_file = file, params = params, envir = new.env(parent = globalenv()))
    })
}

sdata <- function(input, output, session, fname, plot, pheight){
  x <- reactive(data.frame(x = c(1:input$samples), y = rnorm(input$samples, 0, 1)))
}
  
ui <- fluidPage(
       commonUI('tab1')
      )

server <- function(input, output, session){
  
  callModule(rep, 'tab1', fname = 'Summary.docx', plot = splot(), pheight = 10)
  
  sdat <- callModule(sdata, 'tab1')
  
  splot <- reactive({
    data <- sdat()
    ggplot(data, aes(x=x, y=y)) + geom_bar(stat = 'identity')
  })

   output$splotout <- renderPlot(splot())

}

shinyApp(ui, server)

The Rmarkdown file I'm using (report.Rmd) is like this (I've edited the R chunks slightly wrongly so it displays properly here)...

---
output: 
  word_document:
date: "`r format(Sys.Date(),'%d/%m/%Y')`"
params:
  plot: NA
  height: NA
---
```{r, include=FALSE}
	knitr::opts_chunk$set(echo = FALSE)```
```{r plot, dpi = 200, fig.width = 10, fig.height=params$height, echo=FALSE}
	params$plot```

Any help would be greatly appreciated! :slight_smile:
Regards,
Will

I found out what the problem was. Usually, I call modules by passing variables from other modules like this (notice the brackets after splot):

callModule(rep, 'tab1', fname = 'Summary.docx', plot = splot(), pheight = 10)

...but for some reason, to get the module to be reactive, the parameter needs to have the reactive style brackets in the downloadHandler params, and so they must be taken out of the parameter passed as plot in the call to the module:

rep <- function(input, output, session, fname, plot, pheight){
  output$report <- downloadHandler(
    filename = fname,
    content = function(file) {
      td <-tempdir()
      tempReport <- file.path(td, "report.Rmd")
      file.copy("report.Rmd", tempReport, overwrite = TRUE)
      params <- list(plot = plot(), height = pheight)
      render(tempReport, output_file = file, params = params, envir = new.env(parent = globalenv()))
    })
}

callModule(rep, 'tab1', fname = 'Summary.docx', plot = splot, pheight = 10)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.