Skip to content

Instantly share code, notes, and snippets.

@GuillaumePressiat
Created March 24, 2019 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GuillaumePressiat/f2d4ac48b12920ef501a00abf5a2fb99 to your computer and use it in GitHub Desktop.
Save GuillaumePressiat/f2d4ac48b12920ef501a00abf5a2fb99 to your computer and use it in GitHub Desktop.
Strength of a Lennon song exposed with R function glue::glue
library(stringfix)
library(magrittr)
one <- c('love', 'is', 'real')
(one %c% ' ') %,% (rev(one) %c% ' ')
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)
}
love_verse('real', 'feeling', 'wanting')
love_verse('touch', 'reaching', 'asking')
love_verse('free', 'living', 'needing')
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))
@GuillaumePressiat
Copy link
Author

@jimhester
Copy link

jimhester commented Mar 24, 2019

@GuillaumePressiat An alternative implementation, taking advantage of the fact glue() and glue_data() are vectorized.

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

Created on 2019-03-24 by the reprex package (v0.2.1)

@GuillaumePressiat
Copy link
Author

@jimhester Thank you !
This is nice indeed. Tidy way.
In fact I use glue_data often but have not thinked theses words will fit in a 3*3 tibble/tribble for using that way !
I will update blogpost with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment