Skip to content

Commit 0b5a4c4

Browse files
amhrasmussenhadley
authored andcommitted
Pass SIMPLIFY = FALSE to mapply()(#213)
Make eval_interp_matches return more consistent answers by using SIMPLIFY=FALSE in mapply. This should make str_interp behave more consistently when interpolating lists into placeholders.
1 parent a941cf4 commit 0b5a4c4

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# stringr 1.3.1.9000
22

3+
* `str_interp()` now renders lists consistently independent on the presence of
4+
additional placeholders (@amhrasmussen)
5+
36
# stringr 1.3.1
47

58
* `str_replace_all()` with a named vector now respects modifier functions (#207)

R/interp.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ eval_interp_matches <- function(matches, env) {
143143
formats <- extract_formats(matches)
144144

145145
# Format the values and return.
146-
mapply(sprintf, formats, values)
146+
mapply(sprintf, formats, values, SIMPLIFY = FALSE)
147147
}
148148

149149
#' Extract Expression Objects from String Interpolation Matches

tests/testthat/test-interp.r

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,12 @@ test_that("str_interp fails when encountering nested placeholders", {
6868
test_that("str_interp fails when input is not a character string", {
6969
expect_error(str_interp(3L))
7070
})
71+
72+
test_that("str_interp formats list independetly of other placeholders", {
73+
a_list <- c("item1", "item2", "item3")
74+
other <- "1"
75+
extract <- function(text) regmatches(text, regexpr("xx[^x]+xx", text))
76+
from_list <- extract(str_interp("list: xx${a_list}xx"))
77+
from_both <- extract(str_interp("list: xx${a_list}xx, and another ${other}"))
78+
expect_equal(from_list, from_both)
79+
})

0 commit comments

Comments
 (0)