Learn R Programming

this.path (version 1.4.0)

from.shell: Top-Level Code Environment

Description

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

Usage

from.shell()
is.main()

Arguments

Value

TRUE or FALSE.

Details

When an R script is run from a shell, from.shell() and is.main() will both be TRUE. If that script sources another R script, from.shell() and is.main() will both be FALSE for the duration of the second script.

Otherwise, from.shell() will be FALSE. is.main() will be TRUE when there is no executing script or when source()-ing a script in a toplevel context, and FALSE otherwise.

Examples

Run this code
FILES <- tempfile(c("file1_", "file2_"), fileext = ".R")
this.path:::write.code({
    from.shell()
    is.main()
}, FILES[2])
this.path:::write.code((
    bquote(this.path:::withAutoprint({
        from.shell()
        is.main()
        source(.(FILES[2]), echo = TRUE, verbose = FALSE,
            prompt.echo = "file2> ", continue.echo = "file2+ ")
    }, spaced = TRUE, verbose = FALSE, width.cutoff = 60L,
       prompt.echo = "file1> ", continue.echo = "file1+ "))
), FILES[1])


this.path:::Rscript(
    c("--default-packages=this.path", "--vanilla", FILES[1])
)


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(TRUE)[[1L]])\\n\")",
    "-e", "source(commandArgs(TRUE)[[1L]])",
    FILES[1]))


unlink(FILES)

Run the code above in your browser using DataLab