Skip to content

Commit 19ab67b

Browse files
committed
work on string interpolation
make wrapr pipe a bit stricter on piping into values / code
1 parent 0330b99 commit 19ab67b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+663
-98
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Package: wrapr
22
Type: Package
33
Title: Wrap R Tools for Debugging and Parametric Programming
44
Version: 2.0.0
5-
Date: 2020-03-16
5+
Date: 2020-03-17
66
Authors@R: c(
77
person("John", "Mount", email = "[email protected]", role = c("aut", "cre")),
88
person("Nina", "Zumel", email = "[email protected]", role = c("aut")),

NAMESPACE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ export("%.%")
3232
export("%.>%")
3333
export("%.|%")
3434
export("%:=%")
35+
export("%<s%")
3536
export("%>.%")
3637
export("%?%")
3738
export("%c%")
3839
export("%dot%")
3940
export("%in_block%")
4041
export("%p%")
4142
export("%qc%")
42-
export("%s%")
43+
export("%s>%")
4344
export("%|.%")
4445
export(":=")
4546
export(DebugFn)

NEWS.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11

2-
# wrapr 2.0.0 2020-03-16
2+
# wrapr 2.0.0 2020-03-17
33

44
* Allow as_named_list to choose new names.
55
* Update references.
66
* Stricter tests on unpack/to.
7+
* Stricter wrapr-pipe value checks.
78
* := for names.
89
* := for to/unpack.
910
* Don't use := for anonymous function construction.
1011
* Update evalb, and bquote uses including adding .(-) notation.
1112
* Remove some obsolete methods such as CapturePipeine, as_dot_fn, UnaryFunctions/ApplyTo, and locum.
12-
* Make pipe_impl not internal and document more.
13+
* Make pipe_impl public, and document more.
14+
* Vectorize string interpolation and add operator versions.
1315
* Fix description.
1416

1517
# wrapr 1.9.6 2020-01-26

R/bpipe.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ forbidden_pipe_destination_names <- c("else",
7676
"?",
7777
"...",
7878
".",
79-
";", ",")
79+
";", ",",
80+
"list", "c", ":",
81+
"for")
8082

8183
#' S3 dispatch on class of pipe_left_arg.
8284
#'

R/string_interpolation.R

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ strsplit_capture <- function(x, split,
6767
#' and \url{https://CRAN.R-project.org/package=glue}.
6868
#'
6969
#'
70-
#' @param str charater string to be substituted into
70+
#' @param str charater string(s) to be substituted into
7171
#' @param ... force later arguments to bind by name
7272
#' @param envir environemnt to look for values
7373
#' @param enclos enclosing evaluation environment
@@ -91,7 +91,7 @@ strsplit_capture <- function(x, split,
9191
#'
9292
#' # We can also change the delimiters,
9393
#' # in this case to !! through the first whitespace.
94-
#' sinterp(c("x is !!x , x+1 is !!x+1\n!!x is odd is !!x%%2==1"),
94+
#' sinterp(c("x is !!x , x+1 is !!x+1 \n!!x is odd is !!x%%2==1"),
9595
#' match_pattern = '!![^[:space:]]+[[:space:]]?',
9696
#' removal_patterns = c("^!!", "[[:space:]]?$"))
9797
#'
@@ -115,27 +115,37 @@ sinterp <- function(str,
115115
if(!is.character(str)) {
116116
stop("wrapr::sinterp str must be of class character")
117117
}
118-
if(length(str)!=1) {
119-
stop("wrapr::sinterp str length must be 1")
118+
if(length(str) <= 0) {
119+
stop("wrapr::sinterp str must be of length at least 1")
120120
}
121-
pi <- strsplit_capture(str, match_pattern)
122-
npi <- length(pi)
123-
xlated <- list()
124-
for(j in seq_len(npi)) {
125-
pij <- pi[[j]]
126-
if(!isTRUE(attr(pij, "is_sep", exact = TRUE))) {
127-
xlated <- c(xlated, list(as.character(pij))) # strip attributes.
128-
} else {
129-
expr <- as.character(pij) # strip attributes.
130-
for(rp in removal_patterns) {
131-
expr <- as.character(gsub(rp, "", expr))
121+
orig_names <- names(str)
122+
res <- vapply(
123+
str,
124+
function(stri) {
125+
pi <- strsplit_capture(stri, match_pattern)
126+
npi <- length(pi)
127+
xlated <- list()
128+
for(j in seq_len(npi)) {
129+
pij <- pi[[j]]
130+
if(!isTRUE(attr(pij, "is_sep", exact = TRUE))) {
131+
xlated <- c(xlated, list(as.character(pij))) # strip attributes.
132+
} else {
133+
expr <- as.character(pij) # strip attributes.
134+
for(rp in removal_patterns) {
135+
expr <- as.character(gsub(rp, "", expr))
136+
}
137+
val <- eval(parse(text = expr), envir = envir, enclos = enclos)
138+
val <- deparse(val)
139+
xlated <- c(xlated, list(val))
140+
}
132141
}
133-
val <- eval(parse(text = expr), envir = envir, enclos = enclos)
134-
val <- as.character(val)
135-
xlated <- c(xlated, list(val))
136-
}
142+
do.call(paste0, xlated)
143+
},
144+
character(1))
145+
if(length(orig_names) <= 0) {
146+
names(res) <- NULL
137147
}
138-
do.call(paste0, xlated)
148+
return(res)
139149
}
140150

141151

@@ -174,14 +184,16 @@ sinterp <- function(str,
174184
#'
175185
#' # We can also change the delimiters,
176186
#' # in this case to !! through the first whitespace.
177-
#' si(c("x is !!x , x+1 is !!x+1\n!!x is odd is !!x%%2==1"),
178-
#' match_pattern = '!![^[:space:]]+[[:space:]]?',
179-
#' removal_patterns = c("^!!", "[[:space:]]?$"))
187+
#' si(c("x is !!x , x+1 is !!x+1 \n!!x is odd is !!x%%2==1"),
188+
#' match_pattern = '!![^[:space:]]+[[:space:]]?',
189+
#' removal_patterns = c("^!!", "[[:space:]]?$"))
190+
#'
180191
#'
181192
#' @export
182193
#'
183194
si <- sinterp
184195

196+
185197
#' Dot substitution string interpolation.
186198
#'
187199
#' String interpolation using \code{bquote}-stype .() notation. Pure R, no C/C++ code called.
@@ -200,12 +212,41 @@ si <- sinterp
200212
#'
201213
#' @examples
202214
#'
203-
#' "x is .(x), x+1 is .(x+1)\n.(x) is odd is .(x%%2 == 1)" %s% list(x = 7)
215+
#' "x is .(x)" %<s% list(x = 7)
216+
#'
217+
#'
218+
#' @export
219+
#'
220+
`%<s%` <- function(str, envir) {
221+
force(envir)
222+
sinterp(str, envir = envir, enclos = envir)
223+
}
224+
225+
#' Dot substitution string interpolation.
226+
#'
227+
#' String interpolation using \code{bquote}-stype .() notation. Pure R, no C/C++ code called.
228+
#'
229+
#' See also
230+
#' \url{https://CRAN.R-project.org/package=R.utils},
231+
#' \url{https://CRAN.R-project.org/package=rprintf},
232+
#' and \url{https://CRAN.R-project.org/package=glue}.
233+
#'
234+
#'
235+
#' @param envir environemnt to look for values
236+
#' @param str charater string to be substituted into
237+
#' @return modified strings
238+
#'
239+
#' @seealso \code{\link{strsplit_capture}}, \code{\link{si}}
240+
#'
241+
#' @examples
242+
#'
243+
#' list(x = 7) %s>% "x is .(x)"
204244
#'
205245
#'
206246
#' @export
207247
#'
208-
`%s%` <- function(str, envir) {
248+
`%s>%` <- function(envir, str) {
209249
force(envir)
210250
sinterp(str, envir = envir, enclos = envir)
211251
}
252+

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ library(wrapr)
6262
packageVersion("wrapr")
6363
# [1] '2.0.0'
6464
date()
65-
# [1] "Mon Mar 16 22:01:15 2020"
65+
# [1] "Tue Mar 17 05:47:19 2020"
6666
```
6767

6868
## [`%.>%` (dot pipe or dot arrow)](https://winvector.github.io/wrapr/articles/dot_pipe.html)
@@ -516,7 +516,7 @@ evalb(
516516

517517
# alter string
518518
si("plot(x = .(variable), y = .(variable))")
519-
# [1] "plot(x = angle, y = angle)"
519+
# [1] "plot(x = \"angle\", y = \"angle\")"
520520
```
521521

522522
The extra `.(-x)` form is a shortcut for

docs/articles/CornerCases.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/DebugFnW.html

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/FrameTools.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/Named_Arguments.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)