I once did a blog on combining graphis with external programs and received a terrific comment from a reader (-click here-) on implementing this entirely within R with ghosts
Tyler, dude. Have I been demoted from being a Stack Ove-R-flow peer to a "reader" of your blog? Or is that a promotion ;)
This feels a little hackish to me, but should get the job done. Add this as the first few lines of the function and remove the os argument:
testme <- c(UNIX = "gs -version",
Win32 = "gswin32c -version",
Win64 = "gswin64c -version")
os <- names(which(sapply(testme, system) == 0))
I've used the -version
switch so that R doesn't try to load Ghostscript unnecessarily.
On my Ubuntu system, when I run this, os
returns, as expected, UNIX
, and on my Windows system where I have the 32-bit version of Ghostscript installed, it returns Win32
. Try it out on your 64-bit machine running the 32-bit GS and let me know how it works.
After reading the help pages for system()
and system2()
, I learned about Sys.which()
, which seems to be exactly what you're looking for. Here it is in action on my Ubuntu system:
Sys.which(c("gs", "gswin32c", "gswin64c"))
# gs gswin32c gswin64c
# "/usr/bin/gs" "" ""
names(which(Sys.which(c("gs", "gswin32c", "gswin64c")) != ""))
# [1] "gs"
Thus, OS specification can be skipped entirely in the mergePDF()
function:
mergePDF <- function(infiles, outfile) {
gsversion <- names(which(Sys.which(c("gs", "gswin32c", "gswin64c")) != ""))
pre = " -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="
system(paste(paste(gsversion, pre, outfile, sep = ""), infiles, collapse = " "))
}
You may want to do some error checking. If the length of gsversion
is > 1 or is 0, for instance, you may want to stop the function and prompt the user to either install Ghostscript or to verify their Ghostscript version.