This exercise is provided to make sure that everybody is on the same page before we begin our coursework. Some of the content in this lab may be review for you, while some is likely to be new material.
Even though you may have seen some of this before, please glance through the contents and work through any of the material that is rusty or unfamiliar.
We will use R, a free and open-source statistical programming language, for all of our coursework.
We will also use many of the datasets accompanying the main text for the course Spatial Ecology and Conservation Modeling, Fletcher and Fortin 2018.
Since our analyses will require coordinating the contents and locations of multiple files, you need to be very comfortable with the concepts of files, directories, absolute paths, and relative paths.
I’m not going to provide materials in class since there are many good tutorials about the topics, such as this one that discusses paths in the context of HTML.
There are many operating system-specific tutorials on the details of the filesystems used on different operating systems. There are more similarities than differences, and I will leave it up to you to educate yourself.
Note that the characters for file separators may differ on different operating systems.
R is pretty good at guessing the correct symbol. For the most part you will be safe to use “/” in your code, but keep in mind the possibility of specifying the wrong file separator in your future debugging endeavors.
Before you continue make sure you feel comfortable with absolute and relative paths. If you are clear on these concepts, you can save hours of frustration when you need to import data files or save data and figures.
self-assessment question: - Can you explain why using relative paths makes your code much easier to share?
You’ll need to work through the steps below to set up:
Once you have your account set up, follow me: michaelfrancenelson.
ON github.com, create a new repository:
Your should name your repository spatial_data_in_r
.
Select the Private Repo option and create a .gitignore file.
Click the dropdown under the Add .gitignore and type R to search for the R template .gitignore file.
Take a screenshot of your repository page.
Navigate to https://desktop.github.com/ and follow the instructions to install GhtHub Desktop on your computer.
In GitHub Desktop, input your GitHub credentials so that you can access your account through the application.
Next, clone the course repository you just created on github.com. Remember the directory you clone your repository into.
You can ignore this tab if you already have R and RStudio on your computer.
If you don’t already have R installed on your computer, you will need to install it before you can set up RStudio. R is free and open-source statistical programming language. Many powerful packages for dealing with spatial data have been developed in recent years, making R a great tool for spatial analysis!
To We’ll use RStudio, an Integrated Development Environment (IDE) specialized for use with the R language.
You can find download links on the RStudio page.
Select the free version appropriate for your operating system (Windows, Mac OS, Linux).
It’s convenient to store all of the files you use for a project in a common location. RStudio has a convenient functionality for this: the use of RProjects. An RProject a way to organize a group of scripts, data, and other files related to a single project.
Use the file browser on your computer to navigate to the directory where you cloned your repository.
Within this directory, create the following two subdirectories:
Make sure you remember the the location of your course R directory!
At this point, your course folder should look something like this:
Now, within RStudio create a new RProject:
Select the Existing Directory option:
Next you should:
This process will create some new files and a new folder in your course directory. It should now look something like this:
Note the .RProj file.
In an effort to be helpful, by default RStudio will make a copy of all the objects you have in memory when you quit the program.
It turns out that this is very helpful for creating hard-to-debug issues in your code.
Fortunately, you can disable this option, either for the entire program, or for specific RProjects.
Look in the Tools menu within RStudio to find the global and project options. Be sure to changes the settings so that RStudio does not save the workspace to a file called .RData when you exit, and that it does not restore your workspace from a .RData file when you start the program.
The correct options should look like this:
At this point, you should have an RStudio session running with your RProject open.
On my computer, my RStudio session looks like:
Verify that your RStudio session shows the open RProject (in the upper right) and that you can see the two subdirectories and the RProj file in the file browser.
Take a screenshot! You’ll need it for the lab report.
We’ll be using lots of packages to deal with spatial data and perform spatial analyses.
To get started, install the following using install.packages()
To verify that the installations were successful you can run the following block of test code:
require(here)
require(ggplot2)
require(raster)
require(ncdf4)
require(sp)
require(sf)
require(rgdal)
require(rgeos)
Note: You have to download the data for the Fletcher and Fortin textbook. You can find the file here:
https://ufdc.ufl.edu/IR00010770/00001
Unzip the archive and move the unzipped folder to the data subfolder of your class project folder.
Your data subfolder should now look like this:
Now that we’ve got the book data unzipped and in-place, we need to process one of the raster files so that it will be accepted as an upload on GitHub.
If you’ve managed to install the R packages above, and you have the data in the correct location, you can run the following code block to process the raster. For now, don’t worry about what the code is doing, this is just a pre-course setup step.
This raster processing step may take quite a while to complete. The process took about 10 minutes on my computer.
require(raster)
require(here)
# Read the raster data
nlcd_r = raster(here(
"data",
"Fletcher_Fortin-2018-Supporting_Files",
"data",
"nlcd2011SE"))
# Re-write it in a compressed NetCDF file
writeRaster(
nlcd_r,
filename = here(
"data",
"Fletcher_Fortin-2018-Supporting_Files",
"data",
"nlcd2011SE.nc"),
compression = 8)
Now, you need to double check that the raster was properly written to the file.
You should see a file called nlcd2011SE.nc
:
Almost ready!
Now you need to compress the folder called nlcd2011SE
, saving at as a zip file. When you’re finished you should see both the nlcd2011SE.nc
file and a new file called nlcd2011SE.zip
:
nlcd2011SE
.Create a document and include the following screenshots: