downPlot.R 1.51 KB
Newer Older
dmattek's avatar
dmattek committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# RShiny module for downloading pdf of the plot
# Use:
# in ui.R
# downPlotUI('uniqueID', "your_label")
#
# in server.R
# callModule(downPlot, "uniqueID", 'fname.pdf', input_plot_to_save)


downPlotUI <- function(id, label = "Download Plot") {
  ns <- NS(id)
  
  tagList(
    # Label to display as h4 header
    h4(label),
    
    fluidRow(
      column(
        3,
        numericInput(
          ns('inPlotWidth'),
          "Width",
          17,
          min = 1,
          width = 100
        )
      ),
      column(
        3,
        numericInput(
          ns('inPlotHeight'),
          "Height",
          10,
          min = 1,
          width = 100
        )
      ),
      column(6,
             downloadButton(ns('downPlot'), 'PDF'))
    )
  )
}

downPlot <- function(input, output, session, in.fname, in.plot, in.gg = FALSE) {

  output$downPlot <- downloadHandler(
    filename = function() {
      in.fname
    },
    
    content = function(file) {
      if (in.gg) {
        ggsave(
          file,
          limitsize = FALSE,
          in.plot(),
          width  = input$inPlotWidth,
          height = input$inPlotHeight
        )
      } else {
dmattek's avatar
Mod:    
dmattek committed
61
62
63
64
65
66
67
68
69
70
        if (in.fname %like% 'pdf') {
          pdf(file,
              width  = input$inPlotWidth,
              height = input$inPlotHeight)
        } else {
          png(file,
              width  = input$inPlotWidth,
              height = input$inPlotHeight, units = 'in', res = 300)
        }
        
dmattek's avatar
dmattek committed
71
72
73
74
75
76
77
78
        
        in.plot()
        dev.off()
      }
    }
  )
  
}