-
Notifications
You must be signed in to change notification settings - Fork 0
/
printPdfPng.R
70 lines (65 loc) · 2.64 KB
/
printPdfPng.R
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
#' printPdfPng
#'
#' This function prints figure to pdf or png.
#'
#' @param figure Default=NULL. Figure to be printed
#' @param dir Default = getwd(). Directory to print figure
#' @param filename Default = "plot". File name
#' @param width Default=13.
#' @param height Default=9.
#' @param pdfpng Default="png". Either "pdf" or "png"
#' @param transparent Default=F
#' @keywords charts, diffplots
#' @return Prints out graphic
#' @export
#-------------
# Print to PDF or PNG
#-------------
printPdfPng <- function(figure = NULL,
dir = getwd(),
filename = "plot",
width = 13,
height = 9,
pdfpng="png",
transparent=F){
if(is.null(figure)){rlang::inform("No figure provided.")}else{
if(pdfpng=='pdf'){
grDevices::pdf(paste(dir,"/",filename,".pdf",sep=""),width=width,height=height)
print(figure)
grDevices::dev.off()
rlang::inform(gsub("//","/",paste("Figure saved as: ",dir,"/",filename,".pdf", sep="")))
}
if(pdfpng=='png'){
if(transparent){
grDevices::png(paste(dir,"/",filename,".png",sep=""),bg="transparent", width=width,height=height, units="in",res=300)
}else{
grDevices::png(paste(dir,"/",filename,".png",sep=""),width=width,height=height, units="in",res=300)
}
print(figure)
grDevices::dev.off()
fnameTempImage=paste(dir,"/",filename,".png",sep="")
tempImage<-magick::image_read(fnameTempImage)
croppedImage<-magick::image_trim(tempImage,fuzz=0)
magick::image_write(croppedImage,fnameTempImage)
rlang::inform(gsub("//","/",paste("Figure saved as: ",dir,"/",filename,".png", sep="")))
}
if(pdfpng=='both'){
grDevices::pdf(paste(dir,"/",filename,".pdf",sep=""),width=width,height=height)
print(figure)
grDevices::dev.off()
if(transparent){
grDevices::png(paste(dir,"/",filename,".png",sep=""),bg="transparent", width=width,height=height, units="in",res=300)
}else{
grDevices::png(paste(dir,"/",filename,".png",sep=""),width=width,height=height, units="in",res=300)
}
print(figure)
grDevices::dev.off()
fnameTempImage=paste(dir,"/",filename,".png",sep="")
tempImage<-magick::image_read(fnameTempImage)
croppedImage<-magick::image_trim(tempImage,fuzz=0)
magick::image_write(croppedImage,fnameTempImage)
rlang::inform(gsub("//","/",paste("Figure saved as: ",dir,"/",filename,".png", sep="")))
rlang::inform(gsub("//","/",paste("Figure saved as: ",dir,"/",filename,".pdf", sep="")))
}
}
}