1

Hopefully someone can help me figure this issue out. I am trying to system call gdal functions inside of R using the system / system2 functions. Here is an example of the code:

> system("gdalinfo --version")
sh: gdalinfo: command not found

> system2("gdalinfo --version")
sh: gdalinfo --version: command not found

As you can see system cannot call the gdal functions. Anyone know how to fix this?

Inside of the Mac terminal, I can call any of the gdal command line functions:

user-MacBook-Pro:~ user$ gdalinfo --version
GDAL 1.10.1, released 2013/08/26

Here is a Sys.info -- I am running Yosemite on Mac 10.10.1:

Sys.info()
# sysname        "Darwin"                                                                                         
# release        "14.0.0"                                                                                         
# version        "Darwin Kernel Version 14.0.0: Fri Sep 19 00:26:44 PDT 2014; root:xnu-2782.1.97~2/RELEASE_X86_64"
# nodename       "user-MacBook-Pro.local"                                                                         
# machine        "x86_64"                                                                                         
# login          "user"                                                                                           
# user           "user"                                                                                           
# effective_user "user" 

Also see the difference between these two calls. First in R:

system("echo $PATH") /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

And now in terminal, where GDAL is explicitly mentioned:

user-Pro:~ user$ echo $PATH /Library/Frameworks/GDAL.framework/Programs:/Library/Frameworks/GDAL.framework/Programs:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

enter image description here

  • Does the path to the gdal binaries exist in Sys.getenv('PATH')? – jbaums Jan 19 '15 at 1:45
  • @jbaums no they don't link to the goal binaries. I am terrible at CS side of computer. I can handle my data, but not my computers. – user1738753 Jan 19 '15 at 2:00
1

It looks like there are some issues with the path variable, i.e. the shell opened by R doesn't know the path to the gdal binaries. There are two ways to fix this:

Specifying the full path

You can always use the whole path to gdalinfo in your system call to make it work:

path <- "/path/to/gdal/bin/gdalinfo" system2(path, "--version")

This may be the easiest to implement if you are running your script on different systems and OSs since you don't have to mess with environment variables. It's also easier to understand for other people that may be reading your code.

Note that with system2 each argument has to passed separately.

Setting the path variable

As pointed out by jbaums, you can modify the .Renviron file. From your terminal call

nano /path/to/R_HOME/.Renviron

If there's no reference to PATH in there (or if it's empty), add PATH=$PATH:/path/to/gdal/bin somewhere, then save and exit (CTRL+X). If there's already a reference to PATH, just append :/path/to/gdal/bin to it, save and exit.

Start R, call Sys.getenv("PATH") and see if your changes show up. Or just call `system("gdalinfo --version").

Note that this will work differently on windows.

  • the solution is not ideal -- often times my scripts are used both on mac / window systems, hence not ideal but workable (i guess if statement based on mac or not with paste) – user1738753 Jan 19 '15 at 1:23
  • not accepting your solution yet, since I am trying to figure out why R can't find path – user1738753 Jan 19 '15 at 1:23
  • I think new versions of OS X use bash as default shell, whereas the doc of system() says that it tries to launch /bin/sh. Try >system("echo $PATH") from R and $ echo $PATH from Terminal and see if they're different. – Geotob Jan 19 '15 at 1:34
  • yes, they are indeed different. See call in the original post. – user1738753 Jan 19 '15 at 1:56
  • I am still learning how computers REALLY work -- I understand my data though. Do you know how to have R change its behavior? Many thanks!! – user1738753 Jan 19 '15 at 2:03
1

From section 10.13 of R for Mac OS X FAQ:

When executing system commands (for example directly via system or indirectly via functions that call other programs such as install.packages) the locations in which the shell is looking for programs is governed by the PATH environment variable. That variable may be set differently for R started from an interactive shell and for R started in the GUI. You can use Sys.getenv("PATH") to verify the current setting.

When R is started from a shell (e.g., on the command line of the Terminal), it will inherit settings from that shell. Those are typically modified by shell-specific configuration files, such as .profile or .bashrc. Those apply only to the shell, not to the system as a whole. In addition, some installers (such as MacTeX) will modify global shell settings to make user's life more comfortable on the command line, but, again, those do not apply to programs not started from the shell – such as GUIs. See Apple Technical Q&A QA1067 for what Apple says about the topic.

In order to standardize your PATH setting in R, you can set it in your .Renviron file (in your home directory – see ?Startup in R for details on how R uses configuration files on startup). This setting will apply to R regardless of how it is started. You can also add more elaborate constructs to .Rprofile instead if you wish to selectively modify the existing PATH variable via Sys.getenv and Sys.setenv.

You should be able to edit your .Renviron file in your home directory, appending the path to your gdal binaries to the existing PATH variable. The PATH variable is a colon-separated list of paths that should be searched by R when commands are executed e.g. with system.

.Renviron is a hidden file - one way to edit it is to open it in a text editor from the terminal, e.g.:

nano ~/.Renviron

or

open -a TextEdit ~/.Renviron

Once open, identify where PATH is set, and prepend /path/to/my/gdal/binaries: to the existing paths (alter the gdal path to reflect the correct location on your system).

  • I am really sorry not to have grasped your answer fully (I know it is right). I guess my problem is that I don't understand what the PATH variable is. Here is the call I used: Sys.getenv('R_HOME') [1] "/Library/Frameworks/R.framework/Resources". But I still don't know how to append the path. – user1738753 Jan 19 '15 at 2:20
  • @user1738753 - you need to open that file in a text editor, and find the existing reference to PATH (if any). Let me know what's there. – jbaums Jan 19 '15 at 2:32
  • I added a picture to my original post -- I was easily able to find the path, but was unable to locate the file that included the path (I opened every file in the folder but could not find where I needed to add the PATH) – user1738753 Jan 19 '15 at 2:54
  • @user1738753 - see my edits above. Perhaps remove that screenshot from your post now, since I misinformed you when I said you should edit the Rprofile file contained in the path given by Sys.getenv('R_HOME'). Instead, you should modify .Renviron in your home directory. – jbaums Jan 19 '15 at 3:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.