library("googlesheets")
library("dplyr")
Secret Sampling
’Tis the season for white elephant / Yankee swap / secret santa-ing! There are various rules for this, for our version:
🏷 each participant receives the name of someone else to purchase a gift for
🎁 gifts are exchanged at a party
🤔 the receiver is tasked with guessing who the gift-giver was!
We thought it’d be particularly fun to do it #rstats style.
Assigning partners
We had our interested classmates sign up on a Google Form, resulting in a Google Sheet with each participant’s name
, email
, and interests
.
<- gs_url("https://docs.google.com/spreadsheets/d/1NHbsjZVhvIxzuivXWQvDBst0oMN7ZcCjCbI8XJeXxJI/edit#gid=860103774") %>%
df gs_read()
We began by making a tibble that has name
in the first column and assigned a random partner
in the second.
set.seed(525)
<- tibble(
dat name = df$name,
partner = sample(df$name)
)
“But what if someone was assigned to be their own partner!” you may ask. Have no fear! A while
loop is here! We allow a while
loop to iterate until every individual is assigned a partner (who isn’t them!).
while (any(dat$name == dat$partner)) {
<- tibble(
dat name = df$name,
partner = sample(df$name)
) }
This is likely not the most efficient way, but we only had 23 friends so it’s 👌 – if you get excited about efficiency and would like to share a quicker way to do it, please do! We ❤️ to learn!
Edit: Looks like our 🙏 was answered!
What if you just did sample(names) and everyone was assigned to give a give to the next person in the vector?
— Hadley Wickham (@hadleywickham) November 15, 2017
This could look something like:
<- df %>%
dat select(name) %>%
mutate(name = sample(name)) %>%
mutate(partner = lag(name))
$partner[1] <- dat$name[nrow(dat)] dat
Or like this! (Thanks to our #livefreeordichtomize partner in crime + emoji friend Romain 👯)
Maybe tail(.,-1)
— Romain François (@romain_francois) November 15, 2017
We then do a join to pull the emails and interests back in.
<- df[ , c("name", "email")]
df_name <- df[ , c("name", "interests")]
df_partner <- dat %>%
dat left_join(df_name, c("name" = "name")) %>%
left_join(df_partner, c("partner" = "name"))
Sending the email
Karthik and I had a blast at the rOpenSci unconf creating the 🐴 ponyexpress package - what a delightful chance to use it! We didn’t want the secret to be spoiled (i.e. we didn’t want to know who would be buying us gifts!), so we wrote a script to send an email to our classmates.
## devtools::install_github("ropenscilabs/ponyexpress")
library("ponyexpress")
<- "Dear {name},
body
You have been assigned to surprise <b>{partner}</b> with a happy gift! {partner} told us they have the following interests:
<b>{interests}</b>
Remember, the price limit is $20.
<img src = 'https://media.giphy.com/media/zhPXoVIBMtnUs/giphy.gif'> </img>
Happy white elephant-ing,
Sarah & Lucy"
<- glue::glue(glitter_template)
our_template
<- parcel_create(dat,
parcel sender_name = "Lucy",
sender_email = "lucy@myemail.com",
subject = "Secret Santa!",
template = our_template)
parcel_preview(parcel)
parcel_send(parcel)
This will create an email like this:
And that’s it! Happy gift-ing 🎁!