These functions are for working with source files and more generally
with “source references” (
"srcref"), i.e., references to
source code. The resulting data is used for printing and source level
debugging, and is typically available in interactive R sessions,
options(keep.source = TRUE).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
srcfile(filename, encoding = getOption("encoding"), Enc = "unknown") srcfilecopy(filename, lines, timestamp = Sys.time(), isFile = FALSE) srcfilealias(filename, srcfile) getSrcLines(srcfile, first, last) srcref(srcfile, lloc) ## S3 method for class 'srcfile' print(x, ...) ## S3 method for class 'srcfile' summary(object, ...) ## S3 method for class 'srcfile' open(con, line, ...) ## S3 method for class 'srcfile' close(con, ...) ## S3 method for class 'srcref' print(x, useSource = TRUE, ...) ## S3 method for class 'srcref' summary(object, useSource = FALSE, ...) ## S3 method for class 'srcref' as.character(x, useSource = TRUE, to = x, ...) .isOpen(srcfile)
The name of a file.
The character encoding to assume for the file.
The encoding with which to make strings: see the
A character vector of source lines. Other R objects will be coerced to character.
The timestamp to use on a copy of a file.
A vector of four, six or eight values giving a source location; see ‘Details’.
An object of the appropriate class.
Whether to read the
An optional second
Additional arguments to the methods; these will be ignored.
These functions and classes handle source code references.
srcfile function produces an object of class
srcfile, which contains the name and directory of a source code
file, along with its timestamp, for use in source level debugging (not
yet implemented) and source echoing. The encoding of the file is
file for a discussion of encodings, and
iconvlist for a list of allowable encodings on your platform.
srcfilecopy function produces an object of the descendant
srcfilecopy, which saves the source lines in a character
vector. It copies the value of the
isFile argument, to help
debuggers identify whether this text comes from a real file in the
srcfilealias function produces an object of the descendant
srcfilealias, which gives an alternate name to another
srcfile. This is produced by the parser when a
getSrcLines function reads the specified lines from
srcref function produces an object of class
srcref, which describes a range of characters in a
lloc value gives the following values:
c(first_line, first_byte, last_line, last_byte, first_column, last_column, first_parsed, last_parsed)
Bytes (elements 2, 4) and
columns (elements 5, 6) may be different due to multibyte
characters. If only four values are given, the columns and bytes
are assumed to match. Lines (elements 1, 3) and parsed lines (
elements 7, 8) may differ if a
#line directive is used in
code: the former will respect the directive, the latter will just
count lines. If only 4 or 6 elements are given, the parsed lines
will be assumed to match the lines.
Methods are defined for
close for classes
open method opens its internal
file connection at
a particular line; if it was already open, it will be repositioned
to that line.
Methods are defined for
as.character for class
method will read the associated source file to obtain the text
corresponding to the reference. If the
to argument is given,
it should be a second
srcref that follows the first, in the
same file; they will be treated as one reference to the whole
range. The exact behaviour depends on the
class of the source file. If the source file inherits from
srcfilecopy, the lines are taken from the saved copy
using the “parsed” line counts. If not, an attempt
is made to read the file, and the original line numbers of the
srcref record (i.e., elements 1 and 3) are used. If an error
occurs (e.g., the file no longer exists), text like
<srcref: "file" chars 1:1 to 2:10> will be returned instead,
line:column ranges of the first and last
summary method defaults to this type of
srcref objects may be attached to expressions as the
"srcref" attribute. (The list of
srcref objects should be the same
length as the expression.) By default, expressions are printed by
print.default using the associated
see deparsed code instead, call
useSource = FALSE. If a
is printed with
useSource = FALSE, the
record will be printed.
.isOpen is intended for internal use: it checks whether the
connection associated with a
srcfile object is open.
srcfile returns a
srcfilecopy returns a
getSrcLines returns a character vector of source code lines.
srcref returns a
getSrcFilename for extracting information from a source
removeSource to remove it from a
(non-primitive) function (aka ‘closure’).
1 2 3 4 5 6 7
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.