This is a very quick introduction to R and RStudio, to get you set up and running. We’ll go deeper into R and coding later.
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:
While we use R in this course, it is not the only option to analyze data. Maybe the most similar to R, and widely used, is Python, which is also free. There is also commercial software that can be used to analyze data (e.g., Matlab, Mathematica, Tableau, SAS, SPSS). Other more general programming languages are suitable for certain types of analyses as well (e.g., C, Fortran, Perl, Java, Julia). Depending on your future needs or jobs, you might have to learn one or several of those additional languages. The good news is that even though those languages are all 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. And R is a good one to get started with.
Installing R and RStudio should be fairly straightforward. If you want detailed instructions go through chapter 1 of IDS. If things don’t work, ask for help in the R/Coding discussion board.
I personally only have experience with Windows, but everything should work on all the standard operating systems (Windows, Mac, and even Linux).
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 using many different packages in this course. Of course, you are free to install and use any package you come across for any of the assignments.
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. 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.
Try installing a package yourself. Open RStudio. Then go to the R prompt (the
> symbol) in the lower-left corner and type
Question: As you installed the DSAIDE package, which of these packages was also installed: GillespieSSA, adaptivetau, gpDDE, irace? Record the answer. It will likely show up in the quiz. It is unlikely, but it could be that you already have all packages required by DSAIDE installed. In that case, you won’t see any other packages installed. To see which of the packages above DSAIDE needs (and thus installs if it is not present), type
tools::package_dependencies("DSAIDE") into the R console.
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. We won’t do that now, but it’s quite likely that at one point later in this course we will.
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
DSAIDE package, type
You should see a short message on the screen. Some packages show messages when you load them, and others don’t. In this case, the package tells you that you can start using it by typing
dsaidemenu() into the R console. DSAIDE is a package I wrote that allows you to explore infectious disease models. We won’t use it in this class. I’m just using it as an example here since you can use the package without having to write code. Try it briefly, by typing the code below into the R console
A menu should open in your browser, from which you can explore different models/apps.
Question: In the main menu of the DSAIDE package, what’s the title of the second group of apps?
Once you are done with DSAIDE, close it.
This was a quick overview of R packages. We’ll use a lot of them, so you’ll get used to them rather quickly.
Next, go through chapter 2 of IDS for some further information on setting up and getting started with R, RStudio, and R packages. This chapter gives some tips and ideas that might be of interest even to those of you who already have R/RStudio experience. (Hint: I will ask about this chapter on the quiz).
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. A good resource to learn more about RStudio are the R Studio Essentials collection of videos.