Learn R Programming

this.path (version 2.2.0)

LINENO: Line Number of Executing Expression

Description

Get the line number of the executing expression.

Usage

sys.LINENO()
env.LINENO(n = 0, envir = parent.frame(n + 1),
           matchThisEnv = getOption("topLevelEnvironment"))
src.LINENO(n = 0, srcfile = if (n) sys.parent(n) else 0)

LINENO(n = 0, envir = parent.frame(n + 1), matchThisEnv = getOption("topLevelEnvironment"), srcfile = if (n) sys.parent(n) else 0)

Value

An integer, NA_integer_ if the line number cannot be determined.

Arguments

n, envir, matchThisEnv, srcfile

See ?this.path().

Details

sys.LINENO() returns the line number of the most recent expression with a source reference and a source file equal to sys.path().

env.LINENO() returns the line number of the most recent expression with a source reference and a source file equal to env.path().

src.LINENO() returns the line number of its source file.

LINENO() returns the line number of the most recent expression with a source reference and a source file equal to this.path().

In general, LINENO() is the most useful. It works whether your R code is source()-d or modularized.

Examples

Run this code
FILE.R <- tempfile(fileext = ".R")
writeLines(c("
LINENO()
LINENO()
## LINENO() respects #line directives
#line 15
LINENO()
#line 1218
cat(sprintf('invalid value %d at %s, line %d\\n',
            -5, try.this.path(), LINENO()))
"), FILE.R)


# ## previously used:
#
# source(FILE.R, echo = TRUE, verbose = FALSE,
#     max.deparse.length = Inf, keep.source = TRUE)
#
# ## but it echoes incorrectly with #line directives
this.path:::.source(FILE.R, echo = TRUE, verbose = FALSE,
    max.deparse.length = Inf, keep.source = TRUE)


unlink(FILE.R)

Run the code above in your browser using DataLab