LINENO: Line Number of Executing Expression

LINENOR Documentation

Line Number of Executing Expression

Description

Get the line number of the executing expression.

Usage

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

Arguments

n, envir, matchThisEnv, srcfile

See ?this.path().

Details

LINENO() only works if the expressions have a \codelink2base:srcfilesrcref.

Scripts run with \Rscriptlink do not store their srcref, even when \codelink3base:optionsgetOption("keep.source") is TRUE.

For \sourcelink and \syssourcelink, make sure to supply argument keep.source = TRUE directly, or set options \stringkeep.source and \stringkeep.source.pkgs to TRUE.

For \debugSourceinRStudiolink, it has no argument keep.source, so set option \stringkeep.source to TRUE before calling.

For \compilerloadcmplink, the srcref is never stored for the compiled code, there is nothing that can be done.

For \utilsSweavelink, the srcref is never stored, there is nothing that can be done.

For \knitrknitlink, the srcref is never stored, there is nothing that can be done. I am looking into a fix.

For \targetslink, set option \stringkeep.source to TRUE before calling associated functions.

For \boxuselink, \plumberplumblink, \shinyrunApplink, and \testthatsourcefilelink, the srcref is always stored.

Value

integer; NA_integer_ if the line number is not found.

Note

You can get a more accurate line number by wrapping LINENO() in braces:

{ LINENO() }

Examples

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)


if (getRversion() >= "4.3.0") {
    source(FILE.R, echo = TRUE, verbose = FALSE,
        max.deparse.length = Inf, keep.source = TRUE)
} else {
    @R_PACKAGE_NAME@:::.source(FILE.R, echo = TRUE, verbose = FALSE,
        max.deparse.length = Inf, keep.source = TRUE)
}


unlink(FILE.R)

ArcadeAntics/this.path documentation built on July 27, 2024, 12:05 a.m.