Strength of a Lennon song exposed with R function glue::glue

[This article was first published on Guillaume Pressiat, 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.


This post is an actualisation with addendum below.

love_verse <- function(w1, w2, w3){
  glue::glue(
  "Love is {b}, {b} is love
   Love is {y}, {y} love
   Love is {u} to be loved", 
  b = w1, y = w2, u = w3)
}

As a return, parameters sometimes give echoes of poetry.

love_verse('real', 'feeling', 'wanting')
Love is real, real is love
Love is feeling, feeling love
Love is wanting to be loved
love_verse('touch', 'reaching', 'asking')
Love is touch, touch is love
Love is reaching, reaching love
Love is asking to be loved
## refrain
Love is you
You and me
Love is knowing
We can be
love_verse('free', 'living', 'needing')
Love is free, free is love
Love is living, living love
Love is needing to be loved

https://www.youtube.com/watch?v=7er_xx7Wmg8

list(list(w1 = 'real',  w2 = 'feeling',  w3 = 'wanting'),
     list(w1 = 'touch', w2 = 'reaching', w3 = 'asking' ),
     list(w1 = 'free',  w2 = 'living',   w3 = 'needing')) %>% 
  purrr::map(function(x)do.call(love_verse, x))
[[1]]
Love is real, real is love
Love is feeling, feeling love
Love is wanting to be loved

[[2]]
Love is touch, touch is love
Love is reaching, reaching love
Love is asking to be loved

[[3]]
Love is free, free is love
Love is living, living love
Love is needing to be loved

We could also read title of this article as “strength of an R function exposed with a Lennon song”…

If you like it, you can view this code as a gist here.

Addendum

In a more tidyverse way, you could also do this last piping block step using glue_data approach, what I have not thinked about.
I haven’t been able to think theses words will fit in a tibble, which is why I have used do.call which sounds a little like old.school!

With a little help from Jim Hester, it’s getting better:

library(magrittr)

tibble::tribble(
  ~a, ~b, ~c,
  "real", "feeling", "wanting",
  "touch", "reaching", "asking",
  "free", "living", "needing"
  ) %>%
  glue::glue_data(
  "Love is {a}, {a} is love
   Love is {b}, {b} love
   Love is {c} to be loved",
  ) %>%
  glue::glue_collapse("\n\n")
#> Love is real, real is love
#> Love is feeling, feeling love
#> Love is wanting to be loved
#> 
#> Love is touch, touch is love
#> Love is reaching, reaching love
#> Love is asking to be loved
#> 
#> Love is free, free is love
#> Love is living, living love
#> Love is needing to be loved

To leave a comment for the author, please follow the link and comment on their blog: Guillaume Pressiat.

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)