getParseData | R Documentation |
If the "keep.source"
option is TRUE
, R's parser
will attach detailed information on the object it has parsed. These
functions retrieve that information.
getParseData(x, includeText = NA) getParseText(parseData, id)
x |
an expression returned from |
includeText |
logical; whether to include the text of parsed items in the result |
parseData |
a data frame returned from |
id |
a vector of item identifiers whose text is to be retrieved |
In version 3.0.0, the R parser was modified to include code written
by Romain Francois in his parser package. This constructs a
detailed table of information about every token and higher level
construct in parsed code. This table is stored in the
srcfile
record associated with source references in the
parsed code, and retrieved by the getParseData
function.
For getParseData
:
If parse data is not present, NULL
. Otherwise
a data frame is returned, containing the following columns:
line1 |
integer. The line number where the item starts. This is the
parsed line number called |
col1 |
integer. The column number where the item starts. The first character
is column 1. This corresponds to |
line2 |
integer. The line number where the item ends. |
col2 |
integer. The column number where the item ends. |
id |
integer. An identifier associated with this item. |
parent |
integer. The |
token |
character string. The type of the token. |
terminal |
logical. Whether the token is “terminal”, i.e. a leaf in the parse tree. |
text |
character string. If |
The rownames of the data frame will be equal to the id
values,
and the data frame will have a "srcfile"
attribute containing
the srcfile
record which was used. The rows will be
ordered by starting position within the source file, with parent items
occurring before their children.
For getParseText
:
A character vector of the same length as id
containing the associated
text items. If they are not included in parseData
, they will be
retrieved from the original file.
There are a number of differences in the results returned by
getParseData
relative to those in the original parser
code:
Fewer columns are kept.
The internal token number is not returned.
col1
starts counting at 1, not 0.
The id
values are not attached to the elements of the parse
tree, they are only retained in the table returned by getParseData
.
#line
directives are identified, but other comment
markup (e.g., roxygen comments) are not.
Duncan Murdoch
Romain Francois (2012). parser: Detailed R source code parser. R package version 0.0-16. https://github.com/halpo/parser.
parse
, srcref
fn <- function(x) { x + 1 # A comment, kept as part of the source } d <- getParseData(fn) if (!is.null(d)) { plus <- which(d$token == "'+'") sum <- d$parent[plus] print(d[as.character(sum),]) print(getParseText(d, sum)) }
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.