Runs a command line tool with a Conda environment activated. The command
gets the environment variables that the activation sets, including those
of the activate.d scripts of its packages. With activate = FALSE, it
gets a fixed activation instead, without those scripts.
Usage
run(
cmd,
...,
env_name = "condathis-env",
method = "auto",
verbose = c("output", "silent", "cmd", "spinner", "full"),
error = c("cancel", "continue"),
stdout = "|",
stderr = "|",
stdin = NULL,
input = NULL,
binary = FALSE,
supervise = FALSE,
cleanup_tree = FALSE,
linux_pdeathsig = FALSE,
activate = TRUE,
timeout = Inf
)Arguments
- cmd
Character string, the command to run: a single, non-empty string. Other values abort with class
condathis_invalid_cmd. Pass the arguments of the command in....- ...
Arguments of the command, unnamed: character, numeric or logical vectors, each element one argument.
run()dropsNULLvalues. It writes numbers in full, never in scientific notation:100000is passed as"100000".NA, named arguments, and values that are not atomic vectors (for example lists) abort with classcondathis_run_invalid_args.- env_name
Character string with the environment name. It must be a plain directory name that is valid on Linux, macOS and Windows:
Not empty, and not
.or...Not starting with
-, and not ending with..Without path separators,
:,#, whitespace, control characters or<>"|?*.Not a Windows device name, such as
NULorCOM1(see Environment names increate_env()).
Invalid values abort with class
condathis_invalid_env_name.run(),run_bin(),run_pipeline()andrun_step()also accept anenv_path()value. The other functions reject it with classcondathis_env_path_read_only.- method
Character string naming the backend.
"auto"(the default) uses the backend that ownsenv_namewhen it exists, and otherwise the first available backend ingetOption("condathis.backend_priority")order (see condathis-options). Any name fromlist_registered_backend_names()selects that backend, and"micromamba"is built in. The name of a backend whose packagecondathisknows, such as"rattler"of therattlerthispackage, also selects it.condathisloads the package on first use."native"is a deprecated alias for"micromamba"(warns once per session). The 0.1.4 defaultc("native", "auto")is read as"native".If you name a backend other than the owner, functions that change the environment abort. The others warn and use the owner. The classes:
condathis_backend_not_installed: the package of the named backend is not installed.condathis_backend_mismatch: the named backend does not own the environment, and the function changes it.condathis_backend_mismatch_warning(a warning): the same, for a function that does not change the environment.condathis_backend_ambiguous_env: several backends have an environment of that name, andmethodnames none of them.condathis_invalid_method: an invalid value.
- verbose
Character string that controls the console output. One of:
"output": show the output of the command and the messages ofcondathis."silent": show nothing."cmd": show the command line."spinner": show only a spinner."full": show the command line, the output, and the backend's own log.
TRUEmeans"output"andFALSEmeans"silent". Every level except"silent"shows a spinner in interactive sessions. It also shows these messages and this warning when they occur (see condathis-conditions):condathis_micromamba_downloading(a message): no usablemicromambawas found, so it is downloaded. Queries, such aslist_envs(), never download it.condathis_env_lock_waitingandcondathis_cache_lock_waiting(messages): the call waits for another R session.condathis_install_micromamba_checksum_unavailable(a warning): the checksum of the downloadedmicromambacould not be checked.
Invalid values abort with class
condathis_invalid_verbose. The default is shown in Usage.The command line shown, the
cmdof a result and the messages ofcondathisshow the password and the token of a URL as*****.micromambadoes the same (https://user:*****@host/channel,https://host/t/*****/channel). The command still gets them.- error
Character string that controls what happens when the command fails. With
"cancel"(the default),condathisaborts with these classes:condathis_<function>_status_error(alsocondathis_status_error): a non-zero exit status.condathis_<function>_timeout_error(alsocondathis_timeout_error): a timeout.condathis_<function>_env_not_found(alsocondathis_env_not_found): a missing environment.condathis_<function>_resolve_failed(alsocondathis_backend_error): the backend failed to resolve the command, for example in a failed activation. The error keeps the condathis classes of the backend's error, such ascondathis_activation_dump_error, and has that error as its parent.
A problem with an
env_path()value also addscondathis_env_path_not_foundorcondathis_env_path_not_conda_env. The status and timeout errors carry the fieldsstatus,stdout,stderrandtimeout.With
"continue",condathisreturns a result instead. Its exit status is the status of the command, 127 for a missing environment or a command that could not start, or -9 for a timeout.Invalid values abort with class
condathis_invalid_error. See condathis-conditions for all classes.- stdout
Standard output target:
"|"(the default) captures it in the result, a file path writes it to that file,""sends it to the standard output of the R process, andNULLdiscards it.- stderr
Standard error target:
"|"(the default) captures it in the result,"2>&1"sends it to the standard output target, a file path writes it to that file,""sends it to the standard error of the R process, andNULLdiscards it.- stdin
Standard input source:
NULL(the default) for no input, the path of an existing file to read, or"|"to writeinputto the command. Other values abort with classcondathis_invalid_stdin.- input
Character or raw vector written to the standard input of the command.
condathisjoins the elements of a character vector with line feeds and adds none after the last one. Must beNULL(the default) unlessstdin = "|". Other values abort with classcondathis_invalid_input. Withstdin = "|",condathisshows the output when the command ends, not while it runs, and shows no spinner.- binary
Logical. Whether to return
stdoutandstderras raw vectors instead of decoding them as UTF-8 text. Defaults toFALSE. A process has one encoding for both streams, so withTRUEboth are raw, even if only one of them carries binary data. Check withis.raw()before treating either as text.condathisnever shows raw streams on the console, whateververboseis. Values other thanTRUEandFALSEabort with classcondathis_invalid_binary.- supervise
Logical. Whether the
processxsupervisor watches the process: when the R session ends, even by a crash, the supervisor kills it. Defaults toFALSE.- cleanup_tree
Logical. Whether to kill the process tree of the command, its child processes included, when the command finishes, times out, or is interrupted. Defaults to
FALSE, so that a daemon that the command starts keeps running after the command exits. On Windows, a daemon that keeps the captured standard output or error of the command open ends when R has read that output. This is a limit ofprocessxon Windows, whatevercleanup_treeis. Send the output to files (stdout,stderr) to keep such a daemon running.- linux_pdeathsig
Logical. On Linux, whether the command gets
SIGTERMwhen the R session ends, even by a crash. Ignored on other platforms. Defaults toFALSE.- activate
Logical. Whether to run
cmdwith the activation of the backend. Defaults toTRUE: the backend activates the environment, with theactivate.dscripts andenv_vars.dfiles of its packages. An activation that fails is an error, aserrordescribes. WithFALSE, no backend code runs:condathissets onlyCONDA_PREFIX,CONDA_DEFAULT_ENV,CONDA_SHLVLandMAMBA_SHLVL(both1), empty prompt modifiers, and the binary directories of the environment in front ofPATH(see condathis-options). In both modes, the command gets the variables thatcondathissets in every process it starts. Values other thanTRUEandFALSEabort with classcondathis_invalid_activate.- timeout
Number of seconds the call may take, the activation of the environment included (see Details in
run()). Defaults toInf(no limit).condathislowers a finite value above 2147483 (about 24.8 days) to that value. When the time is up,condathiskills the command, or does not start it when the activation took all the time.error = "cancel"then aborts, anderror = "continue"returns a result withstatus = -9andtimeout = TRUE. Values that are not a single, non-negative number abort with classcondathis_invalid_timeout.
Value
A condathis_result object, invisibly: a classed list with
status, stdout, stderr, timeout, pid, cmd and env_name,
which prints as a short summary. status is the exit status of the
command. On Linux and macOS, a command that a signal ends has status
128 plus the signal number, as in a shell (143 for SIGTERM).
stdout and stderr are NA when that stream was not captured (a
target other than "|"). pid is the process ID of the command with
stdin = "|", and NA otherwise. cmd is your command line, each part
quoted, with the credentials of URLs masked (see verbose). env_name
is the environment name, or the directory of an env_path().
Details
run() activates the environment and starts the command itself. The
micromamba backend records the activation of an environment the first
time a command runs in it. That first command takes longer, and timeout
includes the activation. The backend reuses the activation until the
environment changes (its packages, activate.d scripts or env_vars.d
files). A change in the environment variables of the R session also ends
the reuse.
The command does not get the activation of the R session. When R runs
in an activated Conda or pixi environment, or in several nested ones,
condathis removes their binary directories from PATH and their
variables from the command's environment (see condathis-options,
which also lists what cannot be undone). A program of R's own
environment so never runs in place of one the environment lacks. The
command always runs in exactly one activated environment: CONDA_SHLVL
and MAMBA_SHLVL are 1 and the prompt modifiers are empty. The
command does not get what R's own startup set either: no R_*
variable, and not R's part of LD_LIBRARY_PATH or, on Windows, of
PATH (Rtools).
A bare cmd is looked up on the PATH of the command, after the
activation. On Windows, as in cmd.exe, the working directory of the
command comes first, and a name without an extension is looked up with
the extensions of PATHEXT. A name found nowhere fails with status 127,
as in a shell.
Arguments in ... reach the command exactly as given on every platform.
No shell is involved. Characters such as >, |, &, %, $, * and
quotes have no special meaning. Shell builtins (for example export or
source, or dir and echo on Windows) are not programs and cannot be
run, unless a program of that name is on the PATH of the command (as
echo.exe of Git for Windows). To use shell features, run a shell explicitly, as in
run("bash", "-c", "..."), or on Windows run("cmd", "/c", "dir"). To
connect commands with pipes, use run_pipeline(). One exception applies on
Windows. Windows starts batch files (.bat, .cmd) through cmd.exe,
which parses their arguments again.
When env_name is the default "condathis-env" and that environment
does not exist, run() creates it, empty, with the backend that method
selects. run() never creates any other missing environment. The call
fails, as error describes.
run() does not wait for another R session that changes the
environment, so do not change or remove an environment
(create_env() with overwrite = TRUE, install_packages(),
remove_env()) while commands run in it. Several run() calls can run
in parallel.
The errors of a failed or timed-out command carry the fields status,
stdout, stderr and timeout. A handler does not need to parse the
message, as in
tryCatch(run("false"), condathis_status_error = function(e) e$status).
See also
create_env() to create an environment, env_path() for an
environment created outside condathis, and condathis-conditions
for the condition classes.
Other commands:
parse_output(),
run_bin(),
run_pipeline(),
run_step()
Examples
if (FALSE) { # \dontrun{
condathis::with_sandbox_dir({
# A local channel shipped with condathis, with `polyglot`: a
# dependency-free script that runs on Linux, macOS and Windows.
channel <- system.file(
"extdata", "polyglot-channel",
package = "condathis"
)
create_env("polyglot", env_name = "polyglot-env", channels = channel)
# Run its command, on any platform
polyglot_res <- run(
"polyglot",
env_name = "polyglot-env",
verbose = "silent"
)
print(parse_output(polyglot_res)[1])
#> [1] "System detected: Linux/Unix"
# `pigz` (conda-forge) has builds for Linux, macOS and Windows
create_env("conda-forge::pigz", env_name = "pigz-env")
# Decompress a FASTQ file shipped with condathis
fastq_file <- fs::path_package(
"condathis", "extdata", "sample1_L001_R1_001.fastq.gz"
)
pigz_res <- run(
"pigz", "--decompress", "--stdout", fastq_file,
env_name = "pigz-env",
verbose = "silent"
)
print(parse_output(pigz_res)[1])
#> [1] "@read1/gi|424037187|ref|NR_073460.1| Homo sapiens ribosome ..."
})
} # }