Alex
Alex

Reputation: 15708

Print/save interactive report created using Rmarkdown and Shiny

I have an interactive Rmarkdown document which embeds a few shiny apps that generates graphs based on user inputs.

After I am happy with the graphs generated, I would like to save the report (so that it becomes static).

I have opened the report in my browser (Chrome), and tried printing to pdf. It sort of works, however some figures are cut into two by the page break.

Does anyone know what is the best way to print/save such reports?

Upvotes: 2

Views: 1083

Answers (2)

Syamanthaka
Syamanthaka

Reputation: 1327

You can give a download button on the ui.R as:

downloadButton('report', 'Download PDF')

And the corresponding server.R code:

library(knitr)
output$report = downloadHandler(
      filename = function() {
        paste("Report_", <date/identifier>, ".pdf", sep="")
      },

      content = function(file) {
        rnw <- normalizePath('input.rnw') # assuming you wrote the code to display the graphs etc in this file
        owd <- setwd(tempdir())
        on.exit(setwd(owd))
        file.copy(rnw, 'input.rnw')

        out = knit2pdf(rnw, clean=TRUE)
        file.rename(out, file) 
      }
    )

Upvotes: 0

Christophe D.
Christophe D.

Reputation: 1089

I think it's a bit tricky but this is what i use on my app to save html plot on pdf or png format.

Instal wkhtmltopdf

wkhtmltopdf and wkhtmltoimage are open source (LGPLv3) command line tools to render HTML into PDF and various image formats using the Qt WebKit rendering engine. These run entirely "headless" and do not require a display or display service.

Use it in R

This allow you to convert a htmlfile into pdfor img.
In my ShinyApp i use inside a downloadHandler() function somthing like this :

system("wkhtmltopdf   --enable-javascript --javascript-delay 2000 plot.html plot.pdf")  

I think for your example you could simply convert your html by using :

system("wkhtmltopdf yourFile.html yourFile.pdf")

Upvotes: 2

Related Questions