let implements a mapping from desired names (names used directly in the expr code) to names used in the data. Mnemonic: "expr code symbols are on the left, external data and function argument names are on the right."

let(alias, expr, ..., envir = parent.frame(), subsMethod = "langsubs",
  strict = TRUE, eval = TRUE, debugPrint = FALSE)

Arguments

alias

mapping from free names in expr to target names to use (mapping have both unique names and unique values).

expr

block to prepare for execution.

...

force later arguments to be bound by name.

envir

environment to work in.

subsMethod

character substitution method, one of 'langsubs' (preferred), 'subsubs', or 'stringsubs'.

strict

logical if TRUE names and values must be valid un-quoted names, and not dot.

eval

logical if TRUE execute the re-mapped expression (else return it).

debugPrint

logical if TRUE print debugging information when in stringsubs mode.

Value

result of expr executed in calling environment (or expression if eval==FALSE).

Details

Please see the wrapr vignette for some discussion of let and crossing function call boundaries: vignette('wrapr','wrapr'). Transformation is performed by substitution, so please be wary of name collisions or aliasing.

Something like let is only useful to get control of a function that is parameterized (in the sense it take column names) but non-standard (in that it takes column names from non-standard evaluation argument name capture, and not as simple variables or parameters). So wrapr:let is not useful for non-parameterized functions (functions that work only over values such as base::sum), and not useful for functions take parameters in straightforward way (such as base::merge's "by" argument). dplyr::mutate is an example where we can use a let helper. dplyr::mutate is parameterized (in the sense it can work over user supplied columns and expressions), but column names are captured through non-standard evaluation (and it rapidly becomes unwieldy to use complex formulas with the standard evaluation equivalent dplyr::mutate_). alias can not include the symbol ".".

The intent from is from the user perspective to have (if a <- 1; b <- 2): let(c(z = 'a'), z+b) to behave a lot like eval(substitute(z+b, c(z=quote(a)))). let deliberately checks that it is mapping only to legal R names; this is to discourage the use of let to make names to arbitrary values, as that is the more properly left to R's environment systems. let is intended to transform "tame" variable and column names to "tame" variable and column names. Substitution outcomes that are not valid simple R variable names (produced with out use of back-ticks) are forbidden. It is suggested that substitution targets be written ALL_CAPS style to make them stand out.

Examples

d <- data.frame(Sepal_Length=c(5.8,5.7), Sepal_Width=c(4.0,4.4), Species='setosa', rank=c(1,2)) RANKCOLUMN <- NULL # optional, make sure macro target does not look like unbound variable. GROUPCOLUMN <- NULL # optional, make sure macro target does not look like unbound variable. mapping = c(RANKCOLUMN= 'rank', GROUPCOLUMN= 'Species') let(alias = mapping, expr = { # Notice code here can be written in terms of known or concrete # names "RANKCOLUMN" and "GROUPCOLUMN", but executes as if we # had written mapping specified columns "rank" and "Species". # restart ranks at zero. dres <- d dres$RANKCOLUMN <- dres$RANKCOLUMN - 1 # notice, using $ not [[]] # confirm set of groups. groups <- unique(d$GROUPCOLUMN) }, debugPrint = TRUE )
#> $RANKCOLUMN #> [1] "rank" #> #> $GROUPCOLUMN #> [1] "Species" #> #> { #> dres <- d #> dres$rank <- dres$rank - 1 #> groups <- unique(d$Species) #> }
print(groups)
#> [1] setosa #> Levels: setosa
print(length(groups))
#> [1] 1
print(dres)
#> Sepal_Length Sepal_Width Species rank #> 1 5.8 4.0 setosa 0 #> 2 5.7 4.4 setosa 1