call_system: Run a system command, stopping on error

Description Usage Arguments Details

Description

The inverse of callr; this makes it easy to call a system command from R and have it behave.

Usage

1
2
call_system(command, args, env = character(), max_lines = 20, p = 0.8,
  stdout = TRUE, stderr = TRUE, locate_command = TRUE)

Arguments

command

The system command to be invoked, as a character string. Sys.which is useful here.

args

A character vector of arguments to command

env

A character vector of name=value pairs to be set as environment variables (see system2).

max_lines

Maximum number of lines of program output to print with the error message. We may prune further to get the error message under getOption("warn.length"), however.

p

Fraction of the error message to show from the tail of the output if truncating on error (default is 20% lines are head, 80% is tail).

stdout,stderr

Passed to system2. Set one of these to FALSE to avoid capturing output from that stream. Setting both to FALSE is not recommended.

locate_command

The command will be passed through Sys_which to find the full path, and will report a sensible error if the command can't be found.

Details

This function uses system2 to call a system command fairly portably. What it adds is a particular way of dealing with errors. call_system runs the command command with arguments args (and with optionally set environment variables env) and hides all produced output to stdout and stderr. If the command fails (currently any nonzero exit code is counted as a failure) then call_system will throw an R error giving

This means that a successful invocation of a program produces no output while the unsuccessful invocation throws an error and prints all information to the screen (though this is delayed until failure happens).

call_system also returns the contents of both stderr and stdout invisibly so that it can be inspected if needed.

The function run_system does the same thing and will be removed as soon as code that depends on it is out of use.


traitecoevo/callr documentation built on May 31, 2019, 7:42 p.m.