Shiny: Generating downloadable reports with plot of htmlwidget

Hi! I'm trying to make a reports from shiny app.

server.R:

    hc <- reactive({
      highchart() %>%
        hc_chart(type = "line") %>%
        hc_xAxis(cars$speed) %>%
        hc_add_series(name = "dist", data = cars$speed)
    })
    params <- reactive({
      list(
        hc = hc()
        )
    })

    output$report <- downloadHandler(
      # For PDF output, change this to "report.pdf"
      filename = "report3.pdf",
      content = function(file) {
        # Copy the report file to a temporary directory before processing it, in
        # case we don't have write permissions to the current working dir (which
        # can happen when deployed).
        tempReport <- "rmd/report3.Rmd"
        file.copy("report3.Rmd", tempReport, overwrite = TRUE)
        
        # Set up parameters to pass to Rmd document
        params <- params()
        
        # Knit the document, passing in the `params` list, and eval it in a
        # child of the global environment (this isolates the code in the document
        # from the code in this app).
        rmarkdown::render(tempReport, output_file = file, runtime = "auto",
                          params = params,
                          envir = new.env(parent = globalenv())
        )
      }
    )

report3.Rmd:

---
title: "Dynamic report"
output: pdf_document
params:
  n: NA
  hc: NA
---
```{r}
params$hc

I do not get the picture correctly. Can you please help?

Have you tried rendering the chart inside the rmd file instead of passing it as a parameter?

---
title: "Dynamic report"
output: pdf_document
params:
  data: NA
---
```{r}
highchart() %>%
        hc_chart(type = "line") %>%
        hc_xAxis(params$data) %>%
        hc_add_series(name = "dist", data = params$data)
```

In your app

rmarkdown::render(tempReport, output_file = file, runtime = "auto",
                          params = list(speed = cars$speed),
                          envir = new.env(parent = globalenv())

Thank! I found the solution.

htmlwidgets::saveWidget(widget = params$hc, file = "hc.html")
webshot::webshot(url = "hc.html", 
                 file = "hc.png", delay = 2)

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

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