Class 23

Iteration and APIs

Materials for class on

2024-11-14

Agenda

Today we’ll focus on:

  • Updating patch versions of R
  • More on working with APIs
  • Some examples of Iteration
Poll

What’s one thing you still want to learn more about, or improve in, in this class?

Patch (minor minor) version updates

There’s a new “patch” R release since the semester started, so let’s start there!

Do you need to update? Let’s look at the R release NEWS.

If none of these changes make sense/seem important to you, and it’s a change only in the third number of the version, you can just ignore. You can check your current version with R.Version(). If you installed at the beginning of the semester, your major version will be 4 and your minor version is 4.1, so version 4.4.1. The newest version is only a change to 4.4.2. Updates in the third digit are very easy, you can install the new version like a regular program. Changes in the second digit, and especially the first, require more steps like reinstalling your packages.

If you don’t update, you may see a warning when installing some new packages telling you that they were built under the newer R version, like this:

Warning: package 'repurrrsive' was built under R version 4.2.3

This is usually not a problem if the version numbers are close. You can ignore it unless you are having some unexpected issues with the package - then you might look into whether there is something causing a conflict in versions.

More on APIs

First we’ll work with the Reddit API, which doesn’t require a key. Then we’ll come back to the issue of working with keys and authenticating, which by now you’ve probably run into some annoyances with.

Saving your tokens

You should never save your keys or passwords in an R script, and you should especially not push them to GitHub. Instead, if you want to store one, you can keep it in a file outside of your project on your computer locally. A convenient way to do this for R is to save it in your .Renviron file. This is a file that you can use to customize your R startup environment, and is located somewhere in a ‘home’ directory on your computer. You can find a description of the various R/RStudio startup files from Posit here. The package usethis provides a utility to find, open, and then edit your .Renviron file:

#install.packages(usethis)
usethis::edit_r_environ()

This should open the file, and then you can add a line with a key and value, such as:

# this is not really my Canvas API key
CANVAS_KEY = "a93jfk0384jgk392d"

Once that is done, you need to restart R for it to take effect. Then you can always “grab” your key like so, without showing the actual value in the script:

Sys.getenv("CANVAS_KEY")

Some API wrapper packages will provide other utilities with different ways to locally store an encrypted version of your key or authentication details. If you get into more depth with custom API work, you can read more about authentication using the httr2 package in this vignette about OAuth.

Other API Wrappers to Try

Here are some other R packages that provide API wrappers that you might want to try. Read the documentation carefully to determine what functions are available and how to use them.

Iteration

Iteration is a programming tool that taps on your shoulder when you feel annoyed typing the same or similar thing out many times, and reminds you that it doesn’t have to be that way!

Allison Horst illustration of dplyr::across()

There are many ways to iterate in programming, and as a functional language, R has some special ones that do not use the more common “loop” syntax. It takes a while to become fluent in all of these, but they are helpful to know about as a reference tool. When you get that shoulder tap, you know where to look. You need to decide each time whether you want to do it “by hand” or use your iteration tools - it’s often not obvious which will be faster for that specific analysis. Do remember to consider whether you will be doing something similar again, and that it may be worth investing some time learning the iteration so that you can use it again next time!

One of the key things to note about iteration is that there are special considerations when you are working with data, in that you often want to “grow” a vector or dataframe as a result of your iteration. This is very slow if not done properly. If you use functional programming tools and vectorization, you don’t have to worry about it. If you use loops, you need to create the “container” for your data and then fill it in, rather than simply appending a row/element on each iteration. This is alluded to in the base R chapter of R4DS2.

Since you didn’t do a prep assignment and exercises on iteration, we’re going to look at some of the in-text examples from the Iteration chapter together.

If you want to try the file manipulation examples, you can download the gapminder folder from our drive folder.