Thanks Dirk for your response. The "base" R code is just too unwieldy to be easily understandable. The eval(parse(sprintf())) solution provided is ungainly too, but at least its more understandable from a casual reading of it.
I'll punt this case over to the dplyr people then. Cheers. Mateo. -- Mateo Obregón On Friday, 29 December 2023 15:35:20 GMT Dirk Eddelbuettel wrote: > On 29 December 2023 at 14:13, Mateo Obregón wrote: > | Hi all- > | > | Looking through stackoverflow for R string combining examples, I found the > | following from 3 years ago: > | > | <https://stackoverflow.com/questions/63881854/how-to-format-strings-using-> > | values-from-other-column-in-r> > | > | The top answer suggests to use eval(parse(sprintf())). I tried the > | suggestion > Well: > > fortunes::fortune(106) > > If the answer is parse() you should usually rethink the question. > -- Thomas Lumley > R-help (February 2005) > > | and it did not return the expected combines strings. I thought that this > | might be an issue with some leftover values being reused, so I explicitly > | eval()| > | with a new.env(): > | > library(dplyr) > | > df <- tibble(words=c("%s plus %s equals %s"), > | > | args=c("1,1,2","2,2,4","3,3,6")) > | > | > df |> mutate(combined = eval(parse(text=sprintf("sprintf('%s', %s)", > | > words, > | > | args)), envir=new.env())) > | > | # A tibble: 3 × 3 > | > | words args combined > | <chr> <chr> <chr> > | > | 1 %s plus %s equals %s 1,1,2 3 plus 3 equals 6 > | 2 %s plus %s equals %s 2,2,4 3 plus 3 equals 6 > | 3 %s plus %s equals %s 3,3,6 3 plus 3 equals 6 > | > | The `combined` is not what I was expecting, as the same last eval() is > | returned for all three rows. > | > | Am I missing something? What has changed in the past three years? > > Nothing if you use the first answer which relies only on base R and still > > works: > > words <- c('%s + %s equal %s', '%s + %s equal %s') > > arguments <- c('1,1,2', '2,2,4') > > df <- data.frame(words, arguments) > > df > > words arguments > 1 %s + %s equal %s 1,1,2 > 2 %s + %s equal %s 2,2,4 > > > df$combined <- apply(df, 1, function(x) do.call(sprintf, > > c(as.list(strsplit(x[2], ',')[[1]]), fmt = x[[1]]))) df > > words arguments combined > 1 %s + %s equal %s 1,1,2 1 + 1 equal 2 > 2 %s + %s equal %s 2,2,4 2 + 2 equal 4 > > > I am not the best person to answer what may have changed in `dplyr` in those > three years -- and neither is this list which is primarily concerned with > developing R itself. > > Dirk ______________________________________________ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel