run rscript from command line until it succeeds - r

I need to run an R script from command prompt in windows. The problem is that the R script has some lines which sometimes gives warnings/errors (not any logical or syntax error but just fails due to data issue. Anyway, this is not important). So, instead of re-running it manually every time it fails, I want to put the system command in some kind of loop to run it until it succeeds i.e. does not return any warnings/errors.
I am using the following system command to run the r script in question.
Rscript D:/r_code.r 123 544
# 123 and 544 are two numeric arguments which I pass to the r script.
How do I do I achieve my task?
NOTE: In the end, I need to run a batch file which will have multiple commands to run different R scripts and I need to put this loop until it succeeds over every command in it. Batch file will look like:
Rscript D:/r_code1.r 123 544 # put loop condition here
Rscript D:/r_code2.r 125 524 # put loop condition here
Rscript D:/r_code3.r 156 553 # put loop condition here
Rscript D:/r_code4.r 187 587 # put loop condition here
Adding a screenshot showing how error or warning gets displayed in command prompt as soon as I run the rscript command

You could try something like this:
#echo off
:loop
Rscript D:/r_code.r 123 544 | findstr "exception error warning" >nul 2>nul
set warningfound=%errorlevel%
if %warningfound% neq 1 goto loop
This executes a command, and if it finds one of the words in the findstr command in the output it goes to the loop tag, and executes it again.

Related

What does `python3 ()` do?

While trying to execute a timeit command on the command line using the python command line interface I accidentally put .function() on the outside of the command like so:
$ python3 -m timeit '<code>'.function()
Rather than the timeit command being executed, I was prompted as such:
function>
Thinking I had entered the python repl I tried to quit with q. Yes, I'm aware quit() is the correct way to do this. Having returned to the command line, I noticed the error and corrected it like so:
$ python3 -m timeit `<code>.function()`
I expected this code to execute correctly, but instead I received the following error:
python3:7: command not found: q
After discussing it with some colleagues, it was suggested that I check which python was being used:
$ which python3
python3 () {
q
}
This was not what I was expecting! Normally the result would be /usr/local/bin/python3. Through some trial and error I was able to determine that the minimal case to reproduce this is:
$ python3 ()
function> q
$
Now that the context is out of the way, I have two questions about the behaviour I witnessed:
1. What exactly does python3 () do?
2. How do I return execution to its original state in the same terminal window? I'm aware I can open a new terminal window and the original state exists in that window.
The syntax foo () is used in POSIX-compliant shells (such as bash, dash, and zsh) to define a function. Your entire snippet defines a function called python3 and executes the command q when it's ran. You can bypass shell functions and aliases using the command command: command -p python3 myfile.py
To remove the function from the current shell process, you can use unset -f python3. If it keeps coming back after starting new shells, then it's likely defined in one of you shell initialization files.

How to crontab a R-script and append the errors

I'm new to Ubuntu and would like to run a daily crontab. I'm running similar job under Windows Command Line but would like to switch to Ubuntu.
My R-Script has an Error-function which prints errors into a separate
folder everytime a job is done and an error has occured ():
# define error function
my.error.fun <- function() {
cat(geterrmessage(), file="/home/network/R/Test.txt", append=TRUE)
}
# output error
options("error"=my.error.fun)
Under crontab -e I have determined the following:
00 16 * * * /usr/bin/Rscript /home/R/network/Test.R 2> /home/R/RErrors/Test.txt
and the crontabhas been saved under /tmp/crontab.2t/crontab
where I append the errros inside the R-Script. In contrast to Windows Command Line that concept seems not to work in Ubuntu That seems not to work however. I'm not sure whether thats the correct syntax. There has been no file in the RErrors folder.
May I as for some suggestions?

pass character "&" by args to R

the following R code is to be execute by command prompt (windows)
# Collect arguments
args <- commandArgs(TRUE)
archivo <- as.character(args[1])
cat(archivo)
like this
C:\Users\Owner\Desktop>Rscript prueba.r "hola&chao"
problem is that command prompt respond
hola
'chao' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\Owner\Desktop>
I need to R accepts the "&" as a character and print it all together in the cat()
What should I do?
Thanks
This isn't an R problem, but a shell problem. You actually executed
Rscript prueba.r hola&chao
instead of
Rscript prueba.r "hola&chao"
In the Windows command shell,
prog1 & prog2
executes both programs in sequence, which is why you see the output of
Rscript prueba.r hola
followed by the output of
chao
The other possibility is that Rscript is a buggy batch file.

Jenkins Build Script exits after Google Test execution

I am building a Qt GUI application via Jenkins. I added 3 build steps:
Building the test executable
Running the test executable
compiling a coverage report with gcovr
For some reason, the shell task for running the test executable stops after execution. Even a simple echo does not run after. The tests are written with Google Test and output xUnit XML files, which are analyzed after the build.
Some tests start the applications user interface, so I installed the jenkins xvnc plugin to get them to run.
The build tasks are as follows:
Build
cd $WORKSPACE/projectfiles/QMake
sh createbin.sh
Test
cd $WORKSPACE/bin
./Application --gtest_output=xml
Coverage Report
cd $WORKSPACE/projectfiles/QMake/out
gcovr -x -o coverage.xml
Now, an echo at the end of the first build task is correctly printed, but an echo at the end of the second is not. The third build task is therefore not even run, although the Google Test output is visible. I thought that maybe the problem is that some of the Google Tests fail, but why whould the script stop executing just because the tests fail?
Maybe someone can give me a hint on why the second task stops.
Edit
The console output looks like this:
Updating svn://repo/ to revision '2012-11-15T06:43:15.228 -0800'
At revision 2053
no change for svn://repo/ since the previous build
Starting xvnc
[VG5] $ vncserver :10
New 'ubuntu:10 (jenkins)' desktop is ubuntu:10
Starting applications specified in /var/lib/jenkins/.vnc/xstartup
Log file is /var/lib/jenkins/.vnc/ubuntu:10.log
[VG5] $ /bin/sh -xe /tmp/hudson7777833632767565513.sh
+ cd /var/lib/jenkins/workspace/projectfiles/QMake
+ sh createbin.sh
... Compiler output ...
+ echo Build Done
Build Done
[VG5] $ /bin/sh -xe /tmp/hudson4729703161621217344.sh
+ cd /var/lib/jenkins/workspace/VG5/bin
+ ./Application --gtest_output=xml
Xlib: extension "XInputExtension" missing on display ":10".
[==========] Running 29 tests from 8 test cases.
... Test output ...
3 FAILED TESTS
Build step 'Execute shell' marked build as failure
Terminating xvnc.
$ vncserver -kill :10
Killing Xvnc4 process ID 1953
Recording test results
Skipping Cobertura coverage report as build was not UNSTABLE or better ...
Finished: FAILURE
Generally, if one Build Step fails, the rest will not be executed.
Pay attention to this line from your log:
[VG5] $ /bin/sh -xe
The -x makes the shell print each command in console before execution.
The -e makes the shell exit with error if any of the commands failed.
A "fail" in this case, would be a return code of not 0 from any of the individual commands.
You can verify this by running this directly on the machine:
./Application --gtest_output=xml
echo $?
If the echo $? displays 0, it indicates successful completion of the previous command. If it displays anything else, it indicates an error code from the previous command (from ./Application), and Jenkins treats it as such.
Now, there are several things at play here. First is that your second Build Step (essentially a temporary shell script /tmp/hudson4729703161621217344.sh) is set to fail if one command fails (the default behaviour). When the Build Step fails, Jenkins will stop and fail the whole job.
You can fix this particular behaviour by adding set +e to the top of your second Build Step. This will not cause the script (Build Step) to fail due to individual command failure (it will display an error for the command, and continue).
However, the overall result of the script (Build Step) is the exit code of the last command. Since in your OP, you only have 2 commands in the script, and the last is failing, it will cause the whole script (Build Step) to be considered a failure, despite the +x that you've added. Note that if you add an echo as the 3rd command, this would actually work, since the last script command (echo) was successful, however this "workaround" is not what you need.
What you need is proper error handling added to your script. Consider this:
set +e
cd $WORKSPACE/bin && ./Application --gtest_output=xml
if ! [ $? -eq 0 ]; then
echo "Tests failed, however we are continuing"
else
echo "All tests passed"
fi
Three things are happening in the script:
First, we are telling shell not to exit on failure of individual commands
Then i've added basic error handling in the second line. The && means "execute ./Application if-and-only-if the previous cd was successful. You never know, maybe the bin folder is missing, or whatever else can happen. BTW, the && internally works on the same error code equals 0 principle
Lastly, there is now proper error handling for the result of ./Application. If the result is not 0, then we show that it had failed, else we show that it had passed. Note, this since the last command is not a (potentially) failing ./Application, but an echo from either of if-else possibilities, the overall result of the script (Built Step) will be a success (i.e 0), and the next Build Step will be executed.
BTW, you can as well put all 3 of your build steps into a single build step with proper error handling.
Yes... this answer may be a little longer than what's required, but i wanted you to understand how Jenkins and shell treat exit codes.

Write errors to file

Is there a way to write my R errors to a file? I run R on bash via:
R --vanilla < myprogram > myprogram.out &
When my program encounters an error (not a syntax error...like an illegal replacement or something) it stops but the error line isn't written to the output file and I don't know what the program was and a lot of the time I log out from the server while it runs.
Thanks,
Josh
Use the R CMD BATCH <infile> <outfile> syntax instead.

Resources