In case the data = (.)
is missing in a call to a "data-dot"
function (a function that defines that its first argument data=
could be
missing, and it this case, it is considered to be .
), this function does
the injection of the first argument in the call and evaluate it.
A call object, usually a function call.
The name of the argument to inject, usually 'data' or '.data'.
The value to inject, usually the symbol '.'.
The environment where the evaluation of the data-dot-injected call
should be evaluated (by default, caller_env(2L)
, should rarely be
changed).
The message to use in case the 'data' argument is wrong.
An additional message to append to the error message in
case data-dot-injection is not permitted (when
.SciViews.implicit.data.dot != TRUE
)
The environment to use for the error message, by default, the caller environment (should rarely be changed).
The result from evaluating the data-dot-injected call.
The call is not checked if it is a correct function call. When called
from within a function, passing sys.call()
as call
, it should be always
correct.
# Here is how you create a data-dot function
my_subset <- function(data = (.), i, j) {
if (!is.data.frame(data))
return(eval_data_dot(sys.call()))
# Code of the function
# Second argument (i here) must not be a data.frame to avoid confusion
data[i, j]
}
dtf1 <- data.frame(x = 1:3, y = 4:6)
my_subset(dtf1, 1, 'y')
#> [1] 4
# If data is in '.', it can be omitted
.= dtf1
my_subset(1, 'y')
#> [1] 4
# This mechanism is potentially confusing. You can inactivate it anywhere:
.SciViews.implicit.data.dot <- FALSE
# This time next call is wrong
#my_subset(1, 'y')
rm(.SciViews.implicit.data.dot) # Reactivate it
my_subset(1, 'y')
#> [1] 4
# Note that, if you have not defined '.' and try to use it, you got
# an error: uncomment next line to see it
#get_temp(".")