Last active
March 15, 2016 20:19
-
-
Save wabarr/bf1a22abe0e26031b707 to your computer and use it in GitHub Desktop.
simple function to export a plot to EPS, with possible options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## updated with input from @richfitz and @hadley - Thanks, guys!! | |
export_eps <- function(filename, expression, ...){ | |
old <- setEPS() | |
on.exit(do.call(ps.options, old), add = TRUE) | |
postscript(filename, ...) | |
on.exit(dev.off(), add = TRUE) | |
expression | |
} |
Thanks @richfitz. I like this suggestion and have updated the gist.
FWIW eval()
isn't necessary here. This would be sufficient:
export_eps <- function(filename, expression, ...){
setEPS()
postscript(filename, ...)
on.exit(dev.off())
expression
}
You should also consider resetting the ps options:
export_eps <- function(filename, expression, ...){
old <- setEPS()
on.exit(do.call(ps.options, old), add = TRUE)
postscript(filename, ...)
on.exit(dev.off(), add = TRUE)
expression
}
@hadley I don't understand why I don't need to eval()
the expression
. I guess I need to go read that section in Advanced R (more closely).
Function arguments are lazily evaluated. So the plot expression won't actually be executed until it is called inside the function.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Consider
which will always close the device, even if
expression
fails.