3

Currently I run R scripts the following way:

R --slave < <script_fullname> argument1 argument2 ...

I was wondering the best practices in R on how to exit the script with a warning, would q() just do it?

if(!file.exists(argument1)){
q()

}

print to the stdout

    if(!file.exists(argument1)){
    print('file does not exist')
    q()
}

and print to the std err?

Also, I see the following warning everytime I run R scripts this way. When reading the stdout I see:

ARGUMENT 'argument1' __ignored__
ARGUMENT 'argument2' __ignored__

Is there anyway to avoid such warnings?

Dnaiel
  • 7,622
  • 23
  • 67
  • 126

1 Answers1

0

If you are going to exit your R script because of an error, I suggest using
stop("warning message here") rather than print() and q().

print() is not recommended, since "it’s hard to capture and selectively ignore this sort of output. Printed output is not a condition, so you can’t use any of the useful condition handling tools." -- from Debugging, condition handling, and defensive programming

stderr is discussed in this SO post (too long to repeat all of the options here).

Try using --args to avoid the warnings:
R --slave < <script_fullname> --args argument1 argument2 ...

See also Passing Custom Arguments.

Community
  • 1
  • 1
Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492