reflections
01 Syntax
02 Methods
| Name | Overloads | Summary |
|---|---|---|
| attr | 1 | Object Attributes Get or set specific attributes of an object. |
| attributes | 1 | Object Attribute Lists These functions access an object's attributes. |
| getClass | 1 | Object Classes R possesses a simple generic function mechanism which can be used for an object-oriented style of programming. |
| formals | 1 | Access to and Manipulation of the Formal Arguments Get or set the formal arguments of a function. |
| eval | 1 | Evaluate an (Unevaluated) Expression Evaluate an R expression in a specified environment. |
| parse | 1 | Parse Expressions parse returns the parsed but unevaluated expressions in a list. |
| sys_calls | 1 | Functions to Access the Function Call Stack These functions provide access to environments (‘frames’ in S terminology) associated with functions further up the calling stack. |
| sys_call | 1 | Functions to Access the Function Call Stack These functions provide access to environments (‘frames’ in S terminology) associated with functions further up the calling stack. |
03 Members
Object Attributes
Get or set specific attributes of an object.
These functions provide access to a single attribute of an object. The replacement form causes the named attribute to take the value specified (or create a new attribute with the value given).
The extraction Function first looks For an exact match To which amongst the attributes Of x, Then (unless exact = True) a unique Partial match. (Setting options(warnPartialMatchAttr = True) causes Partial matches To give warnings.)
The replacement Function only uses exact matches.
Note that some attributes (namely Class, comment, Dim, dimnames, names, row.names And tsp) are treated specially And have restrictions On the values which can be Set. (Note that this Is Not True Of levels which should be Set For factors via the levels replacement Function.)
The extractor Function allows (And does Not match) empty And missing values Of which: the replacement Function does Not.
NULL objects cannot have attributes And attempting To assign one by attr gives an Error.
Both are primitive functions.
| Name | Type | Description |
|---|---|---|
x | Object | an object whose attributes are to be accessed. |
which | String | a non-empty character string specifying which attribute is to be accessed. |
exact | Boolean | logical: should which be matched exactly? |
value | Object | an object, the new value of the attribute, or NULL to remove the attribute. |
env | Environment | - |
For the extractor, the value of the attribute matched, or NULL if no exact match is found and no or more than one partial match is found.
create a 2 by 5 matrix
x <- 110 attr(x,"dim") <- c(2, 5)
Object, Environment)Object Attribute Lists
These functions access an object's attributes. The first form below returns the object's attribute list. The replacement forms uses the list on the right-hand side of the assignment as the object's attributes (if appropriate).
Unlike attr it is not an error to set attributes on a NULL object: it will first be coerced to an empty list.
Note that some attributes (namely Class, comment, Dim, dimnames, names, row.names And tsp) are treated specially And have restrictions On the values which can be Set. (Note that this Is Not True Of levels which should be Set For factors via the levels replacement Function.)
Attributes are Not stored internally As a list And should be thought Of As a Set And Not a vector, i.e, the order Of the elements Of attributes() does Not matter. This Is also reflected by identical()'s behaviour with the default argument attrib.as.set = TRUE. Attributes must have unique names (and NA is taken as "NA", not a missing value).
Assigning attributes first removes all attributes, Then sets any Dim attribute And Then the remaining attributes In the order given: this ensures that setting a Dim attribute always precedes the dimnames attribute.
The mostattributes assignment takes special care For the Dim, names And dimnames attributes, And assigns them only When known To be valid whereas an attributes assignment would give an Error If any are Not. It Is principally intended For arrays, And should be used With care On classed objects. For example, it does Not check that row.names are assigned correctly For data frames.
The names Of a pairlist are Not stored As attributes, but are reported As If they were (And can be Set by the replacement form Of attributes).
NULL objects cannot have attributes And attempts To assign them will promote the Object To an empty list. Both assignment And replacement forms Of attributes are primitive functions.
| Name | Type | Description |
|---|---|---|
x | Object | any R object |
Object)Object Classes
R possesses a simple generic function mechanism which can be used for an object-oriented style of programming. Method dispatch takes place based on the class of the first argument to the generic function.
| Name | Type | Description |
|---|---|---|
x | Object | a R object |
Object, Environment)Access to and Manipulation of the Formal Arguments
Get or set the formal arguments of a function.
For the first form, fun can also be a character string naming the function to be manipulated, which is searched for from the parent frame. If it is not specified, the function calling formals is used.
Only closures have formals, Not primitive functions.
| Name | Type | Description |
|---|---|---|
fun | Object | a Function, Or see 'Details’. |
env | Environment | environment in which the function should be defined. |
formals returns the formal argument list of the function specified, as a pairlist, or NULL for a non-function or primitive.
The replacement form sets the formals Of a Function To the list/pairlist On the right hand side, And (potentially) resets the environment Of the Function.
Evaluate an (Unevaluated) Expression
Evaluate an R expression in a specified environment.
eval evaluates the expr argument in the environment specified by envir and returns the computed value. If envir is not specified, then the default is parent.frame() (the environment where the call to eval was made).
Objects to be evaluated can be of types call Or expression Or name (when the name Is looked up in the current scope And its binding Is evaluated), a promise Or any of the basic types such as vectors, functions And environments (which are returned unchanged).
| Name | Type | Description |
|---|---|---|
expr | Object | an object to be evaluated. See 'Details’. |
env | dataframe | the environment In which expr Is To be evaluated. May also be NULL, a list, a data frame, a pairlist Or an Integer As specified To sys.Call. |
The result of evaluating the object: for an expression vector this is the result of evaluating the last element.
Parse Expressions
parse returns the parsed but unevaluated expressions in a list.
If text has length greater than zero (after coercion) it is used in preference to file.
All versions Of R accept input from a connection With End Of line marked by LF (As used On Unix), CRLF (As used On DOS/Windows) Or CR (As used On classic Mac OS). The final line can be incomplete, that Is missing the final EOL marker.
| Name | Type | Description |
|---|---|---|
text | String | character vector. The text to parse. Elements are treated as if they were lines of a file. Other R objects will be coerced to character if possible. |
file | Object | a connection, or a character String giving the name Of a file Or a URL To read the expressions from. If file Is "" And text Is missing Or NULL Then input Is taken from the console. |
An object of type "expression", with up to n elements if specified as a non-negative integer.
A syntax error (including an incomplete expression) will throw an error.
Character strings In the result will have a declared encoding If encoding Is "latin1" Or "UTF-8", Or If text Is supplied With every element Of known encoding In a Latin-1 Or UTF-8 locale.
Functions to Access the Function Call Stack
These functions provide access to environments (‘frames’ in S terminology) associated with functions further up the calling stack.
sys.calls and sys.frames give a pairlist of all the active calls and frames, respectively, and sys.parents returns an integer vector of indices of the parent frames of each of those frames.
Int32, Environment)Functions to Access the Function Call Stack
These functions provide access to environments (‘frames’ in S terminology) associated with functions further up the calling stack.
| Name | Type | Description |
|---|---|---|
which | Int32 | the frame number If non-negative, the number Of frames To go back If negative. |
sys.call, sys.function and sys.frame accept integer values for the argument which. Non-negative values of which are frame numbers starting from .GlobalEnv whereas negative values are counted back from the frame number of the current evaluation.