Chapter 3 The software
When buying commercial, GUI-based software, we pay mostly for a silky smooth install experience. Open source software like R is free. Installs of free software are not always silky smooth. There is no money to be lost if your installation goes wrong.
3.1 YOU DO NOT NEED TO INSTALL ANYTHING for IBS538/BIOS505
You will be invited to a cloud-based version of the course software when the semester starts. You will be able to do everything you need to do in this course using a browser and without installing any software.
The installation instructions are for anybody who wishes to stand up a local version on their personal machine.
3.2 tl;dr if you wish to install
First, download and install the latest version of R ‘precompiled binary base distribution’ for your operating system from here.
Next, download and install the latest version of RStudio Desktop Free, from here.
In that order.
If you are compiling, you’re doing it wrong. Never compile.
3.3 Spring 2022 term versions
R: 4.1.2 “Bird Hippie”
RStudio Desktop: 2021.09.1+372
3.3.1 What are these?
R is the computational engine.
RStudio is an integrated development environment (IDE) that makes using R very easy on your machine.
3.4 Follow these rules
3.4.1 Rule 1: Install R first, install RStudio next
This will make it easier for RStudio to automagically “find” your R installation.
3.4.2 Rule 2: Don’t pay!
You are doing it wrong if you find yourself about to purchase something. Step away if that’s the case. These are free.
3.4.3 Rule 3: Don’t compile!
Make sure you download and install the PRECOMPILED BINARY VERSIONS
Unless you really really really know what you are doing, do not compile from ‘source’ or ‘source code.’
3.4.4 Rule 4: Install the right R software for your machine
R binaries for Mac/OsX Know your OS!
R for Linux Know your OS!
3.4.5 Doubts?
Unsure? Like videos instead?
Go to this link, find your machine system, and follow the directions therein.
3.5 After installation
Launch only RStudio on your machine.
RStudio should be able to find your R installation and will run that for you.
You will see the R console as one of the panes in R studio. On the prompt (>
) line, type either 2+2
, 2^2
, 16/4
or 2*2
. If you get 4
with any of these you are good to go.
3.5.1 Problems?
Just uninstall and try to re-install again.
If that doesn’t work, contact the course director or TA’s
3.6 What is R?
R is an object oriented programming language, useful for a wide assortment of data analysis projects. It is also open source and free.
The R you’ve just installed is the core software along with some “base” packages.
3.7 What are R packages?
R packages are libraries of R functions that accomplish specific tasks.
People write and share their packages with all of us, for free. Thus, open source.
For the most part, we will work with a fairly limited repertoire of R packages. We will use packages that allow us to read data files, to organize datasets so they can be analyzed, to plot the data, and to run statistical tests.
In the Packages pane in RStudio we see all the packages installed on our machine. There are two types of package library, system or user.
To see these, after installing R and then R Studio, on the top menu go to View > Show Packages (or use Ctrl+7 on your keyboard while in RStudio). Or in one of your R Studio panes you’ll see a Packages
thumbnail.
The packages in the system library come along with your R installation. These are often referred to as the base packages.
The user library are packages we install on our machine. The user library will grow over time. As we do more things with R, we’ll find all sorts of additional packages to use.
If you had a prior version of R on your machine, the packages in that version may differ from the newly installed version.
For this reason, during the course I want everybody working with the same R version. Don’t use a prior version. And don’t upgrade to the next version if one comes out during the semester.
3.8 How to install and use packages
This is really important.
Whenever you need to install a package that you are missing, say, the mythical foobar
package, go to the console. Next to the >
prompt, type the command install.packages("foobar")
and then hit enter. R will search for the package on an internet repository, download it, and install it.1
You only need to install a given package once when working with a given R version. Install puts the software on your machine.
But installing a package doesn’t make it available for use.
Whenever we need to use functions within an installed user library package, we must first load it into our working environment during our current R session.
We use the library()
function for this. Or we can go to the Packages thumbnail and click on the box just left of the package name.
For example, typing library(foobar)
in the console will load the mythical foobar
package in our environment. The functions within foobar
that we want to use will now be available. They will remain present in our environment until cleared. For example, by shutting down and restarting R.
Notice the installation command uses quotes ""
whereas the load library command does not.2
The packages in the system library don’t need to be called into the environment using the library()
function. System packages work automagically.
3.9 Packages we’ll need
At the beginning of each chapter I list the packages that will be necessary to run the scripts within that chapter. In order for you to run my scripts, you will first need to install those packages on your machine. Then load their library when you need their functions in an R session.
Note: When you are working in an RMarkdown document, you need to load those libraries in a code chunk within the RMarkdown document. And run it.
Remember, you install a package only once.
After that, the package and its function are stored on your machine. Whenever you need to use the package (or a function(s) within it) for the current session, you will need to load it into your environment using the library()
command
3.10 Super important: Always avoid compiling
When installing R or R packages we are sometimes presented with the option of compiling from source code. For example, after typing an install package command you might see a message in the console like this:
Do you want to install from sources the package which needs compilation (yes/no/cancel)?
In almost ALL cases, the selection should be no
.
Type no
or n
. Selecting ‘no’ forces R to download and install the binary version properly. That’s what we want. Binary versions are pre-compiled.
3.10.1 Reasons not to compile
- No need to make things more complicated, especially when we are starting out.
- Compiling lengthens the installation time…often quite dramatically.
- Source code is often developmental, and may have bugs.
- The biggest: Our machine may not be configured to compile properly (eg, our JRE version is out of date or not set up right.)
Compiling might make it harder to perform the required tasks for this course.
As our R experience and skills grow, down the road we’ll likely want to use packages we need to compile. Let’s wait until then to crack the compile nut.
3.11 Summary
R and RStudio are free. When we pay for commercial software we are mostly buying a smooth installation experience. Open source software installs are never completely idiot proof.
For most people, the installations will go smoothly. If w experience any problems we can always uninstall, and then try to install again.
Otherwise, contact TJ or the course TA for help.
We only have to install packages onto our machine one time per R version. But we will have to run the package library command with each new session or when working inside an RMarkdown document.
Never compile from source unless you know what you are doing.
Alternately, on the RStudio top menu bar click on Tools > Install Packages…↩︎
Seems weird but it makes a lot of sense. Before it got on our machine, the package had a name. Names are character strings. Strings get enquoted in R. Once the package is on our machine, it is an object. Objects are never enquoted in R.↩︎