R Flashcards
(34 cards)
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
Explain the differences between calls and expressions.
An expression can be made up of call objects and so call objects are an element of an expression.
In the context of R, what is meant by Associativity? Mention two important operators that are special cases in R. Why should you care?
The order of evaluation of repeated infix functions. Most operators are left-associative in R, meaning that R evaluates from the left, but two exceptions are the exponential operator and the assignment.
I.e. 2^3^4 compute first 3^4 = 81 and then 2^81
You should care because some operations are non-associative, ex. is usage of + in ggplot2.
Are + associative in ggplot2?
No, ggplot2 uses + to draw geoms on top of eachother, i.e. geom_point + geom_smooth != geom_smooth + geom_point.
rlang::parse_expr()
Takes a string and creates an expression.
is.call(rlang::parse_expr()) always evaluates to TRUE.
What is deparsing?
The process of taking an expression and turning it in to a string.
rlang::expr_text()
Deparses an expression.
How is := used in tidy evaluation?
The colon-equals operator allows for having expressions as argument names.
What is quotation?
It is the act of capturing an unevaluated expression.
What is unquotation?
It is the act of selectively evaluate parts of an otherwise unevaluated expression.