This tutorial is all about the basics you need to run your labs. We covered most of this in Lab 1.
Everything in this tutorial is a one-off to get R and R-studio set up.
First, in an easy to access place on your computer, make a folder called GEOG-364. This is where all your labs and projects are going to live.
Now everything is installed, open R-studio (NOT R!). If you are using R-Studio cloud, you should in your workspace. Make a new R-Project and call it GEOG364_Lab1_PROJECT (or whatever lab you are working on)
You will be greeted by three panels:
If you have used R before, you might see that there are variables and plots etc already loaded**. It is always good to clear these before you start a new analysis. To do this, click the little broom symbol in your environment tab
R-studio wants to be helpful and will try to re-load exactly where you were in a project when you log back in. This can get confusing, so we are going to turn this off.
ON A MAC: Click on the R-studio menu button on the top left of the screen, then click Preferences.
ON A PC/R-Studio Cloud: Click on Tools-> Global Options -> Preferences
Now:
As described in Tutorial 3, we programme in R by asking a series of commands. R is open source meaning anyone can create a command. Over the last 20 years, millions of commands have been written.
These tend to be grouped together into collections called Packages
or Libraries
(two names for the same thing). They are similar to apps on your phone. For example, one package contains the complete works of Shakespeare; allows interactive website design; another allows advanced Bayesian statistics.
There is a package for literally everything and there are now about 20,000 packages available. You can see the full list here: https://cran.r-project.org/web/packages/available_packages_by_name.html
This is far too many to store on your computer, so most live on the internet in an online (free) “Package Store”. You can download the ones you want, ready to load later.
So to access the commands in a package we need these two steps:
A close analogy is your phone: There are millions of apps available from banking, to 50 different calendar apps. You don’t have every app in the world installed on your phone - and you don’t have every app you do download running at the same time. Instead you download the apps that you think you will need (occasionally downloading a new one on the fly) - and when you need to use an app, you click on it to open.
Look at the Packages tab next to the plot one, you can see which packages/libraries are already pre-installed onto your computer.
If the package you want isn’t listed:
OR
In the console, run the install.packages()
command on the package you want to download with quotes around the package name e.g.
install.packages("bardr")
OR
Note, if you run this command mulitple times, or the packages is already loaded, R-Studio might want to restart and sometimes gets confused. If it keeps asking, close R-studio, reopen and try again. If it really doesn’t want to work, open R itself and run in the console there.
Try installing the bardr
package onto your computer
Now we are going to download/install (hopefully) all the packages you need for these labs.
Copy this command into the R CONSOLE and press enter to run.
install.packages(c("tidyverse","abind","car","corrplot","dismo",
"elevatr","fields","ggpubr","ggstatsplot",
"hrbrthemes", "ISLR","knitr", "lattice","matlab",
"maps","mapdata","maptools","mapview","matlab",
"ncdf4","olsrr", "plotly","raster",
"rasterVis","RColorBrewer","readxl","remotes",
"rgdal","rgeos","rmapshaper","rmdformats",
"rnaturalearth","sf", "skimr", "sp",
"spdep","spatialEco","spatialreg","spatstat",
"Stat2Data","tidycensus","tigris","tmap",
"units","USAboundaries", "usmap","viridis",
"yarrr"))
When you press enter, a load of text should stream down your console. Just let it run until the console is back to the > symbol and you can type.
Sometimes this will give you an error and not run, This can be because copy/pasting from the internet messes up the quote marks around each package name. In that case, you can easily manually download them by clicking the INSTALL button at the top of the packages tab, then choosing each name in turn.
We also want to download some larger spatial datasets stored separately to the main packages.
Copy and run these lines in the console.
remotes::install_github("ropensci/USAboundariesData")
remotes::install_github("ropenscilabs/rnaturalearthhires")
When all the download text is streaming down your screen, it’s hard to know what is an error and what is just “informative”. The easiest way to check is to load all the libraries at once and see whether there are errors.
(e.g this is the same as clicking every app on your phone to see if they all installed OK.)
Copy the code below into the console TWICE. E.g. copy it once, wait until it stops running (it will show a load of “welcome” text again that makes it hard to see errors). The SECOND time you copy it into the console, it should just run without error or messages.
library("tidyverse")
library("abind")
library("car")
library("corrplot")
library("dismo")
library("elevatr")
library("fields")
library("ggpubr")
library("ggstatsplot")
library("hrbrthemes")
library("ISLR")
library("knitr")
library("lattice")
library("matlab")
library("maps")
library("mapdata")
library("maptools")
library("mapview")
library("matlab")
library("ncdf4")
library("olsrr")
library("plotly")
library("raster")
library("rasterVis")
library("RColorBrewer")
library("readxl")
library("remotes")
library("rgdal")
library("rgeos")
library("rmapshaper")
library("rmdformats")
library("rnaturalearth")
library("sf")
library("skimr")
library("sp")
library("spdep")
library("spatialEco")
library("spatialreg")
library("spatstat")
library("Stat2Data")
library("tidycensus")
library("tigris")
library("tmap")
library("units")
library("USAboundaries")
library("usmap")
library("viridis")
library("yarrr")
** IF YOU HAVE ERRORS TALK TO A TEACHER **
You need to make a new project before you start EVERY lab!
An R-project is a special folder that will store everything to do with each lab in one place on your computer. This is incredibly useful - it means that if you switch from R-Cloud, to the lab computers, to your laptop, all you have to do is to move the folder and everything will just work. Learn more here.
This step is needed for EVERY lab.
Note, if you are running R-Studio Cloud, you just need to click “Create Project” and name it.
New Project
New Directory
, then New Project
Create Project
R will change slightly. If you look at the top of the screen in the title bar, it should say GEOG364-Lab1-Project R Studio.
The Files tab should have gone to your project folder. Essentially, R-Studio is now “looking” inside your Lab 1 folder, making it easier to find your data and output your results.
Essentially, R-Studio is now “looking” inside your Lab 1 folder, making it easier to find your data and output your results.
If you want one, final check, try typing this into the console (INCLUDING THE EMPTY PARANTHESES/BRACKETS), press enter and see if it prints out the location of Lab 1 on your computer. If not, talk to an instructor.
getwd()
OK, let’s imagine that you get halfway through your lab and your computer dies. How do you get back to your Lab work? Try this now. Close down R-Studio.
To reopen a lab:
This will reopen R for that specific lab, so you can continue where you left off.
It means you can also open several versions of R studio for multiple projects, which can be very useful in keeping labs separate and staying sane.
Website created and maintained by Helen Greatrex. Website template by Noli Brazil