[R-SIG-Mac] Shell used within system()

Marc Girondot marc_grt at yahoo.fr
Sun Jul 6 16:30:02 CEST 2014


Thanks a lot to Anne and Simon,
I much better understand now how R can interact with shell.

Here is a sysnthesis of the solutions based on your suggestions. I post 
here as it could help others.

Solution 1: read the .profile file and execute it before to send commands

# Here the content of the .profile file is read and store to be used later
# Note that other files can be used for configuration
fileprofile <- paste0(system("echo $HOME", intern=TRUE), "/.profile")
a <- readLines(con=fileprofile)
abat1 <- paste(a[a!=""], sep="", collapse=";")

# Here I indicate the commands that I want send to the shell
command <- "echo $0;echo $PATH"

# example with sh shell
system(paste(abat1, command, sep=";"))

# example with bash (Thanks Anne)
system(paste0("bash -c '", abat1, ";", command, "'"))

Solution 2: Modify the .Renviron file

The more "normal" solution is indeed indicated in R faq for Mac 10.13. 
It is to modify .Renviron directly.
All the variables indicated in .profile must be indicated here, not only 
the PATH variable.
It works also perfectly.


But still a problem: When I run the software ncl_convert2nc 
(https://www.ncl.ucar.edu/Document/Tools/ncl_convert2nc.shtml) using a 
shell script from R.app, it works but the same within Rstudio, it 
freezes Rstudio. I will contact the team who develops RStudio.

Thanks a lot. I progress very well today (waiting that sun comes to 
France :( )

Marc Girondot


Le 06/07/2014 13:03, Simon Urbanek a écrit :
> Marc,
>
> you omitted important details, but I think the short answer is see Mac FAQ 10.13.
>
> The long answer is that it has nothing to do with the shell. If you run R on the command line then it inherits all your setting - including all variables that you set in your config files.
>
> If you run the R.app Mac GUI then there is *no* login shell involved since it's an application run through launch services, so none of user login config files are processed. When you run system() - it is not running a login shell, but just regular shell hence the config files are again irrelevant (this is also true for R in Terminal, but there you already inherit the settings from the login shell). Also if you look at sh --version you will notice that it is already bash.
>
> Cheers,
> Simon
>
> On Jul 6, 2014, at 8:02 AM, Marc Girondot <marc_grt at yahoo.fr> wrote:
>
>> Hi,
>>
>> I try to automatize some commands normally used in terminal that I would like to run in R.
>>
>> However, the same command xxx in terminal and in system("xxx") does not produce the same result.
>>
>> It seems related to the shell used and the variables of the shell.
>>
>> For example, in terminal:
>> Marc-Girondot-MBA:Chlorophyle marc$ echo $PATH
>> /opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/local/ncl-6.2.0/bin:/opt/local/bin:/opt/local/sbin:/Library/Frameworks/GDAL.framework/Programs:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/texbin
>>
>> whereas in R:
>>> system("echo $PATH")
>> /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/texbin:/opt/local/bin
>>
>> The shell used in terminal is
>> Marc-Girondot-MBA:Chlorophyle marc$ echo $SHELL
>> /bin/bash
>> Marc-Girondot-MBA:Chlorophyle marc$ echo $0
>> -bash
>>
>> whereas in R, it is sh:
>>> system("echo $0")
>> sh
>>
>> And this sh does not read my configuration files. I have tried to modifiy the ~/.profile or ~/.login but system("echo $PATH") does not change.
>>
>> My question is then:
>> is it possible to change the default shell used by system ? I have tried:
>> system("/bin/bash\necho $0\necho $PATH")
>>
>> but R becomes unresponsive...
>>
>> or where is the configuration file for sh run by R ?
>>
>> Thanks a lot
>>
>> Marc
>>
>> _______________________________________________
>> R-SIG-Mac mailing list
>> R-SIG-Mac at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-mac
>>
>



More information about the R-SIG-Mac mailing list