Get the line number of the executing expression.

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 srcref.

Scripts run with Rscript do not store their srcref, even when getOption("keep.source") is TRUE.

For source() and sys.source(), make sure to supply argument keep.source = TRUE directly, or set options "keep.source" and "keep.source.pkgs" to TRUE.

For debugSource() in ‘RStudio’, it has no argument keep.source, so set option "keep.source" to TRUE before calling.

For compiler::loadcmp(), the srcref is never stored for the compiled code, there is nothing that can be done.

For utils::Sweave(), the srcref is never stored, there is nothing that can be done.

For knitr::knit(), the srcref is never stored, there is nothing that can be done. I am looking into a fix.

For package:targets, set option "keep.source" to TRUE before calling associated functions.

For box::use(), plumber::plumb(), shiny::runApp(), and testthat::source_file(), 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