Using R

I use R a lot. This is my collection of how-tos, tricks, and favorite bits.
R
project management
website
Author
Affiliation
Published

Wednesday, March 24, 2021

I forget how to do a lot in R when I only do it once every year or two. I also wanted a place to keep track of my favorite packages. So:

Updating R with installr

This is probably the simplest one on the list. Per the installr vignette:

if(!require("installr")) install.packages('installr')
library("installr")
updateR() # this will open dialog boxes to take you through the steps.
# OR use:
# updateR(TRUE) # this will use common defaults and will be the safest/fastest option

You should run it in the Rgui, not in RStudio.

In my experience, after using installr to update, the packages have never come over to the new library. I normally have to copy and paste everything from one library to the next. Running update.packages(ask = FALSE, checkBuilt = TRUE) tidies everything up afterwards.

And that’s it! 🥳

formatR

A great way to keep your code looking tidy. Remember: space is your friend! The example Yihui Xie uses on his website is this poorly organized code:

## comments are retained;
# a comment block will be reflowed if it contains long comments;
#' roxygen comments will not be wrapped in any case
1+1

if(TRUE){
x=1  # inline comments
}else{
x=2;print('Oh no... ask the right bracket to go away!')}
1*3 # one space before this comment will become two!
2+2+2    # only 'single quotes' are allowed in comments

lm(y~x1+x2, data=data.frame(y=rnorm(100),x1=rnorm(100),x2=rnorm(100)))  ### a linear model
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  # comment after a long line
## here is a long long long long long long long long long long long long long comment that may be wrapped

And then, after using formatR:

## comments are retained; a comment block will be
## reflowed if it contains long comments;
#' roxygen comments will not be wrapped in any case
1 + 1

if (TRUE) {
    x = 1  # inline comments
} else {
    x = 2
    print("Oh no... ask the right bracket to go away!")
}
1 * 3  # one space before this comment will become two!
2 + 2 + 2  # only 'single quotes' are allowed in comments

lm(y ~ x1 + x2, data = data.frame(y = rnorm(100), x1 = rnorm(100), 
    x2 = rnorm(100)))  ### a linear model
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 
    1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1  # comment after a long line
## here is a long long long long long long long long
## long long long long long comment that may be
## wrapped

Knitr

There’s so much that knitr can do and I find myself having to over and over research just how to do something. No more!

Including an image:

knitr::include_graphics("image.ext")

or

knitr::include_graphics("url")

The various R code chunk options are handy to know, as well. This is also related to the verbatim code bit below, as it’s a knitr option.

Installing and loading packages

I give Rmd files to my students to use and practice with. This makes sure they don’t run into issues with not having packages installed:

for (package in c('package1', 'package2', 'package3')) {
    if (!require(package, character.only=T, quietly=T)) {
        install.packages(package, repos="http://cran.us.r-project.org")
        library(package, character.only=T)
    }
}

Including icons and animation

To use FontAwesome:

fontawesome::fa("arrow-down", fill = "Sienna"):

To use emo::ji:

emo::ji("sweat_smile"): 😅

To use anicon:

anicon::nia("Some text!", animate="wrench")

Warning

For some reason, inline icon code prevented Quarto from rendering a document as of June 21, 2022, but seems to be fixed in v0.9.611, so update Quarto if this is throwing errors. Note that anicon is still throwing an error but fontawesome and emo work fine.

Best to use the icon package for static icons.

Project management: ProjectTemplate

Project management has always been my downfall in terms of finding the solution that works just the way I want it to. (And, yes, I know that perfect is the enemy of good but I have to procrastinate somehow, don’t I?) I really like ProjectTemplate, though. I typically use the minimal version of the template and it helps keep me organized and on a standard workflow. It’s easy to put down and pick back up later, as well.

Note

I also have a couple project template repositories that I’ll highlight at some point. ProjectTemplate is still something I use, regardless.

Citation, papaja, and BetterBibTeX

I use Zotero and, since I do a lot of writing in APA format, I use the papaja package to make that easier. As such, I use BibTeX (unsurprisingly).

Quick copy from Zotero

Took me a while to learn this but as my Zotero library has thousands of entries, I don’t always use the full library as a .bib file (for obvious reasons). As I’m also constantly adding new entries and I don’t want to have to export a .bib every time, the Quick Copy option is as I can easily throw a new BibTeX entry into a .bib file in RStudio.

You can change the Quick Copy formatting by going to Edit -> Preferences -> Export and changing the Default Format to BetterBibTeX. Voila, you can start using the citation key.

Different in-text citation forms

While I love doing everything like this in plain text, I can never remember how the different in-text citations work. So here’s a cheatsheet. We’ll be using (Ash et al., 2017) as the example.

My YAML includes: bibliography: ["example.bib"]

Depending on what RMarkdown template/document you’re using, you can also change the bibliography style.

I have this BibTeX entry in the .bib file:

@article{Ash2017,
  title = {Unit, Vibration, Tone: A Post-Phenomenological Method for Researching Digital Interfaces},
  author = {Ash, James and Anderson, Ben and Gordon, Rachel and Langley, Paul},
  year = {2017},
  pages = {147447401772655},
  issn = {1474-4740},
  doi = {10.1177/1474474017726556},
  abstract = {Digital interfaces, in the form of websites, mobile apps and other platforms, now mediate user experiences with a variety of economic, cultural and political services and products. To study these digital mediations, researchers have to date followed a range of methodological strategies including the modification of pre-existing qualitative research methods, such as content analysis, discourse analysis and semiotics, among many others, and an experimentation with new methods designed to make visible the operation of data aggregation, analytics and algorithms that are hidden from users. Building upon, while distinct from these strategies, the article sets out a post-phenomenological approach to studying interfaces, websites and apps that explicitly interrogates how they appear as objects. In doing so, the article provides a response to a problem that animates contemporary cultural geography: that new cultural objects are emerging which place in question the habits and practices of analysis that composed the `new' cultural geography. To do this, the paper develops the concepts of unit, vibration and tone to unpack interfaces as sets of entities that work together to shape the experiences and responses of users. As such, the article provides a methodological vocabulary for the analysis of how interfaces operate to modulate user response and action on a series of habitual and un-reflected upon levels and thereby to create outcomes that suit their owners and operators.},
  journal = {Cultural Geographies},
  keywords = {digital geographies,digital methods,interfaces,materiality,post-phenomenology}
}

Obviously, the abstract is optional but I’m leaving here for demonstration purposes. For the different formats of in-text citation:

[@Ash2017] gives you what you saw above: (Ash et al., 2017)
[@Ash2017, pp. 22] gives you: (Ash et al., 2017, p. 22)
[-@Ash2017] removes the author: (2017)
@Ash2017 (without the brackets) gives you: Ash et al. (2017)
@Ash2017 [pp. 22] gives you: Ash et al. (2017, p. 22).

If you wanted to include a citation in the bibliography but not actually cite it in the text, just add this to the YAML:

nocite: |
  @Ash2017

Finally, just place a ## References header at the bottom of your file and voila, instant bibliography! In Quarto, this will be done automatically. No need to add that section.

papaja

For doing more nuanced work, I use the papaja package to create APA-formatted papers. I highly encourage folks–especially students that produce APA-formatted documents frequently–to check it out. It’s so handy.

All things Xaringan: Presentation Ninja

Important

Since Quarto’s release, I’m now using Quarto Presentations before choosing to begin a new slide deck with xaringan. Will put together a post about my favorite features at some point. Still, if you’re still using xaringan, the following is still very cool.

I use the xaringan package a lot. I gave up Powerpoint years ago in favor of something that’s lightweight, can include native R calculations and code, and is mobile, accessible, and responsive.

There’s a whole section of R Markdown: The Definitive Guide devoted to xaringan but here are thing little bits that I really love:

Progress bar

Add the slideNumberFormat lines to your YAML.

output:
  xaringan::moon_reader:
    nature:
      slideNumberFormat: |
       <div class="progress-bar-container">
         <div class="progress-bar" style="width: calc(%current% / %total% * 100%);">
         </div>
       </div>

Then, add this to your custom.css (or whatever custom CSS file you’re using) and alter to your liking:

.remark-slide-number {
  position: inherit;
}

.remark-slide-number .progress-bar-container {
  position: absolute;
  bottom: 0;
  height: 4px;
  display: block;
  left: 0;
  right: 0;
}

.remark-slide-number .progress-bar {
  height: 100%;
  background-color: red;
}

This gives you a thin red progress bar at the bottom of each slide. Much better than numbered slides!

Start with a Please Stand By slide

Use this directly after your R setup code chunk:

background-image: url(https://media.giphy.com/media/CdhxVrdRN4YFi/giphy.gif)
background-position: center
background-color: #000
background-size: contain

This sets your very first slide full screen as:

xaringanExtra

I recently wrote about using the scribble() function from xaringanExtra, which is orders of magnitude easier to use when annotating a slide deck and sharing via Zoom. Other functions from the package I really like:

# xaringanExtra options

# Includes a "subtle tone" when slides change. Good for accessibility
xaringanExtra::use_slide_tone() # turns on the audio tone when slides change

# Provides more obvious code highlighting. Great for teaching.
xaringanExtra::use_extra_styles(  # higlights code
  hover_code_line = TRUE,         #<<
  mute_unhighlighted_code = TRUE  #<<
)

# Turns on clipboard options. Great for giving code to students.
htmltools::tagList(
  xaringanExtra::use_clipboard(
    button_text = "<i class=\"fa fa-clipboard\"></i>",
    success_text = "<i class=\"fa fa-check\" style=\"color: #90BE6D\"></i>",
    error_text = "<i class=\"fa fa-times-circle\" style=\"color: #F94144\"></i>"
  ),
  rmarkdown::html_dependency_font_awesome()
)

# The sainted scribble function I keep talking about.
xaringanExtra::use_scribble()     # turns on scribble, built-in annotation

Supplemental

This stuff isn’t R-related, specifically, but makes working with R a lot easier. I’ll probably add to this as I think of things.

Copycat

A Chrome extension that allows for quick copying of URLs, text, et cetera, as markdown syntax. When creating a list of links or resources, for example, this is very, very handy. It speeds things up considerably to just be able to copy and paste without thinking about the markdown syntax.

Proper file naming

Follow the advice of Jenny Bryan and use filenames that are:

  1. machine readable
  2. human readable
  3. play well with default ordering

Her classic slide deck on the topic is a wealth of guidance. In short:

Good:
2021-03-13_blogpost_how-to-r.Rmd
2021-03-21_blogpost_stream-deck-for-online-teaching.Rmd
2021-04-04_blogpost_something-i-may-write-who-knows.Rmd

Chronological order template:

YYYY-MM-DD_Project-title_Specific-file_Version.ext

Logical order:

01_Filename-title-that-is-descriptive.ext
02_Filename-title-that-is-descriptive.ext
90_Filename-title-that-is-descriptive.ext

Function/value/variable naming:

variable.name not variable_name

Bad:
draft How to R.Rmd
New File (1)(1)(1).PDF
image.jpg
RS-here are my edits to yourfile.docx

Help Today-You be kind to Tomorrow-You by using proper naming!

Embedding tweets

Using the tweetrmd package (requires the webshot2 package to create the screenshot embeds). The include_tweet function will add a pure HTML copy of the tweet in HTML output or a screenshot in others.

tweetrmd::include_tweet("https://twitter.com/dsquintana/status/1275705042385940480")

References

Ash, J., Anderson, B., Gordon, R., & Langley, P. (2017). Unit, vibration, tone: A post-phenomenological method for researching digital interfaces. Cultural Geographies, 147447401772655. https://doi.org/10.1177/1474474017726556

Reuse

Citation

BibTeX citation:
@misc{straight2021,
  author = {Straight, Ryan},
  title = {Using {R}},
  date = {2021-03-24},
  url = {https://ryanstraight.com/posts/how-to-r},
  langid = {en}
}
For attribution, please cite this work as:
Straight, R. (2021, March 24). Using R. https://ryanstraight.com/posts/how-to-r