I would like to run all the R script (script1.R, script2.R, ...) stored in a directory (~/Sims). Moreover I would like that each script run in a separate terminal. The os I'm using is OS X 10.9.5.
I used a bash script with the following commands:
#!/bin/bash
FILES=~/Sims/*.R
for f in $FILES
do
xterm -e bash -c "R --vanilla < $f; exec bash" &
done
I would like to find an alternative for xterm (given that under os x require to install the package X11 and on some machine I can't install it) that it is part of os x (like the Terminal app)
I would like to not exit from the R environment at the end of the R script
This will mimic your xterm configuration but use new Terminal.app sessions instead:
for f in *.R
do
osascript -e "tell app \"Terminal\" to do script \"R --vanilla < /FULL/PATH/TO/${f}\""
done
As far as keeping the R session alive, I'm not sure that's possible.
Related
I have a R-script within which i call a shell script using system command and use the paste command to pass arguments to the shell script ( on unix machine) and i now would like execute the same R-script on a windows machine and am struggling to get it working
Here are the steps i followed
R code
source('C:\\Users\\xxxx\\Documents\\R\\R-3.5.2\\ms\\ms\\MS_Config.R')
if(is.null(git_version) | git_version == "" | length(git_version) == 0){
print('ERROR: EXECUTION STOPPED !!!')
print('PLEASE SPECIFY GITHUB TAG_ID')
stop()
}
print("test4444")
print(enable_data_pull)
print (getwd())
system(paste('C:\\Users\\xxxx\\Documents\\R\\R- 3.5.2\\ms\\ms\\MS_ALLM_Parallel_Runner.sh -c ', num_cores,
'-s ', snapshot_dt,
'-p ' , local_storage_path,
'-t ', tag,
'-g ', git_version,
'-y ', enable_data_pull
))
print ("after shell script execution")
I tried the following, but did not succeed
Installed cygwin and called the rscript from the cygwin terminal(PATH variable is updated to include R and its binaries)
rscript "C:\Users\xxxx\Documents\R\R-3.5.2\ms\ms\MS_Model_Kickoff.R"
Below is the error message that i see after the r-script attempts to run the shell script
'CreateProcess' failed to run 'C:\Users\xxxx\DOCUME~1\R\R-35~1.2\ms\ms\CONRM_~1.SH -c 25 -s 201811 -p C:\Users\xxxx\Documents\Test -t Analytical -g verModelRefit2.2.2 -y N'
what does the above error mean and how do i fix this and execute the shell script within from the R-script on windows machine?r
I need to issue unix commands from an R session. I'm on Windows R2 2012 server using RStudio 1.1.383 and R 3.4.3.
The shell() function looks to be the right one for me but when I specify the path to my bash shell (from Git for Windows install) the command fails with error code 127.
shell_path <- "C:\\Program Files\\Git\\git-bash.exe"
shell("ls -a", shell = shell_path)
## running command 'C:\Program Files\Git\git-bash.exe /c ls -a' had status 127'ls -a' execution failed with error code 127
Pretty sure my shell path is correct:
What am I doing wrong?
EDIT: for clarity I would like to pass any number of UNIX commands, I am just using ls -a for an example.
EDIT:
After some playing about 2018-03-09:
shell(cmd = "ls -a", shell = '"C:/Program Files/Git/bin/bash.exe"', intern = TRUE, flag = "-c")
The correct location of my bash.exe was at .../bin/bash.exe. This uses shell with intern = TRUE to return the output as an R object. Note the use of single quote marks around the shell path.
EDIT: 2018-03-09 21:40:46 UT
In RStudio we can also call bash using knitr and setting chunk options:
library(knitr)
```{bash my_bash_chunk, engine.path="C:\\Program Files\\Git\\bin\\bash.exe"}
# Using a call to unix shell
ls -a
```
Two things stand out here. Bash will return exit code 127 if a command is not found; you should try running the fully qualified command name.
I also see that your shell is being run with a /c flag. According to the documentation, the flag argument specifies "the switch to run a command under the shell" and it defaults to /c, but "if the shell is bash or tcsh or sh the default is changed to '-c'." Obviously this isn't happening for git-bash.exe.
Try these changes out:
shell_path <- "C:\\Program Files\\Git\\git-bash.exe"
shell("/bin/ls -a", shell = shell_path, flag = "-c")
Not on Windows, so can't be sure this will work.
Perhaps you need to use shQuote?
shell( paste("ls -a ", shQuote( shell_path) ) )
(Untested. I'm not on Windows. But do read ?shQuote))
If you just want to do ls -a, you can use the below commands:
shell("'ls -a'", shell="C:\\Git\\bin\\sh.exe")
#or
shell('C:\\Git\\bin\\sh.exe -c "ls -a"')
Let us know if the space in "Program Files" is causing problems.
And if you require login before you can call your command,
shell('C:\\Git\\bin\\sh.exe --login -c "ls -a"')
But if you are looking at performing git commands from R, the git2r by ropensci might suit your needs.
I need to run R in a Docker container and want to input a script from a volume I mounted using the standard infile notation, however, the file seems to be redirected to Docker, not R.
I'm using the following command:
docker run -v /root/share:/share r-base:latest R --vanilla --quiet < /share/test.r
How can I use the infile notation and run my R in Docker? (I need the direct output from R, so Rscript will not do.)
As #mazel-tov mentioned: try
docker run -v /root/share:/share r-base:latest /bin/bash -c 'R --vanilla --quiet < /share/test.r'
That way the redirection of stdin is done by the bash inside the docker instead of in your shell that starts the docker.
The WSL on Windows 10 allows execution of Linux commands and command-line tools via bash.exe. Very usefully, a Linux tool/command can be called from the Windows command-line (cmd.exe) by passing it as an argument to bash.exe like so:
bash.exe -c <linux command>
This is very useful because it should allow Windows-based scripts to combine Windows and Linux tools seamlessly.
Unfortunately, I have failed to call Linux commands from an R script (see below).
0) System
Win10 x64 + Anniversary Update + WSL installed
1) Comparison cases where calling Linux commands work
The following all work for me; shown here just with an example call to ls.
from the windows command-line (cmd.exe prompt)
bash -c "ls /mnt/a"
bash -c "ls /mnt/a > /mnt/a/test.txt"
Same works if started from WinKey + R
Same works from within a .bat file.
It can be called from compiled code. I tried with Delphi XE2 32-bit and 64-bit using ShellExecute:
For example, these work (32 and 64 bit):
ShellExecute (0, PChar('open'), PChar('cmd.exe'), PChar('/c c:\windows\system32\bash.exe -c "ls /mnt/a > /mnt/a/test.txt"'), nil, SW_SHOWNORMAL);
Or (32-bit code):
ShellExecute (0, PChar('open'), PChar('c:\windows\sysnative\bash.exe'), PChar('-c "ls /mnt/a > /mnt/a/test.txt"'), nil, SW_SHOWNORMAL);
Or (64-bit code):
ShellExecute (0, PChar('open'), PChar('c:\windows\system32\bash.exe'), PChar('-c "ls /mnt/a > /mnt/a/test.txt"'), nil, SW_SHOWNORMAL);
All of these seem to work (and ShellExecute returns 42).
2) Failure to call Linux commands from R
using R 3.3.1 x64
All of the below (and several similar things I've tried) fail with status 65535:
shell('c:/windows/system32/bash.exe -c "ls /mnt/a"', shell="cmd.exe", flag = "/c")
shell("ls", shell="c:/windows/system32/bash.exe", flag = "-c")
system('cmd /c c:/windows/system32/bash.exe -c "ls /mnt/a > /mnt/a/test.txt"')
system('bash -c "ls /mnt/a"')
system('c:/windows/system32/bash.exe -c "ls /mnt/a > /mnt/a/test.txt"')
3) Question
Given that examples under 1) work, I find 2) very puzzling. Am I missing anything obvious here?
I would be very grateful for a simple example where running a Linux command via bash.exe under WSL works.
Your failing examples should now be working correctly in Windows 10 Insider builds >= 14951 which introduced many "interop" improvements and new capabilities:
> system('bash -c "ls /"')
Generates:
bin cache dev home lib media opt root sbin srv tmp var
boot data etc init lib64 mnt proc run snap sys usr
I am currently working on one project which uses R to do a very sophisticated calculation, it sets up to be called by ASP.NET web application. It uses RServe as the interface to R and needs to use ROracle and DBI libraries too.
Now the problem is every time when the server restarts, I have to sign in to the server to launch the Rserve command manually, the question is: is there any way to automatically run RServe every time when server restarts. I am running it on windows.
Thanks very much.
As the public documentation of Rserve says, you can start Rserve from a shell script.
Just create a script that executes the following command:
echo 'library(Rserve);Rserve(FALSE,args="--no-save --slave --RS-conf <your_own_path>/rserve.conf")'|<R_bin_path>/R --no-save --slave
For instance, in my MacOS computer I can start Rserve executing this line:
/bin/sh -c "echo 'library(Rserve);Rserve(FALSE,args=\"--slave\")'|/Library/Frameworks/R.framework/Versions/3.2/Resources/bin/exec/R --no-save --slave"
This command outputs something like this:
Starting Rserve:
/Library/Frameworks/R.framework/Resources/bin/R CMD /Library/Frameworks/R.framework/Versions/3.2/Resources/library/Rserve/libs//Rserve --slave
Rserv started in daemon mode.
You can create a shell script (or windows script) and tell the OS to execute this script during the startup process.