downPlot.R 1.87 KB
Newer Older
dmattek's avatar
dmattek committed
1
2
3
4
5
#
# Time Course Inspector: Shiny app for plotting time series data
# Author: Maciej Dobrzynski
#
# This module is for downloading pdf of the plot
dmattek's avatar
dmattek committed
6
7
8
9
10
11
12
# Use:
# in ui.R
# downPlotUI('uniqueID', "your_label")
#
# in server.R
# callModule(downPlot, "uniqueID", 'fname.pdf', input_plot_to_save)

dmattek's avatar
dmattek committed
13
# UI ----
dmattek's avatar
dmattek committed
14
15
16
17
18
19
20
21
22
23
24
25
downPlotUI <- function(id, label = "Download Plot") {
  ns <- NS(id)
  
  tagList(
    # Label to display as h4 header
    h4(label),
    
    fluidRow(
      column(
        3,
        numericInput(
          ns('inPlotWidth'),
26
27
          "Width (in)",
          8.5,
dmattek's avatar
dmattek committed
28
29
30
31
32
33
34
35
          min = 1,
          width = 100
        )
      ),
      column(
        3,
        numericInput(
          ns('inPlotHeight'),
36
37
          "Height (in)",
          11,
dmattek's avatar
dmattek committed
38
39
40
41
42
          min = 1,
          width = 100
        )
      ),
      column(6,
dmattek's avatar
dmattek committed
43
             uiOutput(ns('uiDownButton')))
dmattek's avatar
dmattek committed
44
45
46
47
    )
  )
}

dmattek's avatar
dmattek committed
48
49
# SERVER ----

dmattek's avatar
dmattek committed
50
51
downPlot <- function(input, output, session, in.fname, in.plot, in.gg = FALSE) {

dmattek's avatar
dmattek committed
52
53
54
  output$uiDownButton = renderUI({
    ns <- session$ns
    
55
    if (in.fname() %like% 'pdf') {
dmattek's avatar
dmattek committed
56
57
58
59
60
61
62
      downloadButton(ns('downPlot'), 'PDF')
    } else {
      downloadButton(ns('downPlot'), 'PNG')
    }
    
  })
  
dmattek's avatar
dmattek committed
63
64
  output$downPlot <- downloadHandler(
    filename = function() {
65
66
      cat(in.fname(), "\n")
      in.fname()
dmattek's avatar
dmattek committed
67
68
69
70
71
72
73
74
75
76
77
78
    },
    
    content = function(file) {
      if (in.gg) {
        ggsave(
          file,
          limitsize = FALSE,
          in.plot(),
          width  = input$inPlotWidth,
          height = input$inPlotHeight
        )
      } else {
79
        if (in.fname() %like% 'pdf') {
dmattek's avatar
Mod:    
dmattek committed
80
81
82
83
84
85
86
87
88
          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
89
90
91
92
93
94
95
96
        
        in.plot()
        dev.off()
      }
    }
  )
  
}