Site icon R-bloggers

Use shortcodes to embed tweets, videos, etc. in your blogdown blog posts

[This article was first published on reigo.eu, 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.

Shortcodes are simple snippets inside your content files calling built-in or custom templates. This short post is a shout-out for the blogdown::shortcode() function. Example(s) of blogdown::shortcode() can be found in blogdown book. To learn more about shortcodes visit https://gohugo.io/content-management/shortcodes/.

Shortcodes make it super easy to embed content in blog posts. Lets say we have a video we want to embed in a post: https://youtu.be/CjTLN-FXiFA. By adding

```{r, eval=TRUE}
blogdown::shortcode("youtube", "CjTLN-FXiFA")
```

to our .Rmd file, we get:

How I discovered shortcodes

For some time I have had the routine of (scrolling twitter and) sending tweets holding information I see myself using to my e-mail. For example, I find @rensa_co’s example of dplyr::case_when() and geom_text() to be quite useful:

? dplyr::case_when goes really well with geom_text (or ggrepel) for when you want to programmatically label a few of the most interesting features in your #rstats plots! And the criteria can be as complex as you want ? pic.twitter.com/0jUp1uarde

— James Goldie (@rensa_co) March 21, 2018

So I sent it to my (work or personal – I’m not sure which) e-mail with hopes to easily find it in the future. One can experience the pain of the e-mail system after first time really looking for something. One can quickly understand that there must exist a better way. As I was looking for a way to archive all these pearls, I was thinking about devoting a blog post to those tweets. I started by looking for a way to embed twitter cards to blog post written with blogdown. I googeled “how to embed tweet in rmarkdown” which not surprisingly lead me to stackoverflow. This question was answered by Yihui and pointed to the blogdown documentation where I learned about shortcode() function.

FIN

After giving the blog post approach a second thought I realized that this is probably not gonna work. It seemed to be too much hassle to update the post manually or figure out how to automate the process. I ended up creating a #rhtwotes (rh – my initials; twote1 – twitter note/quote) and the plan is to retweet things I see myself using with #rhtwotes hashtag. This gives me easy access to my twotes whenever I have internet connections and also simplifies (retweeting is easier than sending an e-mail) note taking part.

For now, using the hashtag system feels quite natural and optimal way to cover my use case. I also ask myself: “How could I use something not as simple as retweeting system before?”. As it often happens, obvious solutions for our problems become obvious only after we have tried many not so obvious ones.

#rhtwotes

< !-- One extra thing I learned writing this post is how to add verbatim code chunks to posts written in rmarkown. Learn more from Yihui's [blog](https://yihui.name/en/2017/11/knitr-verbatim-code-chunk/). -->

  1. I first had tnotes for twitter notes in mind. Fortunately ML proposed twote which has much better sounding. Word twote seems to have wider usage as well: https://www.urbandictionary.com/define.php?term=Twote

To leave a comment for the author, please follow the link and comment on their blog: reigo.eu.

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.