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!
Regards,
Will