R Flashcards
What is an expression?
1) An object that captures the structure of the code without evaluating it.
2) The data structures present in Abstract Syntax Trees.
3) An expression is any member of the set of base types created by parsing code: scalars, symbols call objects and pairlists.
Name the elements of an expression:
Constants, Symbols, Call objects and pairlists.
What is a Call Object?
A captured function call.
It is a special type of list where the first element is the name of the function and the remaining elements is the arguments to the function call.
Generally the function called is a symbol.
What is a constant?
NULL or an atomic vector of length 1 (i.e. a scalar). It is also the simplest element of the AST.
Bonus: Constants are self quoting meaning that the expression used to represent a constant is the constant itself.
What is a Symbol?
Name of an object.
Bonus: You can create symbols in two ways:
1) Capture an expression referencing to the name of an object:
expr(x)
2) Turn string into a symbol:
rlang::sym(“x”)
rlang::expr()
returns its argument unevaluated. It is equivalent to base::bquote()
rlang::is_syntactic_literal(x)
checks if x is a constant.
rlang::sym()
creates a symbol from a string.
rlang::call_standardise()
Standardize the call to use complete argument names.
Abstract Syntax Trees and function positions: What happens when a function in a call object is either: 1) Called from another package, an R6 object method or generated from a function factory?
The function position (the first position in the call object) in the abstract syntax tree is occupied by another call object.
The function is not in the current environment.
rlang::call2()
Create a call object from it’s components. First position is the name of the function, provided either as a string, symbol or another call. The remaining arguments are the arguments for the provided function.
Explain rlang::enexpr() vs rlang::expr().
Used to capture the code passed to a function call. As opposed to rlang::expr() which will capture whatever is typed within rlang::expr().
rlang::eval_tidy()
About controlling the evaluation of code.
What is datamasking?
The data mask makes it easier to evaluate an expression in the context of a data frame. This introduces potential evaluation ambiguity which we’ll then resolve with data pronouns.
What does parsing mean?
The process by which a computer language takes a string and constructs an expression