Running system() with git-bash in R - r

I checked
How to execute git-bash command with system() or shell() in R but this didn't exactly solve the problem for me. I'm very new to R, I'm using it on Windows and I'm modifying another project. I suspect originally this project was written for a different OS.
At one point, the main script calls a .sh file from inside R in a for loop, and uses system() to run it. The sh file creates a new directory, copies files from one directory to the other and modifies them slightly (removes first row & adds another).
The part in the code that calls the file goes like this: (run_this is the sh file we want to run)
directory_name = "data/clean"
for (i in 1:n) {
filename = sprintf("%s.json",i)
cmd = sprintf("run_this.sh %s %s", filename, directory_name)
system(cmd)
}
I suspect system() calls command prompt in Windows, which I've checked doesn't run this sh file. But I've found I can run them from Git Bash. Unfortunately I'd have to do them one by one if I choose this option and since n is large this doesn't work very well for me.
So, I'm wondering
1) is there any way to direct system() or system2() to use Git Bash from inside R? (I have Git Bash to my environment variables.)
2) any other possible solutions to run sh files from command prompt?

Related

Calling bash from within R

I have R generating some .csv files for another python program to run in another folder, I know it is possible to call bash from R but how could I call the command make in my ubuntu virtual machine in another directory?
The simple way is creating an script to cd to your dir and exec make after that
script <- tempfile()
fhandle <- file(script)
writeLines("( cd /your_directory && make )",con=fhandle)
system2("/bin/bash",args=c(script))
You may need to find the correct path to /bin/bash, mine is from MacOs
You can work with system2 parameters to control what happens with output from make command and if you want to run the process in parallel with your R task or wait for completion.

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.

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.

Multiple shell commands in Windows

I'm trying to replicate a shell command in R and cannot figure out how to string commands together. This just returns the contents of the working folder (system() fails for some reason):
> shell("dir")
Volume info ..
Directory of E:\Documents\R
contents are listed..
Now lets try and navigate to C drive and run dir (without using the obvious dir C:)..
> shell("cd C:")
C:\
> shell("dir")
Volume in drive E is GT
etc..
So it seems commands can't be entered separately as the shell doesn't remember the working directory. So..
> (cmd = "cd C:
+ dir")
[1] "cd C:\ndir"
> shell(cmd)
C:\
No luck as the C: folders are not reported. Other methods I've tried also fail. Grateful for any ideas.
The documentation explains why system doesn’t work: it executes the command directly on Windows, without spawning a shell first.
shell (or better, system2) is the way to go but as you’ve noticed, shell will always spawn a new shell so that changes to the environment don’t carry over. system2 won’t work directly either since it quotes its commands (and thus doesn’t allow chaining of commands).
The correct solution in this context is not to use a shell command to change the directory. Use setwd instead:
setwd('C:')
system2('dir')
If you want to reset the working directory after executing the command, use the following:
local({
oldwd = getwd()
on.exit(setwd(oldwd))
setwd('C:')
system2('dir')
})
I'm on Linux and this works for me:
system("cd ..;ls")
in navigating to the previous directory and running ls/dir there. In your case, on Windows, this apparently works:
shell("cd C: & dir")
or to get the output as a character vector:
shell("cd C: & dir", intern=T) and on Linux: system("cd ..; ls", intern=T)
Don't know if this helps, but collapsing the commands to one string when using system works on MacOS
cmds <- c("ls", "cd ..", "ls");
system(paste(cmds, collapse=";"))
This is a solution from here. That solved my problem in calling the windows dir command:
system("cmd.exe /c dir", intern=TRUE)

How to run a R language(.r) file using Batch file?

I want to run a R script file (.r) using batch file.
If R.exe is in your PATH, then your windows batch file (.bat) would simply consist of one line:
R CMD BATCH your_r_script.R
otherwise, you need to give the path of R.exe, so for example:
"C:\Program Files\R\R-2.13.0\bin\R.exe" CMD BATCH your_r_script.R
you can add certain input arguments to the BATCH command, such as --no-save, --no-restore
so it would be
R CMD BATCH [options] your_r_script.R
more info on options, etc at http://stat.ethz.ch/R-manual/R-devel/library/utils/html/BATCH.html
Note: R uses the command "BATCH" to non-interactively evaluate a script located in a file. Here we are running the command "BATCH" from a windows .BAT file, but that's merely a coincidence.
An answer to another question suggests using Rscript.exe, so your batch file would contain:
"C:\Program Files\R\R-3.0.2\bin\i386\Rscript.exe" your_script.R
pause
It is a good idea to add R to the windows environment path. In a comment in this question #chase gave a link that explains how to set the path on windows 7. Once R is added to the windows path, your batch file should become simply :
Rscript.exe your_script.R
pause
You can also directly call a R command by using the -e flag. For example this batchfile will tell R to set its current working directory to Documents, then it will print the working directory:
Rscript.exe -e setwd('Documents');getwd()
pause
I struggled with the syntax with the answers below, but this worked for me in the .bat file:
C:\Windows\System32\cmd.exe /k ""path to Rscript.exe"
"path to .R script""
Be sure to place both the path to Rscript.exe and the script in "" together and separately as above.
I doubt you will be able to run it using a batch file.
http://www.fileinfo.com/extension/r
Most known programs that use .r files do so for source code files it looks like. You will probably have to compile it using the program it was written for. I guess you could use a command line compiler from a batch file, but I don't know what language or applications you are using.
If you post the script file or give more information about it, we could probably help you better.

Resources