Site icon R-bloggers

Galentine’s day cards

[This article was first published on Maëlle's R blog on Maëlle Salmon's personal website, 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.

Remember the nascent series of blog posts about Parks and recreation? Well, we’re still at one post, but don’t worry, here is a new one, and I’m sure the series will eventually be a real one. I’m looking at you, my R-Ladies friends. That said, today is not a day for passive agressive hints, because I’ve decided it’s Galentine’s day and I’ll show you how to craft cards for your R-Ladies friends from your R prompt!

Galentine’s day is a celebration Leslie Knope throws every year for her female friends, showering them with love and gifts. Since she’s very talented, she can even write them poems, but in the R version of Galentine’s day, we’ll make do with simple cards featuring Lesliesque compliments. Indeed, Leslie Knope is also known for her creative complimenting of her best friend Ann. I’ve decided to adopt her tone by using three praising adjectives followed by an animal name, and to dedicate them to imaginary friends called Ann, Donna, April and Shauna. Had I not decided to steal names from the TV show, I could have used the charlatan::ch_name() from the rOpenSci charlatan package, possibly extracting first names thanks to the humaniformat package.

Create the compliments

Here I need animal names and praising adjectives for a pretty frivolous use case. Now sometimes you could want to randomly draw words to make identifiers for, say, participants in your epidemiological study because that’d be easier to deal with than numbers. The ids package actually provides human readable identifiers in the style <adjective>_<animal> (following gfycat.com) but sadly that adjective wouldn’t necessary be positive, so I’ll use the praise package to get adjectives and the rcorpora package to get animal names. I’ll set the random seed to ensure reproducibility, not forgetting to be careful about where to generate it.

set.seed(123)

compliment <- function(name, animal){
  praise::praise(paste0(name, ", you ${adjective}, ${adjective}, ${adjective} ", animal))
}

compliments <- tibble::tibble(name = c("Ann", "April", "Donna", "Shauna"),
                              animal = sample(rcorpora::corpora(which = "animals/common")$animals, size = 4),
                              compliment = purrr::map2_chr(name, animal, compliment))

Create the cards

I first thought I could use cute animal pics for the cards, my husband even suggested I use the animal from the compliment which I could have done using Pexels as in my bubblegum puppies post or my rainbowing post, using the gist posted by Bob Rudis to avoid using RSelenium, but I decided to go a simpler route (which I’ll call using Kasia’s strategy to get stuff done), generating random colours, and putting text on a single-colour background. Let’s assume Leslie Knope would have written a poem on the other side, which you could do using Katie Jolly’s approach. Anyway, how did I generate random colours? Well, using charlatan! Really, a quite useful package by Scott Chamberlain, peer-reviewed by Brooke Anderson and TJ Mahr at rOpenSci onboarding repo.

set.seed(123)
compliments <- dplyr::mutate(compliments, 
                             background = charlatan::ch_hex_color(n = 4), 
                             text_colour = charlatan::ch_hex_color(n = 4))

And then I could use magick! I’ve convinced myself that although I use magick for non serious stuff, I could use these skills on scientific image data one day. Note that I could have packed the whole code of the post in a single function, but I wanted to progressively build the card (compliments, then colours, then the image).

library("magrittr")

create_card <- function(who, compliments){
  compliments <- dplyr::filter(compliments, name == who)
  magick::image_blank(600, 400,
                      color = compliments$background) %>%
    magick::image_annotate(text = compliments$compliment,
                           color = compliments$text_colour,
                           location = "+50+100",
                           boxcolor = "white",
                           size = 20) %>%
    magick::image_annotate(text = "Love, Leslie",
                           color = compliments$text_colour,
                           location = "+200+300",
                           boxcolor = "white",
                           size = 20)%>%
    magick::image_annotate(text = "HAPPY GALENTINE'S DAY!",
                           boxcolor = compliments$text_colour,
                           location = "+100+200",
                           color = "white",
                           size = 20) %>%
    magick::image_write(paste0("data/galentines", who, ".png"))
}

purrr::walk(compliments$name, create_card, compliments)

Yes, I signed the cards “Leslie”. I know the text could be centered more but I don’t really care.

Display the cards

In real life, you’d print and send these cards to your friends, but since my imaginary friends Ann, April, Donna and Shauna probably read my blog, I’ll let the cards here for them to see!

Not too shabby for random text and colours!

To leave a comment for the author, please follow the link and comment on their blog: Maëlle's R blog on Maëlle Salmon's personal website.

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.