Learn R Programming

this.path (version 2.5.0)

is.main: Top-Level Code Environment

Description

Determine if a program is the main program, or if a program was run from a shell.

Usage

is.main()
from.shell()

Arguments

Value

TRUE or FALSE.

Details

If no scripts are being source()-ed, then is.main() will be TRUE, and FALSE otherwise.

If is.main() is TRUE and R was invoked with the command-line arguments -f FILE, --file=FILE, or -e EXPR, then from.shell() will be TRUE, and FALSE otherwise.

Examples

Run this code
FILE.R <- tempfile(fileext = ".R")
this.path:::.writeCode({
    this.path:::.withAutoprint({
        from.shell()
        is.main()
    }, spaced = TRUE, verbose = FALSE,
       prompt.echo = "FILE.R> ", continue.echo = "FILE.R+ ")
}, FILE.R)

this.path:::.Rscript(c(
    "--default-packages=this.path", "--vanilla",
    FILE.R
), show.command = FALSE, show.output.on.console = TRUE)

source(FILE.R, verbose = FALSE)

this.path:::.Rscript(c(
    "--default-packages=this.path", "--vanilla",
    "-e", "cat(\"\n> from.shell()\\n\")",
    "-e", "from.shell()",
    "-e", "cat(\"\n> is.main()\\n\")",
    "-e", "is.main()",
    "-e", "cat(\"\n> source(commandArgs(trailingOnly = TRUE))\\n\")",
    "-e", "source(commandArgs(trailingOnly = TRUE))", FILE.R
), show.command = FALSE, show.output.on.console = TRUE)

unlink(FILE.R)

Run the code above in your browser using DataLab