Note: Unfortunately, the latest version of the DSAIDE package didn’t make it to CRAN in time (they are on vacation). Thus you need to install it from GitHub and also need a recent version of R for things to work. Here’s a quick overview of what you need.

  • Install R version 4.0 or later.
  • Install a recent version of RStudio.
  • Install the remotes R package. Use the remotes package command install_github('ahgroup/DSAIDE') to install the latest version of DSAIDE (see more details in the DSAIDE Get Started tutorial). Make sure you have version 0.8.6 or newer (I’ll be releasing new versions as the course goes on).

If you are new to R, please go through all the documents of this module, as well as those linked from there in the indicated order. Otherwise you will likely miss things.

Overview

This is a very quick introduction to R and RStudio, to get you set up and running. We won’t do any coding in this class, but we need R to use the DSAIDE package. R is a very useful tool to be familiar with if you want to get into ID modeling, or if you want to do data analysis. As such, having a bit of familiarity is good.

Learning Objectives

  • Know some of the strengths and weaknesses of R.
  • Have R and Rstudio up and running on your computer.
  • Know how to install and load R packages.

About R

Like every programming language, R has its advantages and disadvantages. Feel free to do a web search on that topic, and you will encounter tons of people with tons of opinions. Some of the features that are useful to us are:

  • R is Open Source, FREE and cross-platform.
  • R is a “high-level” programming language, relatively easy to learn (compared to Fortran, C, etc.).
  • R comes with many integrated functions.
  • R is great for statistics, data analysis, and graphics.
  • R is a full programming language and can do a lot of things, including running infectious disease models.
  • The R Community is very dynamic, helpful and welcoming.
  • Through R packages, it is easy to get lots of state-of-the-art algorithms.
  • Documentation and help files for R are generally good.

While we use R in this course, it is not the only option for building and exploring infectious disease models. Maybe the most similar to R, and widely used, is Python, which is also free. There is also commercial software that can be used (e.g., Matlab, Mathematica). Other more general programming languages are suitable too (e.g., C, Fortran, Java, Julia). Depending on your future needs or jobs, you might have to learn one or several of those languages. In general, knowing some coding is a very useful skill, and I strongly encourage you to learn some programming. The good news is that while all programming languages are somewhat different, they all share general ways of thinking and structuring code. So once you understand a specific concept (e.g., variables, loops, branching statements or functions), it applies to all those languages. Thus, learning a new programming language is much easier once you already know one. R is a good one to get started with.

Installing R and RStudio

If you haven’t already, install R first. You can pick any mirror you like. If you already have R installed, make sure it is a fairly recent version, otherwise upgrade to a current version.

For this course, you should have 4.0 or newer for things to work.

Once you have R installed, install the free version of RStudio Desktop. Again, make sure it’s a recent version.

Installing R and RStudio should be fairly straightforward. If you want detailed instructions this reading provides more detailed steps. If things don’t work, ask for help in the R/DSAIDE channel.

I personally only have experience with Windows, but everything should work on all the standard operating systems (Windows, Mac, and even Linux).

Installing and loading R packages

Most of the functionality and features in R come in the form of add-on packages. There are tens of thousands of packages available, some big, some small, some well documented, some not. We’ll be focusing on the DSAIDE package in this course. Of course, you are free to install and use any package you come across for any of the assignments.

The “official” place for packages is the CRAN website. If you are interested in packages on a specific topic, the CRAN task views provide curated descriptions of packages sorted by topic.

To install a package from CRAN, go to the R prompt at the bottom left of your RStudio session and type install.packages("PACKAGENAME"). The figure shows an example where I installed a package called learnr. Often, a package needs other packages to work (called dependencies), and they are installed automatically. Sometimes this can be a lot of packages. The good thing is that you only have to do that once, unless you change your computer or reinstall/update R. It usually doesn’t matter if you use a single or double quotation mark around the name of the package. Note that R cares about capitalization, so you need to get the upper and lower case exactly right. Otherwise, it won’t work.

Installing an R package. Right-click on the image to enlarge it.

Installing an R package. Right-click on the image to enlarge it.

Try installing a package yourself. Open RStudio. Then go to the R prompt (the > symbol) in the lower-left corner and type

install.packages('deSolve')

This will install the deSolve package.

In RStudio, you can also install (and update/remove) packages by clicking on the ‘Packages’ tab in the bottom right window.

It is very common these days for packages to be developed on GitHub. It is possible to install packages from Github directly. Those usually contain the latest version of the package, with features that might not be available yet on the CRAN website. Sometimes, in early development stages, a package is only on Github until the developer(s) feel it’s good enough for CRAN submission. So installing from Github gives you the latest. The downside is that packages under development can often be buggy and not working right. To install packages from Github, you need to install the remotes package and then use the install_github function.

You only need to install a package once, unless you upgrade/re-install R. Once installed, you still need to load the package before you can use it. That has to happen every time you start a new R session. You do that using the library() command (an alternative is require() but I recommend library()). For instance to load the deSolve package, type

library('deSolve')

You should see a short message on the screen. Some packages show messages when you load them, and others don’t. In this case, there is no message. No error or warning message is a good sign. You rarely have to see if a package is loaded, you generally find out by getting an error message when you want to use a function from that package. But if you want to see what’s loaded, you can type this command into the R console

(.packages())

Learning R

As mentioned, we’ll be using R in this class to run the software which contains the ID models. There is no requirement for writing code in this class. Of course, if you want to try some R coding as part of the class, you are welcome to do so. I’ll often give pointers explaining how you could do that. If you want to learn more R there are lots of resources available online. The tricky bit is finding some that you like. This list of resources might be useful. In general, the RStudio folks often produce very high quality content, and there are many good online courses as well. Search around a bit until you find something that fits your level and style of learning. The good thing is that pretty much all those resources are free.

Learning R Studio

While one can use R and do pretty much every task, including all the ones we cover in this class, without using RStudio, RStudio is very useful, has lots of features that make your R coding life easier and has become pretty much the default integrated development environment (IDE) for R. Since RStudio has lots of features, it takes time to learn them. You don’t really need to know much about RStudio for this course. But if you are interested, a good resource to learn more about RStudio are the R Studio Essentials collection of videos.