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 this chapter of IDS. If things don’t work, ask for help on the discussion boards.
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
This installs a package that gives you access to various infectious disease simulation models. We won’t do anything with that package in this course, we just install it for practice. If you want to learn more about DSAIDE, take a look at the package website.
If this is the first time you are installing packages, you’ll see that a lot of other packages are installed, too. You might get a message about Installing from source packages that need compilation. I generally recommend to say No to this, though usually both approaches work.
To see which packages are needed by a specific package, e.g.
DSAIDE, and thus are being installed if not present, type
tools::package_dependencies("DSAIDE") into the R console. Of course it can be that those packages depend on other packages, so you end up installing even more. At some point, you’ll have the most common packages all installed and installing new packages will lead to less overall installing. The package install process generally works well.
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. 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.
If you are new to R and RStudio and want to learn a bit more, I suggest you skim through this chapter of IDS.
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.