better string interpolation in R

biocyberman picture biocyberman · Jun 8, 2015 · Viewed 7k times · Source

I need to build up long command lines in R and pass them to system(). I find it is very inconvenient to use paste0/paste function, or even sprintf function to build each command line. Is there a simpler way to do like this:

Instead of this hard-to-read-and-too-many-quotes:

cmd <- paste("command", "-a", line$elem1, "-b", line$elem3, "-f", df$Colum5[4])

or:

cmd <- sprintf("command -a %s -b %s -f %s", line$elem1, line$elem3, df$Colum5[4])

Can I have this:

cmd <- buildcommand("command -a %line$elem1 -b %line$elem3 -f %df$Colum5[4]")

Answer

Holger Brandl picture Holger Brandl · Jun 16, 2017

For a tidyverse solution see https://github.com/tidyverse/glue. Example

name="Foo Bar"
glue::glue("How do you do, {name}?")