Julia style string literal interpolation in R
[This article was first published on Econometrics by Simulation, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I feel like a sculptor who has been using the same metal tools for the last four years and happened to have looked at my comrades and found them sporting new, sleek electric tools. Suddenly all of the hard work put into maintaining and adapting my metal tools ends up looking like duck tape and bubble gum patches.
I hate to say it but I feel that I have become somewhat infatuated with Julia. And infatuation is the right word. I have not yet committed the time to fully immerse myself in the language, yet everything I know about it makes me want to learn more. The language is well known for its mind-blowingly speed accomplished through just-in-time compiling. It also has many features which enhance the efficiency and readability of its code (see previous post, note the documentation has greatly improved since posting).
However, though I very much want to, I cannot entirely switch my coding needs from R into Julia. This is primarily due to my ongoing usage of packages such as RStudio’s “Shiny” and the University of Cambridge’s server side software for building adaptive tests, “Concerto”. And so with regret I will resign my Julia coding to probably a minor portion of my programming needs.
That does not mean however that I can’t make some small changes to make R work more like Julia. To this end I have programmed a small function p which will replace string literals identified as “Hello #(name), how are you?” with their values being evaluated. If there are nested parenthesizes then it is necessary to close the literal with “)#”, for example “c=#(b^(1+a))#”.
Thank you SO community for your help.
I hate to say it but I feel that I have become somewhat infatuated with Julia. And infatuation is the right word. I have not yet committed the time to fully immerse myself in the language, yet everything I know about it makes me want to learn more. The language is well known for its mind-blowingly speed accomplished through just-in-time compiling. It also has many features which enhance the efficiency and readability of its code (see previous post, note the documentation has greatly improved since posting).
However, though I very much want to, I cannot entirely switch my coding needs from R into Julia. This is primarily due to my ongoing usage of packages such as RStudio’s “Shiny” and the University of Cambridge’s server side software for building adaptive tests, “Concerto”. And so with regret I will resign my Julia coding to probably a minor portion of my programming needs.
That does not mean however that I can’t make some small changes to make R work more like Julia. To this end I have programmed a small function p which will replace string literals identified as “Hello #(name), how are you?” with their values being evaluated. If there are nested parenthesizes then it is necessary to close the literal with “)#”, for example “c=#(b^(1+a))#”.
# Julia like text concaction function. p <- function(..., sep="", esc="#") { # Change escape characters by specifying esc. # Break the input values into different strings cut at '#(' x <- paste(..., sep=sep) x <- unlist(strsplit(x, paste0(esc,"("), fixed = TRUE)) # The first element is never evaluated. out <- x[1] # Check if x has been split. if (length(x)>1) for (i in 2:length(x)) { y <- unlist(strsplit(x[i], paste0(")",esc), fixed = TRUE)) if (x[i]==y[1]) y <- unlist(regmatches(x[i], regexpr(")", x[i]), invert = TRUE)) out <- paste0(out, eval(parse(text=y[1])), y[-1]) } out } name="Bob" height=72 weight=230 # Let's see it in action p(sep=" ", "Hello #(name).", "My record indicates you are #(height) inches tall and weigh #(weight) pounds.", "Your body mass index is #(round(703*weight/height^2,1))#") # [1] "Hello Bob. My record indicates you are 72 inches tall and weigh 230 pounds. # Your body mass index is 31.2" # The other nice thing about the p function is that it can be used to concat # strings as a shortcut for paste0. p("QRS","TUV") # [1] "QRSTUV"
Thank you SO community for your help.
To leave a comment for the author, please follow the link and comment on their blog: Econometrics by Simulation.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.