Introducing ggirl
An R package that lets you make ggplots in real life
I’m very pleased to introduce ggirl, a new R package I made that lets you make your ggplots in real life. There are so many beautiful ggplots in the world, and I want more of these to exist outside of a computer screen! Version 1.0.0 of the package will let you take a ggplot2 object and have it sent on a postcard to your friends, colleagues, and family! With one ggpostcard()
function call (and $2.50) a real physical postcard will be in the mail.
ORIGINAL GGPLOT2 PLOT | A REAL LIFE POSTCARD |
---|---|
How it works
You can send a postcard with just a few lines of code! First, install the package with:
# install.packages("remotes") # if you don't already have it
remotes::install_github("jnolis/ggirl")
Then create a plot you like, which in this case is a pretty boring example:
library(ggplot2)
plot <- ggplot(data.frame(x=1:10, y=runif(10)),aes(x=x,y=y)) +
geom_line() +
geom_point()
Now let’s ship it! Specify your email address (for order updates), address to mail to, and the message to put on the back:
library(ggirl)
contact_email <- "fakeemailforreal@gmail.com"
send_address_1 <- address(name = "Fake Personname",
address_line_1 = "250 North Ave",
city = "Boston",
state = "MA",
postal_code = "22222",
country = "US")
message_1 <- "This plot made me think of you!"
ggpostcard(plot,
contact_email,
messages = message_1,
send_addresses = send_address_1)
(You can include multiple recipients too and customize the return address–check out the function help for more details.)
This will pop up a web page showing you what the order will look like. If both the front picture and back info look good to you click the button to make the payment and submit the order.
This will bring you to Stripe to finish the purchase.
Once you pay you’re done! Postcards will arrive in rougly 1-2 weeks.
And if you can’t think of a cool plot to use, don’t worry! The package includes three example plots you can try each with a single function call:
{ContouR} by Ijeamaka Anyene
{rstereogram} by Ryan Timpe
Sunrise plot by me
Version 1.0.0 is for postcards (and United States only to start). International postcards will be added in the coming weeks, and others mediums besides postcards will be added too!
The backend
The ggirl R package itself is primarily an interface with a separate backend server which does the previews, requests the payment, and submits the fulfillment request. What language was the the backend written in that could handle such a complex production system? R of course! The entire backend for ggirl is running on a single Shiny app! That app is hosted on RStudio shinyapps.io.
The ggirl backend relies on a number of different methods to be able to upload a plot, make into a postcard, take a payment, and fulfill the order. Specifically:
- It uses the R package {brochure} by Colin Fay to have multiple endpoints in a single Shiny app (and handle the upload requests).
- The data is stored on Google Cloud using the {GoogleCloudStorageR} package.
- The backend uses {httr} to call the Stripe API to request the payment. Once the payment is received a webhook starts a Shiny background job to call the postcard sending API.
- An order confirmation email is sent with the RStudio {blastula} package.
The full flow is shown below. The user calls the function which uploads the plot via a POST request, then the user is directed to the Shiny app. When the user views the preview then presses “pay and submit” they are directed to a Stripe session to pay. Upon paying, Stripe uses a webhook to tell Shiny to fulfill the order. Shiny then uses a background process to call the postcard sending API and send the confirmation email. All of the components of the Shiny app are orchestrated with {brochure}.
While the backend code isn’t yet public, I will be giving a talk at Cascadia R Conf 2021 about it and the work that went into making this package possible. “eCommerce platform written entirely in R” may feel like borderline stunt engineering, I found it ended up being easier than I would have guessed and I think that other people could benefit from learning some of these techniques I used!
Check out some of the postcards made during the ggirl beta! If you do end up using the {ggirl} package I’d love to see it! You can use the hashtag #ggirlart to share your art and see what other people have made too.
Thanks to:
- @nolistic and @robinsones for helping design the product.
- The beta testers: @cxinya, @delabj, @thisisnic, @TheCoachEdwards, @ryantimpe, @robinsones, @mcsiple, and @cathblatter.
- @ColinFay for the package {brochure} which powers the Shiny server doing the back-end work.
- @Ijeamakaanyene for the use of the contouR example.
- @ryantimpe for the use of the rstereogram example.
- sunrise-sunset.org for the API powering the sunrise example.
- The R Foundation for the R logo in the ggpostcard_example_rstereogram().