Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R functions glue vs paste

Tags:

string

r

I recently discovered the glue package. After a little bit of research I couldn't figure out why glue::glue should be preferred over paste:

  • According to this site, glue is slower than paste.
  • The syntax of glue is similar and in no way easier to read than the syntax of paste.

However, in the link from above, the authors say that paste does not do string interpolation (only string insertion). I don't really know what this means.

Can some clarify why the glue package is interesting and what is meant by string insertion?

Thank you in advance.

EDIT: After the first feedback I want to ask a more precise question.

Are there any situations where one of paste or glue would clearly be preferred over the other?

I am trying to understand why someone put in the effort to create the glue package.

like image 948
Cettt Avatar asked Oct 29 '18 16:10

Cettt


People also ask

What does glue function do in R?

Overview. Glue offers interpreted string literals that are small, fast, and dependency-free. Glue does this by embedding R expressions in curly braces which are then evaluated and inserted into the argument string.

What is glue function?

Adhesive, also known as glue, cement, mucilage, or paste, is any non-metallic substance applied to one or both surfaces of two separate items that binds them together and resists their separation.

What is R package glue?

Package {glue} is designed as “small, fast, dependency free” tools to “glue strings to data in R”. To put simply, it provides concise and flexible alternatives for. paste() with some additional features: library(glue)


1 Answers

Wikipedia says :

In computer programming, string interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.

paste doesn't look into the strings that are passed to it to replace some characters by others, that's why it doesn't do interpolation.

According to this definition I'm not sure why one would say sprintf doesn't do interpolation (as it's mentioned on your link), it might be because the placeholders are not explicit as they are in glue, but I think this assertion is debatable.

Your question about why glue is good is unfortunately out of scope here as it's a matter of opinion.

like image 199
Moody_Mudskipper Avatar answered Nov 03 '22 18:11

Moody_Mudskipper