Site icon R-bloggers

Shiny plots PDF export

[This article was first published on R – MYHAPPYDATA BLOG, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Sometimes it is useful to build a pdf export option into a Shiny app. I built a basic app at first (every important notes was included as comment) to show how to do it easily:

server.r

 
library(shiny)
library(Cairo)
 
server <- function(input, output) {
 
  ### create the plots. 
  # No need to duplicate the plot functions if You use this kind of form
  hist_plot <- function(){
    hist(mtcars$mpg, main = "")
  }
 
  scatter_plot <- function(){
    plot(mtcars$disp, mtcars$hp)
  }
 
  box_plot <- function(){
    boxplot(mtcars$drat)
  }
 
  ### make UI objects
  output$exp_plot1 <- renderPlot({
    hist_plot()
  })
 
  output$exp_plot2 <- renderPlot({
    scatter_plot()
  })
 
  output$exp_plot3 <- renderPlot({
    box_plot()
  })
 
  ### function to do the pdf export
  output$export <- downloadHandler("test.pdf", function(theFile) {
 
    makePdf <- function(filename){
      # I use Cairo instead of the the basic pdf function. It is much more complex and it supports special characters like "ű"  
      Cairo(type = 'pdf', file = filename, width = 21, height = 29.7, units='cm', bg='transparent')
 
      # configure the layout like this
      ################
      # plot1 plot2  #
      # plot3        #
      ################
      lo = matrix(c(1,2,3,4),2,2)
      layout(lo)
 
        # call the plots
        hist_plot()
        scatter_plot()
        box_plot()
 
      dev.off()
    }
 
    makePdf(theFile)
  })
 
}

ui.r

 
library(shiny)
library(ggplot2)
 
ui <- fluidPage(
  downloadButton("export", label = "download content"),
  titlePanel("PDF export example"),
 
  mainPanel(
    plotOutput(outputId = "exp_plot1"),
    plotOutput(outputId = "exp_plot2"),
    plotOutput(outputId = "exp_plot3")
  )  
)

You can also export the ggplot2 type plots like this:

server.r

 
library(shiny)
library(Cairo)
library(ggplot2)
library(gridExtra)
 
server <- function(input, output) {
 
  ### create the plots. 
  # No need to duplicate the plot functions if You use this kind of form
  hist_plot <- function(){
    ggplot(mtcars, aes(mpg)) + geom_histogram()
  }
 
  scatter_plot <- function(){
    ggplot(mtcars, aes(x=disp, y=hp)) + geom_point()
  }
 
  box_plot <- function(){
    ggplot(mtcars, aes(x=factor(""), y = drat)) + geom_boxplot()
  }
 
  ### make UI objects
  output$exp_plot1 <- renderPlot({
    hist_plot()
  })
 
  output$exp_plot2 <- renderPlot({
    scatter_plot()
  })
 
  output$exp_plot3 <- renderPlot({
    box_plot()
  })
 
  ### function to do the pdf export
  output$export <- downloadHandler("test.pdf", function(theFile) {
 
    makePdf <- function(filename){
      # I use Cairo instead of the the basic pdf function. It is much more complex and it supports special characters like "ű"  
      Cairo(type = 'pdf', file = filename, width = 21, height = 29.7, units='cm', bg='transparent')
 
        p1 <- hist_plot()
        p2 <- scatter_plot()
        p3 <- box_plot()
 
        # configure the layout like this
        ################
        # plot1 plot2  #
        # plot3        #
        ################
        p <- grid.arrange(p1, p2, p3, ncol=2)
 
        print(p)
 
      dev.off()
    }
 
    makePdf(theFile)
  })
 
}

final notes: If You would like to use Cairo on Ubuntu You need to run this commands at first:
sudo apt-get install libcairo2-dev sudo apt-get install libxt-dev

To leave a comment for the author, please follow the link and comment on their blog: R – MYHAPPYDATA BLOG.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.