# Substitution Modes

## The substitution modes

wrapr::let() now has three substitution implementations:

• Language substitution (subsMethod='langsubs' the new default). In this mode user code is captured as an abstract syntax tree (or parse tree) and substitution is performed only on nodes known to be symbols or behaving in a symbol-role ("X" in d$"X" is one such example). • Substitute substitution (subsMethod='subsubs'). In this mode substitution is performed by R’s own base::substitute(). • String substitution (subsMethod='stringsubs', the previous default now deprecated). In this mode user code is captured as text and then string replacement on word-boundaries is used to substitute in variable re-mappings. The semantics of the three methods can be illustrated by showing the effects of substituting the variable name “y” for “X” and the function “sin” for “F” in the somewhat complicated block of statements:  { d <- data.frame("X" = "X", X2 = "XX", d = X*X, .X = X_) X <- list(X = d$X, X2 = d$"X", v1 = X, v2 =  X, F(1:2)) X$a
"X"$a X = function(X, ...) { X + 1 } } This block a lot of different examples and corner-cases. #### Language substitution (subsMethod='langsubs') library("wrapr") let( c(X = 'y', F = 'sin'), { d <- data.frame("X" = "X", X2 = "XX", d = X*X, .X = X_) X <- list(X = d$X, X2 = d$"X", v1 = X, v2 =  X, F(1:2)) X$a
"X"$a X = function(X, ...) { X + 1 } }, eval = FALSE, subsMethod = 'langsubs') ## { ## d <- data.frame(y = "X", X2 = "XX", d = y * y, .X = X_) ## y <- list(y = d$y, X2 = d$y, v1 = y, v2 =  X, sin(1:2)) ## y$a
##     "X"$a ## y = function(y, ...) { ## y + 1 ## } ## } Notice the substitution replaced all symbol-like uses of “X”, and only these (including correctly working with some that were quoted!). #### String substitution (subsMethod='stringsubs') let( c(X = 'y', F = 'sin'), { d <- data.frame("X" = "X", X2 = "XX", d = X*X, .X = X_) X <- list(X = d$X, X2 = d$"X", v1 = X, v2 =  X, F(1:2)) X$a
"X"$a X = function(X, ...) { X + 1 } }, eval = FALSE, subsMethod = 'stringsubs') ## expression({ ## d <- data.frame(y = "y", X2 = "XX", d = y * y, .y = X_) ## y <- list(y = d$y, X2 = d$y, v1 = y, v2 =  y, sin(1:2)) ## y$a
##     "y"$a ## y = function(y, ...) { ## y + 1 ## } ## }) Notice string substitution has a few flaws: it went after variable names that appeared to start with a word-boundary (the cases where the variable name started with a dot or a space). Substitution also occurred in some string constants (which as we have seen could be considered a good thing). These situations are all avoidable as both the code inside the let-block and the substitution targets are chosen by the programmer, so they can be chosen to be simple and mutually consistent. We suggest “ALL_CAPS” style substitution targets as they jump out as being macro targets. But, of course, it is better to have stricter control on substitution. Think of the language substitution implementation as a lower-bound on a perfect implementation (cautious, with a few corner cases to get coverage) and string substitution as an upper bound on a perfect implementation (aggressive, with a few over-reaches). #### Substitute substitution (subsMethod='subsubs') let(c(X = 'y', F = 'sin'), { d <- data.frame("X" = "X", X2 = "XX", d = X*X, .X = X_) X <- list(X = d$X, X2 = d$"X", v1 = X, v2 =  X, F(1:2)) X$a
"X"$a X = function(X, ...) { X + 1 } }, eval = FALSE, subsMethod = 'subsubs') ## { ## d <- data.frame(X = "X", X2 = "XX", d = y * y, .X = X_) ## y <- list(X = d$y, X2 = d$X, v1 = y, v2 =  X, sin(1:2)) ## y$a
##     "X"$a ## y = function(X, ...) { ## y + 1 ## } ## } Notice base::substitute() doesn’t re-write left-hand-sides of argument bindings. This is why I originally didn’t consider using this implementation. Re-writing left-hand-sides of assignments is critical in expressions such as dplyr::mutate( RESULTCOL = INPUTCOL + 1). Also base::substitute() doesn’t special case the d$"X" situation (but that really isn’t very important).

## Conclusion

wrapr::let() when used prudently is a safe and powerful tool.
{"ft_lang_label":"__label__en","ft_lang_prob":0.77593,"math_prob":0.9897651,"size":3835,"snap":"2023-14-2023-23","text_gpt3_token_len":1212,"char_repetition_ratio":0.121117204,"word_repetition_ratio":0.40495867,"special_character_ratio":0.37340286,"punctuation_ratio":0.18401937,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99976104,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-03-27T10:10:29Z\",\"WARC-Record-ID\":\"<urn:uuid:cbf0bf13-d779-4fbb-8e89-3539a15d9d98>\",\"Content-Length\":\"24800\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:427b0891-29d0-47d8-af58-7e45cf74ae83>\",\"WARC-Concurrent-To\":\"<urn:uuid:ffc1f959-4adc-4fa6-b665-5ce47033fd46>\",\"WARC-IP-Address\":\"40.118.246.51\",\"WARC-Target-URI\":\"https://mran.revolutionanalytics.com/snapshot/2021-11-01/web/packages/wrapr/vignettes/SubstitutionModes.html\",\"WARC-Payload-Digest\":\"sha1:QOICB6U6RPPA726KMZK7IPUZGO5W7CHW\",\"WARC-Block-Digest\":\"sha1:XC5FMEMRS44SDCY3CVMTZRI66RPY42QI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296948620.60_warc_CC-MAIN-20230327092225-20230327122225-00441.warc.gz\"}"}