R Flashcards

1
Q

What is an expression?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Name the elements of an expression:

A

Constants, Symbols, Call objects and pairlists.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a Call Object?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a constant?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a Symbol?

A

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”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

rlang::expr()

A

returns its argument unevaluated. It is equivalent to base::bquote()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

rlang::is_syntactic_literal(x)

A

checks if x is a constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

rlang::sym()

A

creates a symbol from a string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

rlang::call_standardise()

A

Standardize the call to use complete argument names.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

rlang::call2()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Explain rlang::enexpr() vs rlang::expr().

A

Used to capture the code passed to a function call. As opposed to rlang::expr() which will capture whatever is typed within rlang::expr().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

rlang::eval_tidy()

A

About controlling the evaluation of code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is datamasking?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does parsing mean?

A

The process by which a computer language takes a string and constructs an expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Explain the differences between calls and expressions.

A

An expression can be made up of call objects and so call objects are an element of an expression.

17
Q

In the context of R, what is meant by Associativity? Mention two important operators that are special cases in R. Why should you care?

A

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.

18
Q

Are + associative in ggplot2?

A

No, ggplot2 uses + to draw geoms on top of eachother, i.e. geom_point + geom_smooth != geom_smooth + geom_point.

19
Q

rlang::parse_expr()

A

Takes a string and creates an expression.

is.call(rlang::parse_expr()) always evaluates to TRUE.

20
Q

What is deparsing?

A

The process of taking an expression and turning it in to a string.

21
Q

rlang::expr_text()

A

Deparses an expression.

22
Q

How is := used in tidy evaluation?

A

The colon-equals operator allows for having expressions as argument names.

23
Q

What is quotation?

A

It is the act of capturing an unevaluated expression.

24
Q

What is unquotation?

A

It is the act of selectively evaluate parts of an otherwise unevaluated expression.

25
Q

Defusing is a.k.a.

A

Quoting

26
Q

Why quote/defuse evaluation of expressions?

A

Enable data-masking. This means that an expression can be evaluated in the context of a data-frame instead of following standard evaluation rules.

27
Q

Explain the difference between data-variables and environment-variables.

A

Environment-variables are defined in the global environment or in a function and data-variables are column names in a data-frame.

28
Q

The rlang equivalent of substitute()? What do they do differently?

A

What is enquote?

enquote captures what the user has provided as arguments to a function.

substitute does also this is certain situations, but it also substitutes the arguments, if it finds a binding in the current environment provided.

29
Q

What does !! (bang-bang operator) do?

A

It unquotes its input, i.e. it takes the expression and inserts its value, i.e. evaluates it.

x = expr(!!x)

30
Q

Explain how !! behaves with call objects vs constants and symbols.

A

Constants and symbols are inserted for the right hand side of !!. Call objects are first evaluated then inserted.

31
Q

What does the following code do

dfs

A

Equivalent to
dplyr::bind_rows(a,b)

The use of !!! in this context is known as splatting. It uses unquotation to insert the elements of a list into a function as arguments.

32
Q

Explain what happens in the code below

x

A

Apply the same arguments to different functions.

purrr::map_dbl(.x, .f, …) takes an input, .x, and transform it with the function .f. The input here is the vector of function names, c(“mean”, “median”, “sd”). The function applied to this vector is the rlang::exec(.fn, …) function. Excec evaluates the function provided here each member of the vector of function names. Additional arguments, x (the numbers to compute on) and na.rm =T are passed in via the … argument in purrr::map_dbl to excec’s … argument and lastly to the particular function call.

33
Q

What is a quosure?

A

a data structure that captures an expression along with its associated environment, as found in function arguments.

34
Q

What is quasiquotation?

A

Quotation is the act of capturing an unevaluated expression and unquotation is the ability to selectively evaluate parts of an otherwise quoted expression. Together, this is called quasiquotation.