Why the command system() does not work in R? - r

I am running an .Rmd script in R 4.2.0.
This script executes several system() commands.
However, some of these commands return 127 in the console window in Rstudio 2022.02.3 Build 492 (I think that this output refers to the error 127), while other commands are simply not executed (mkdir or /din/mv for instance).
I am running R and Rstudio under Windows 10 Pro.
I read the suggestion that in Windows the system() command works better in this style for instance:
system("cmd.exe", input = "cd ..")
But also this simple command does not work. The output is like that:
(c) Microsoft Corporation. All rights reserved.
C:\User\me\Documents>cd ..
C:\User\me>[1] 0
However, if I type:
system("cmd.exe", input = "cd ..")
The directory is not changed:
(c) Microsoft Corporation. All rights reserved.
C:\User\me\Documents>pwd
C:/User/me/Documents
As a consequence the .Rmd script cannot create subdirectories and save new files generated from the data elaboration.
I hope someone can find the solution.
Thanks.

Related

system function usage from Rstudio in linux environment

I have the below line in my R script which is working when I invoke R from command line in Linux environment,
system('spectre run.scs -f psfascii +l mylog.txt >> /dev/null')
But the same line is not working when I run this script by launching RStudio from the command line,
spectre is a valid command which works when R is invoked, but when rstudio the command is not getting recognized.
Point to Note: The spectre command is from an external module apart from R, which I have loaded in the linux terminal like below before calling R and rstudio.
EDITED with PATH from R and RStudio
When I run which spectre from the command line and check its path,
> Sys.getenv("PATH")
[1] "/tool/eda/apps/synopsysHspice/2013.03-SP2/hspice/bin:/tool/eda/apps/synopsysHspice/2013.03-SP2/hspice/arch:/tool/eda/apps/mentorCalibre/2014.2_33.25/aoi_cal_2014.2_33.25/bin:/tool/eda/apps/cadenceMMSIM/13.11.292/tools/bin:/tool/eda/apps/cadenceMMSIM/13.11.292/bin:/tool/eda/apps/cadenceICOA/6.1.6.500.11/share/oa/bin:/tool/eda/apps/cadenceICOA/6.1.6.500.11/tools/dfII/bin:/tool/eda/apps/cadenceICOA/6.1.6.500.11/tools/bin:/tool/pandora64/bin:/tool/pandora64/sbin:/tool/pandora/bin:/tool/pandora/sbin:/usr/lib64/qt-3.3/bin:/bin:/usr/bin:/opt/puppetlabs/bin"
But when I check the path in R studio,
> Sys.getenv("PATH")
[1] "/tool/pandora64/.package/qt-5.5.1/bin:/tool/pandora64/.package/R-3.4.3/bin:/tool/pandora64/.package/rstudio-99.9.9/app/bin:/bin:/usr/bin:/tool/eda/apps/cadenceMMSIM/13.11.292:/tool/eda/apps/cadenceMMSIM/13.11.292"
Answer was suggested by #r2evans from the comment section,
Changed the PATH in Rstudio by copying the path from R environment in command line,
Sys.setenv(PATH="/tool/eda/apps/synopsysHspice/2013.03-SP2/hspice/bin:/tool/eda/apps/synopsysHspice/2013.03-SP2/hspice/arch:/tool/eda/apps/mentorCalibre/2014.2_33.25/aoi_cal_2014.2_33.25/bin:/tool/eda/apps/cadenceMMSIM/13.11.292/tools/bin:/tool/eda/apps/cadenceMMSIM/13.11.292/bin:/tool/eda/apps/cadenceICOA/6.1.6.500.11/share/oa/bin:/tool/eda/apps/cadenceICOA/6.1.6.500.11/tools/dfII/bin:/tool/eda/apps/cadenceICOA/6.1.6.500.11/tools/bin:/tool/pandora64/bin:/tool/pandora64/sbin:/tool/pandora/bin:/tool/pandora/sbin:/usr/lib64/qt-3.3/bin:/bin:/usr/bin:/opt/puppetlabs/bin")

RStudio does not execute `system` commands from Rprofile.site

Using Rstudio (Version 1.0.143) under Ubuntu (16.04), if I add system("echo 'Hello world'") to my /usr/lib/R/etc/Rprofile.site file, I have no Hello world message displayed in R studio at startup.
If I start R from the terminal I have the Hello world message.
It seems that R studio ignores the system commands from Rprofile.site (idem from the .Rprofile file in the home directory)
Is it possible to enable system commands executions or is there a good reason to avoid this behavior ?
For short, RStudio doesn't source Rprofile.site file at all. (I use Ubuntu 17.04 and compile R-3.4.1 myself)
test Rprofile.site
To simply the question, you can use x=1 rather system("echo 'Hello world'") in the Rprofile.site file. Then you open RStudio:
> x
Error: object 'x' not found
That tells you whether RStudio ignores the system() commands in Rprofile.site or R studio ignores the entire Rprofile.site file.
test system()
If you find that your RStudio source theRprofile.site file instead. You can try system("echo haha > x") or system2('echo', 'haha', stdout = T) -> x.
That tells you whether RStudio ignores the system() commands or you can't see the message due to other reasons.
what RStudio says
Finally, see https://support.rstudio.com/hc/en-us/community/posts/200643758-Rprofile-site-
We don't actually implement the code for sourcing Rprofile.site (R does)
by Ian Pylvainen, Support Engineer at RStudio, Inc.

How does R system() recognize command path?

How does R's built-in system() function know where to look to invoke some arbitrary OS command specified by the command argument? For example, if I homebrew install some_command_line_program, how does R's system() function know where it is located when I type:
cmd <- "some_complicated_code_from_some_command_line_program"
system(cmd, wait=FALSE)
In other words, how is R smart enough to know where to look without any user input? If I compile from source via Github (instead of homebrew install), would system() also recognize the command?
What system does depends on your OS, you've not told us (although you've given us some clues...).
On unix-alike systems, it executes it as a command in a bash shell, which searches for the first match in the directories on the $PATH environment variable. You can see what that is in R:
> Sys.getenv("PATH")
[1] "/usr/local/heroku/bin:/usr/local/heroku/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/nobackup/rowlings/NEST4B"
In Windows, it does something else.
You can get a full path to whatever it might run with Sys.which, which uses the systems' which command on unixes and fakes it on Windows. Read the help for more.
If you compile something from source then it will be found if the file that runs the command (a shell script, an executable, a #!-script in any language) is placed in a folder in your $PATH. You can create a folder yourself, say /home/user/bin, put your executables in there, add that to your $PATH, and (possibly after logging out an in again, or restarting R, or just firing up a new shell...) then R will find it.

R, RStudio: Programmatically switch between 32-bit and 64-bit versions of R

Is there a function that, within RStudio, restarts the R console in 64-bit mode or 32-bit mode without re-opening RStudio (or at least automatically re-opening it if that can't be avoided)?
I commonly run in 32-bit when using RODBC so that I can retrieve data from an Access database, but would like to otherwise leverage the capabilities of 64-bit mode for all other tasks while still in RStudio.
You could save the part of your code that you wish to execute using the 32-bit executable into a new script. For example, I have a script called myscript.r which will just print which version of the R executable (64 or 32-bit) which was used to run it:
cat(as.character(version[2]))
Of course you could replace this with the part of your code dealing with RODBC.
Now the main way to programmatically run a script with a custom executable is to invoke a command to the OS terminal or shell. This command should contain:
Which executable to invoke, in our case R 32-bit (the Rscript.exe file contained in the i386 folder of your R_HOME directory)
Arguments accepted by this executable, such as the path to the R script we want to run using this executable.
The path of myscript is "c:/gp/trash/myscript.r", and my 32-bit R executable is
paste0(Sys.getenv("R_HOME"), "/bin/i386/Rscript.exe)
C:/PROGRA~1/R/R-40~1.4/bin/i386/Rscript.exe
I can run this script using:
myscript <- "c:/gp/trash/myscript.r"
output <- system(paste0(Sys.getenv("R_HOME"), "/bin/x64/Rscript.exe ", myscript), wait = FALSE, invisible = FALSE, intern = T)
output
[1] "x86_64"
output_32 <- system(paste0(Sys.getenv("R_HOME"), "/bin/i386/Rscript.exe ", myscript), wait = FALSE, invisible = FALSE, intern = T)
output_32
[1] "i386"
So as you can see we're executing this script from two different executables. In practice I'd suggest to save the results of your ODBC queries to a file, which you can read in your main x64 R session.
Just a little vocabulary if you don't know some of these terms:
The terms terminal or shell are often used interchangeably. In RStudio, if you click on the terminal tab next to console, you'll be able to enter commands that will be processed by a shell.
Shell commands are instructions that instruct the system to do some action.
A shell is a user interface for accessing the services of an operating system.
A terminal is a wrapper program that runs a shell and allows us to enter commands.
sources: terminal, console and cli, shell commands, scripting with r, this stackoverflow answer

Unable to run R script through .bat files in Windows Server

I'm trying to run a R script through a .bat file. When I run myself the commands line by line it works. But when I try to run the .bat file, it doesn't works.
This is the .bat file
cd "C:\Program Files\R\R-3.1.2\bin"
R CMD BATCH "C:\Users\Administrator\Downloads\testa_vps.R"
This is the R script
setwd('C:\Users\Administrator\Documents')
file.create('mycsv.csv')
I'm not an expert with Windows and generally try to stick to Unix-like systems for things like this, but I have found that using programs non-interactively (e.g. via .bat files) is usually less error-prone when you add the appropriate directories to your (user) PATH variable, rather than cding into the directory and calling the executable from within the .bat file. For example, among other things, my user PATH variable contains C:\PROGRA~1\R\R-3.0\bin\; - the directory that contains both R.exe and Rscript.exe - (where PROGRA~1 is an alias for Program Files which you can use in an unquoted file path, since there are no spaces in the name).
After you do this, you can check that your PATH modification was successful by typing Rscript in a new Command Prompt - it should print out usage information for Rscript rather than the typical xxx is not recognized as an internal or external command... error message.
In the directory C:\Users\russe_000\Desktop\Tempfiles, I created test_r_script.r, which contains
library(methods)
setwd("C:\Users\russe_000\Desktop\Tempfiles")
file.create("mycsv.csv")
and test_r.bat, which contains
Rscript --vanilla --no-save "C:\Users\russe_000\Desktop\Tempfiles\test_r_script.r"
Clicking on the Windows Batch File test_r ran the process successfully and produced mycsv.csv in the correct folder.
Before running test_r.bat:
After running test_r.bat:
I've never worked with a Windows server, but I don't see why the process would be fundamentally different than on a personal computer; you just may need your sysadmin to modify the PATH variable if you don't have sufficient privileges to alter environment variables.
As already suggested by #nrussel in the comments you should use RScript.exe for this.
Create a file launcher.bat with the following content:
cd C:\Users\Administrator\Documents
Rscript testa_vps.R
In addition, add C:\Program Files\R\R-[your R version]\bin\x64; or C:\Program Files\R\R-[your R version]\bin\i386to the System PATH variable in the Environment Variables menu depending if you run R on a 64-bit or 32-bit system.
I just tested the approach above successfully on a Windows Server 2008 64-bit system and mycsv.csv got created as expected.
EDIT
One important point I forgot to mention is the following: You need to specify the path in your R file in the setwd() call using \\ instead of \.
setwd('C:\\Users\\Administrator\\Documents')
Here is a screenshot of the successful run on the Windows 2008 server:
Note: I added cmd /k to the .bat file so that the cmd window stays open after clicking on the file.

Resources