Search code examples
rcygwinrscript

Rscript cant find file using cygwin path


I'm running cygwin on Windows 7 and am having problems with Rscript. I'd like to run an R file as R script in a bin directory, but Rscript seems to be have problems with cygwin path names. Here an example Rscript file.

$ which test.R
/cygdrive/e/Glenn/bin/test.R
$ ls -l /cygdrive/e/Glenn/bin/test.R
-rwxr-xr-x+ 1 Glenn None 36 Jan  6 08:06 /cygdrive/e/Glenn/bin/test.R
$ cat /cygdrive/e/Glenn/bin/test.R
#!/usr/bin/env Rscript
print("hi")

The basic problem is an error when I try to run test.R

$ test.R
Fatal error: cannot open file '/cygdrive/e/Glenn/bin/test.R': No such file or directory

In diagnosing the problem, I found I can execute the code using Rscript explicitly, but not if a cygwin-style path is used.

$ Rscript /cygdrive/e/Glenn/bin/test.R
Fatal error: cannot open file '/cygdrive/e/Glenn/bin/test.R': No such file or directory

$ Rscript E:/Glenn/bin/test.R
[1] "hi"
$ which Rscript
/cygdrive/c/Program Files/R/R-3.1.2/bin/Rscript

I'd like to fix the problem or find a good workaround.

After understanding the problem better thanks to the answer by @varro, I wrote this hack:

$ cat ~/bin/myRscript
#!/bin/bash
winpath=$(cygpath -d "$1")
shift
Rscript $winpath "$@"

Then I rewrote test.sh:

$ cat ~/bin/test.R
#!/usr/bin/env myRscript
print("hi")

It works, but I don't like the hack and I'd like a more portable solution.

$ which test.R
/cygdrive/e/Glenn/bin/test.R
$ test.R
[1] "hi"

Solution

  • Judging by the location of Rscript under "Program Files", it doesn't look like it's a Cygwin program, so of course it will not understand Cygwin paths. It you want to run it in a Cygwin environment, you have to install the Cygwin version of R, which I believe is available under Cygwin Ports.