[Rd] How best to get around shadowing of executables by system()'s prepending of directories to Windows' PATH?

Henrik Bengtsson henrik.bengtsson at ucsf.edu
Mon May 18 22:40:09 CEST 2015


You probably already know, but you can at least work around it as:

Sys.which2 <- function(cmd) {
  stopifnot(length(cmd) == 1)
  if (.Platform$OS.type == "windows") {
    suppressWarnings({
      pathname <- shell(sprintf("where %s 2> NUL", cmd), intern=TRUE)[1]
   })
   if (!is.na(pathname)) return(setNames(pathname, cmd))
  }
  Sys.which(cmd)
}

(it falls back to Sys.which() if 'where %s' doesn't give anything)


> Sys.which2("convert")
                                                convert
"C:\\Program Files\\ImageMagick-6.8.3-Q16\\convert.exe"

> Sys.which("convert")
                             convert
"C:\\Windows\\system32\\convert.exe"

/Henrik

On Mon, May 18, 2015 at 11:08 AM, Yihui Xie <xie at yihui.name> wrote:
> +1 I have exactly the same problem.
>
> Regards,
> Yihui
> --
> Yihui Xie <xieyihui at gmail.com>
> Web: http://yihui.name
>
>
> On Mon, May 18, 2015 at 12:29 PM, Josh O'Brien <joshmobrien at gmail.com> wrote:
>> My question:
>>
>> On Windows, R's system() command prepends several directories to those
>> in the Windows Path variable.
>>
>> >From ?system
>>
>>      The search path for 'command' may be system-dependent: it will
>>      include the R 'bin' directory, the working directory and the
>>      Windows system directories before 'PATH'.
>>
>> This shadows any executables on the Path that share a name with, for
>> example, one of the Windows commands.
>>
>> What should I do when I'd really like (the equivalent of) a call
>> passed to system() that would be executed using the same Path that
>> you'd get if working directly at the Windows command line? Is there a
>> recommended workaround for situtations like this? (It _seems_ like it
>> would be handy if system() et al. included an additional argument that
>> optionally disabled the prepending of those extra directories, to give
>> Windows users full control of the path seen by system(). Would adding
>> such an argument have undesirable ramifications?)
>>
>>
>> Motivation and reproducible example:
>>
>> I'm motivated here by a desire to use the function plotdiff() from
>> Paul Murrell's gridGraphics package on my Windows laptop.  Getting
>> that to work will require a few code fixes, of which the masking of
>> ImageMagick's convert.exe by that in the C:/Windows/System32 seems to
>> be the most challenging. plotdiff() relies on system2() calls to
>> ImageMagick's 'convert'  function, as well as a call to
>> Sys.which(c("convert", "compare")) that tests for the presence of
>> ImageMagick on the Path. Even  if ImageMagick is placed early on the
>> Path, though, both calls to Sys.which() and system2() find Windows'
>> convert command  (which "Converts FAT volumes to NTFS") rather than
>> ImageMagick's convert.
>>
>>
>> Here's a reproducible example that shows what I'm seeing:
>>
>>     ## In R, make a pdf
>>     pdf("a.pdf")
>>     plot(rnorm(99), col="red")
>>     dev.off()
>>
>>     ## At Windows cmd command line
>>     where convert
>>     ## C:\Program Files\ImageMagick-6.8.8-Q16\convert.exe
>>     ## C:\Windows\System32\convert.exe
>>     convert -density 100x100 a.pdf a.png
>>
>>     ## From R
>>
>>     ## Unqualified references to convert find the 'wrong' one
>>     Sys.which("convert")
>>     ##                               convert
>>     ## "C:\\Windows\\system32\\convert.exe"
>>      system2("convert",  "-density 100x100 a.pdf b.png")
>>     ## Invalid Parameter - 100x100
>>     ## Warning message:
>>     ## running command '"convert" -density 100x100 a.pdf b.png' had status 4
>>
>>     ## A fully qualified reference does work
>>     system2("C:/Program Files/ImageMagick-6.8.8-Q16/convert",
>> "-density 100x100 a.pdf b.png")
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list