Make sure to download and install the software, packages and data onto your computer as outlined in the following section prior to attending the workshop.


Software, packages and data (to install prior to workshop)

R

Version 3.4.2 is the latest version and the one used in this workshop. R is an open source data analysis and visualization programming environment whose roots go back to the S programming language developed at Bell Laboratories in the 1970’s by John Chambers. It’s available for most operating systems including Windows, Mac and Linux.

You can download the latest version of R for Windows from https://cran.r-project.org/bin/windows/base/ and for the latest version of R for Mac got to https://cran.r-project.org/bin/macosx/

RStudio

RStudio is an integrated development environment (IDE) for R. It offers a user friendly interface to R by including features such as a source code editor (with colored syntax), data table viewer, git and GitHub integration and markdown output. Note that RStudio is not needed to run R (which has its own IDE environment–albeit not as nice as RStudio’s) but it makes using R far easier. RStudio is free software, but unlike R, it’s maintained by a private entity which also distributes a commercial version of RStudio for businesses or individuals needing customer support.

You can download the free RStudio desktop from this link https://www.rstudio.com/products/rstudio/download3/#download

R packages

R packages are installed in the user’s home directory (C:/Users/…) by default. This is advantageous in that you do not need to have administrative privileges to install any package. But it can be a disadvantage in that if someone else logs on to the same computer where you installed a package, that person will not have access to it requiring that she install that same package in her home directory thereby duplicating an instance of that package on the same computer.

The following CRAN packages will be used in this workshop: raster, tmap, rastervis, gstat and rgdal. There are two ways you can install R packages from the CRAN repository: via the command line or via the RStudio interface.

Package installation option 1

For the command line approach simply run the following lines of code in an R console:

install.packages("raster")
install.packages("tmap")
install.packages("rastervis")
install.packages("gstat")
install.packages("rgdal")

Note that most of these packages have dependencies, but these dependencies are automatically loaded for you.

Package installation option 2

Packages can also be installed via the RStudio interface. To install a CRAN package from within RStudio, click on the Packages tab, select Install and choose Repository (CRAN, CRANextra) as the source location. In the following example, the package raster is installed from CRAN.

knitr::include_graphics("Figures/Install_CRAN_packages.png")

Data

Data used in this workshop can be downloaded from https://mgimond.github.io/megug2017/data.zip then extracted into a folder for use in the workshop.


R and RStudio basics

Command line vs. script file

Command line

R can be run from a R console or a RStudio command line environment. For example, we can assign four numbers to the object x then have R read out the values stored in x by typing the following at a command line:

x <- c(1,2,3,4)
x
[1] 1 2 3 4

<- is referred to as the assignment operator. Operations and functions to the right of the operator are stored in the object to the left.

R script file

If you intend on typing more than a few lines of code in a command prompt environment, or if you wish to save a series of commands as part of a project’s analysis, it is probably best that you type your commands in an a R script file. Such file is usually saved with a .R extension.

You create a new script by clicking on the upper-left icon and selecting R Script.

In RStudio, you can run (or execute in programming lingo) a line of code of an R script by placing a cursor anywhere on that line (while being careful not to highlight any subset of that line) and pressing the shortcut keys Ctrl+Enter (or Command+Enter on a Mac).

You can also run an entire block of code by selecting (highlighting) all lines to be run and pressing the shortcut keys Ctrl+Enter. Or, you can run the entire R script by pressing Ctrl+Alt+R.

In the following example, the R script file has three lines of code: two assignment operations and one regression analysis. The lines are run one at a time using the Ctrl+Enter keys and the output of each line is displayed in the console window.