Skip to content

Commit 41f00ae

Browse files
committed
Add function saveActiveDevice()
1 parent 363b152 commit 41f00ae

3 files changed

Lines changed: 68 additions & 0 deletions

File tree

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export(load_rivers)
2525
export(load_timeframes)
2626
export(plot_empty_map)
2727
export(prepare_rivers)
28+
export(saveActiveDevice)
2829
export(site_info_from_qsimID)
2930
export(stats)
3031
export(value_to_classes)
@@ -34,8 +35,11 @@ importFrom(geosphere,distHaversine)
3435
importFrom(grDevices,col2rgb)
3536
importFrom(grDevices,dev.new)
3637
importFrom(grDevices,dev.off)
38+
importFrom(grDevices,dev.print)
39+
importFrom(grDevices,dev.size)
3740
importFrom(grDevices,png)
3841
importFrom(grDevices,rgb)
42+
importFrom(grDevices,svg)
3943
importFrom(graphics,abline)
4044
importFrom(graphics,legend)
4145
importFrom(graphics,lines)

R/saveActiveDevice.R

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#' Saves the graphic in the active device either as svg or png
2+
#'
3+
#' @param filename The Name of the file without suffix (ex. "plot")
4+
#' @param type The default "vector" saves the graphic as svg, everything else
5+
#' saves as png (ex. "")
6+
#' @param path Path where to store the file. If no path is defined, the current
7+
#' working directory is used.
8+
#' @param resolution One of "low", "medium" or "high" (only used for png files)
9+
#'
10+
#' @importFrom grDevices dev.print dev.size svg
11+
#' @export
12+
#'
13+
saveActiveDevice <- function(
14+
filename, path = NULL, type = "vector", resolution = "medium"
15+
){
16+
17+
if(type == "vector"){
18+
dev.print(
19+
device = svg,
20+
file = file.path(path, paste0(filename, ".svg")),
21+
width = dev.size("in")[1],
22+
height = dev.size("in")[2],
23+
pointsize = par("ps")
24+
)
25+
26+
} else {
27+
res <- if(resolution == "low"){
28+
200
29+
} else if(resolution == "medium"){
30+
500
31+
} else if(resolution == "high"){
32+
1000
33+
}
34+
dev.print(
35+
device = png,
36+
filename = file.path(path, paste0(filename, ".png")),
37+
width = dev.size("in")[1],
38+
height = dev.size("in")[2],
39+
units = "in", res = res
40+
)
41+
}
42+
}

man/saveActiveDevice.Rd

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)