-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidgetDownloadablePlot.R
More file actions
195 lines (165 loc) · 7.96 KB
/
widgetDownloadablePlot.R
File metadata and controls
195 lines (165 loc) · 7.96 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#'
#' Contains a plot output with download buttons
#'
library(DT)
library(shiny)
source("uiHelper.R")
source("classPlotSettings.R")
#' Creates a UI with a plot output and download buttons
#'
#' @param id ID of the control
#' @param custom.header.items Additional items to include into header bar
#' @param custom.export.items Additional items in the export menu
#' @param settings.panel An additional panel with settings that can be expanded
#'
#' @return
#' @export
#'
#' @examples
downloadablePlotOutput <- function(id,
custom.header.items = NULL,
custom.export.items = NULL,
settings.panel = NULL,
settings.panel.label = "Settings") {
if(!is.character(id)) {
stop("Invalid arguments!")
}
ns <- NS(id)
export.items <- tagList(
downloadButton(ns("export.svg"), "as *.svg"),
downloadButton(ns("export.png"), "as *.png"),
downloadButton(ns("export.pdf"), "as *.pdf"),
downloadButton(ns("export.tiff"), "as *.tiff")
)
if(!is.null(custom.export.items)) {
export.items <- tagAppendChildren(export.items, list = custom.export.items)
}
settings.button <- if(!is.null(settings.panel)) { bsButton(ns("settings"), settings.panel.label, icon = icon("cog"), type = "toggle") } else NULL
return(tags$div(class = "downloadable-plot",
headerPanel(header = tags$span(class="headerbar-row",
settings.button,
dropdownButton(ns("menu.export"), "Export image",
icon = icon("download"),
export.items),
custom.header.items,
conditionalPanel(conditionalPanel.equals(ns("settings"), "true"),
tags$div(class = "settings-panel",
hDivider(),
settings.panel)),
bsButton(ns("collapse"), "Collapse", icon = icon("eye-slash"), type = "toggle")
),
conditionalPanel(conditionalPanel.equals(ns("collapse"), "false"), uiOutput(ns("plot.container")))
# plotOutput(ns("plot"))
)))
}
#' Plots a downloadable plot
#' This function is supposed to be called by callModule. Use the one without an underscore for easier access.
#'
#' @param input
#' @param output
#' @param session
#' @param exprplot Function that takes width, height, format and filename and saves a plot to filename. Optionally returns plot settings object
#' @param plot.settings Reactive that returns a plot settings object
#' @param render.format Format of data sent to user (default: png)
#' @param render.alt Alt text for img object (default: "Plot")
#' @param export.filename Base filename of exported plot
#'
#' @return
#' @export
#'
#' @examples
downloadablePlot_ <- function(input,
output,
session,
exprplot,
plot.settings = reactive({ PlotSettings() }),
render.format = "png",
render.alt = "Plot",
export.filename = "plot") {
out.width <- reactive({ session$clientData[[paste0("output_", session$ns("plot"), "_width")]] })
out.height <- reactive({ session$clientData[[paste0("output_", session$ns("plot"), "_height")]] })
# If the user didn't set any output sizes, overwrite them here
plot.settings.defaults <- reactive({
return(plotSettingsSetNA(plot.settings(), PlotSettings(width = out.width(), height = out.height(), dpi = default.plot.dpi, scale = 1) ))
})
output$plot.container <- renderUI({
width <- "100%"
height <- "700px"
if(!is.na(plot.settings()@width)) {
width <- paste0(plot.settings()@width, "px")
}
if(!is.na(plot.settings()@height)) {
height <- paste0(plot.settings()@height, "px")
}
return(plotOutput(session$ns("plot"), width = width, height = height))
})
output$plot <- renderImage({
validate(need(out.width(), "No output width!"),
need(out.height(), "No output height!"))
out.file <- tempfile(fileext=paste0(".", render.format))
plot.settings.output <- exprplot(plot.settings = plot.settings.defaults(), filename = out.file, format = render.format)
width <- out.width()
height <- out.height()
if(!is.null(plot.settings.output)) {
if(!is(plot.settings.output, "PlotSettings")) {
stop("Return value of exprplot must be NULL or a PlotSettings object!")
}
if(!is.na(plot.settings.output@width)) {
width <- plot.settings.output@width
}
if(!is.na(plot.settings.output@height)) {
height <- plot.settings.output@height
}
}
return(list(src = out.file,
width = width,
height = height,
alt = render.alt))
})
output$export.svg <- downloadHandler(filename = paste0(export.filename, ".svg"),
content = function(file) {
exprplot(plot.settings = plot.settings.defaults(), filename = file, format = "svg")
},
contentType = "image/svg")
output$export.png <- downloadHandler(filename = paste0(export.filename, ".png"),
content = function(file) {
exprplot(plot.settings = plot.settings.defaults(), filename = file, format = "png")
},
contentType = "image/png")
output$export.pdf <- downloadHandler(filename = paste0(export.filename, ".pdf"),
content = function(file) {
exprplot(plot.settings = plot.settings.defaults(), filename = file, format = "pdf")
},
contentType = "application/pdf")
output$export.tiff <- downloadHandler(filename = paste0(export.filename, ".tiff"),
content = function(file) {
exprplot(plot.settings = plot.settings.defaults(), filename = file, format = "tiff")
},
contentType = "image/tiff")
}
#' Plots a downloadable plot
#'
#' @param exprplot Function that takes width, height, format and filename and saves a plot to filename
#' @param plot.settings Reactive that returns a plot settings object. Optionally returns plot settings object.
#' @param render.format Format of data sent to user (default: png)
#' @param render.alt Alt text for img object (default: "Plot")
#' @param export.filename Base filename of exported plot
#'
#' @return
#' @export
#'
#' @examples
downloadablePlot <- function(id,
exprplot,
plot.settings = reactive({ PlotSettings() }),
render.format = "png",
render.alt = "Plot",
export.filename = "plot") {
return(callModule(downloadablePlot_,
id,
exprplot = exprplot,
plot.settings = plot.settings,
render.format = render.format,
render.alt = render.alt,
export.filename = export.filename))
}