Tip: Use R to create an email nudge

code
r
beginner
tips
Author

Daniel Kick

Published

April 16, 2024

Suppose you’re giving a presentation and you want to make it easy for people to contact you afterwards. Maybe you have your email in the acknowledgements or maybe you make business cards with a qr code to your website.

These are good steps but we can go further. Let’s make qr code that nudges people to send an email. I’ve used this to good effect for getting emails of people who would like to be notified when a software project goes live.

Here’s the plan: 1. Give people your email. 2. Make it easy for them to send you an email. 3. Encourage them to do it now.

To accomplish this we’re going to create a mailto link and encode it as a qr code. mailtos are opened in your default mail application so this gets the address where it’ll be used with zero typing. We’ll add some suggested text to the email. This gives the user a starting point and gives us a default subject line to search for later.

Here’s what this looks like in R. After setting the email and default subject and body text the spaces are replaced with %20 (20 is the ASCII hexdecimal code for space). We concatenate these strings together and then use the marvelous qrcode library to make a graphic that’s ready for a poster, presentation, or card.

library(qrcode)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.0     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
email    = 'your.name@usda.gov'
inp_subj = 'Re: Envriomental Deep Learning Presentation'
inp_body = 'Hello from the conference...'


inp_subj = str_replace_all(inp_subj, ' ', '%20')
inp_body = str_replace_all(inp_body, ' ', '%20')

inp_text = paste0(c(
  '<a href="mailto:',email,'?&subject=', inp_subj, '&body=', inp_body, '> </a>'
), collapse = '')

plot(qrcode::qr_code(inp_text))