Installing R and RStudio
Before you start
- Check which version of Mac OS you have. To do this, click on the Apple icon on the top-left of your screen, then click “About This Mac”. A little window should pop up with version information.
- Make sure that you have about 500 MB of free space on your machine. We will need this space to install both R and RStudio, and when the program installs the compressed files expand to take much more space.
Go to this website.
If your mac is OS X 10.11 and higher (this should be the case for most of you), click the
R-3.6.2.pkglink. If your mac is OS X 10.9 or 10.10, click the
After clicking on the link, the file (package installer) will be downloaded onto your computer. Once the file has been downloaded, click on the
.pkgfile that downloads in your default Download folder and walk through the installation process. Accept the default options for now.
R is now installed on your computer.
- Double-click on the R icon in the Applications folder:
A window titled “R Console” should pop up:
> character is called the prompt and is the place where we enter commands for the program to interpret. For instance, if you type
25+5 followed by the return/enter key, you will see:
- To quit R, call the quit function by going to
File -> Quit R.
R will ask you if you want to save your work:
Click on “Don’t Save”. You have now left R.
We are not going to use R this way often, as we will use an integrated environment called RStudio.
Go to this website.
- Click on the
Freelink (note: there are paid options, but we won’t be using those!). The file download will begin.
- If you have an older computer, you may have to find your installer on this page, but feel free to contact us about that!
- Once the file has been downloaded, click on it. A window should pop up:
Click on the RStudio icon and drag it into the Applications folder.
RStudio is now installed on your computer.
Note: For RStudio to work, R needs to be installed first.
- Double-click on the RStudio icon in the Applications:
A window titled “RStudio” should pop up, looking something like this:
- Just as in R, the
>character in the Console window is where we enter commands for the program to interpret. You can try entering something there and then hitting the
Enterkey to see:
- To exit RStudio, click “RStudio” in the menu bar, then click “Quit RStudio”. Alternatively, use the
Cmd-Qshortcut. If prompted to save the workspace, do not save it.
Installing the geospatial and federal data packages
When RStudio is open and working, create a new R script file.
Here’s where to make a new script:
When you open a new script, your RStudio will now look like this (without the red labels):
Once this is done copy the following into your script window:
And follow it by pressing the
Source button. You might get some red words showing up, and that is normal! If you see any red with the word
Error in it, this may indicate a problem however.
This looks like:
- If the red stop sign in the top right corner of the console is still highlighted, an installation process is still running.
- If you see a blue > in the console , you’re ready to go.
This action is “sourcing” i.e. running a bit of code from the internet that installs a bunch of libraries that provide functionality that is not included with standard R installation.
Alternatively, you can copy the following into the script window or console:
install.packages("ggplot2") install.packages("rgdal") install.packages("dplyr") install.packages("sf") install.packages("gstat") install.packages("tmap") install.packages("measurements") install.packages("daymetr") install.packages("FedData") install.packages("lubridate") install.packages("raster") install.packages("data.table") install.packages("broom")
Checking that the packages installed correctly
We have a test script to make sure all packages have installed correctly. To run this test script, we will “Source” it’s location like we did above for the package installation script.
In your script window you can type in:
… and then press the
Source button. This will look like:
Next, we will run the test script to see what happens. When we run this script, it will create a new directory called
WorkingDir to save all of our test images in. This directory will also be used to store data for this workshop. (You can change the default creation location for
WorkingDir by calling the function as
run_workshop_test(workingDir='YOUR DIR'), shown below is how this looks for the default).
Once we press the
Source some things will download and be calculated, and a new file called
WorkingDir will show up in our file list:
To check that all our images have been created, we can click on
WorkingDir in our File list in RStudio (see #2 in above figure), and then navigate to
Once you open this file, you should see several images:
You can click on each of these to open them up. Here is a grid of what each one should look like:
Download data for this workshop
The final thing we need to do for this workshop is to download the data. To do this, we need to run one more function. To avoid re-running our test function, we can put a little
# in front of this line. So the code in our script will now look like:
source('https://raw.githubusercontent.com/data-carpentry-for-agriculture/trial-lesson/gh-pages/_episodes_rmd/functions.R') #run_workshop_test() download_workshop_data()
If we navigate to our
WorkingDir directory we will see that a folder called
data and a script called
functions.R have now appeared. This process looks like:
Download and Install QGIS
To download QGIS, visit the QGIS download site and choose one of the standalone installers. Be sure to check your Mac version to download the correct package.
Note: This is a ~1Gb file, so it’s likely to take some time to download.
You should see a new icon in your Applications folder:
When you double click QGIS should open up:
Citation: R/RStudio install structure from Kenneth Tay