Make R Exit with Non-Zero Status Code

Make R exit with non-zero status code

It's an argument to quit(). See ?quit.

Arguments:

status: the (numerical) error status to be returned to the operating
system, where relevant. Conventionally ‘0’ indicates
successful completion.

Details:

 Some error statuses are used by R itself.  The default error
handler for non-interactive use effectively calls ‘q("no", 1,
FALSE)’ and returns error code 1. Error status 2 is used for R
‘suicide’, that is a catastrophic failure, and other small numbers
are used by specific ports for initialization failures. It is
recommended that users choose statuses of 10 or more.

Exit code when calling R script from bash

I can get the status from Rscript back to the calling shell (R version 4.0.2, bash version 3.2.57 or zsh version 5.8 running on a MacBook Pro, macOS Mohave 10.14.6) using q(status = N). To get non-zero exit status from a failed R script, use q(status = 2) or higher, see quit:

$ Rscript -e 'q(status = 0);'
$ echo $?
0

$ Rscript -e 'q(status = 2);'
$ echo $?
2

$ Rscript -e 'q(status = 10);'
$ echo $?
10

$ R --version
R version 4.0.2 (2020-06-22) -- "Taking Off Again"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin13.4.0 (64-bit)

$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)

$ zsh --version
zsh 5.8 (x86_64-apple-darwin17.7.0)

Using a script essentially identical to the one you posted, I get the expected results (the status from quit is passed successfully to the enclosing shell):

Script:

#!/usr/bin/env Rscript

message("Starting script")
## Use status = 0, 2 or 10:
q(save = "no", status = 2, runLast = FALSE)
message("Should not reach here")

Output (tested with zsh and bash versions shown above):

$ ~/test/test1.r
Starting script
$ echo $?
0
$ ~/test/test1.r
Starting script
$ echo $?
2
$ ~/test/test1.r
Starting script
$ echo $?
10

SEE ALSO:

Some error status values are used by R itself. The default error
handler for non-interactive use effectively calls q("no", 1, FALSE)
and returns error status 1. Error status 2 is used for R ‘suicide’,
that is a catastrophic failure, and other small numbers are used by
specific ports for initialization failures. It is recommended that
users choose statuses of 10 or more.

Valid values of status are system-dependent, but 0:255 are normally
valid.

(From quit docs)

Can't access User Library in R - Non-Zero Exit Status warning

A non-zero exit status means in this case that the system failed to install the package. There seem to be a number of unresolved dependencies in the installation process. You could try to resolve this by attempting to install the package using the option dependencies=TRUE; like this:

install.packages("ggplot2", dependencies=TRUE)

How to get non-zero exit status with lintr::lint() in order to fail a build

The lintr::lint() function returns results in a list with class "lints". You have issues if its length is greater than zero, so you can do

Rscript -e "quit(save = 'no', status = length(lintr::lint('my_script.R')))"

r- install package returned non-zero exit status warning

I managed to resolve it by installing readxl. One of the warnings states that there is no package called 'readxl'



Related Topics



Leave a reply



Submit