Spatial Data Analysis with R
Society for Conservation GIS, July 2021

Working with Packages

Finding Packages

People in your field

CRAN - List of all packages by name

CRAN - Task Views

Publications

Twitter

R Spatial Ecosystem

Tidyverse

The tidyverse is a collection of R packages that share common design principles and are designed to work together seamlessly.

They generally support piping syntax which looks like this:

x <- mydata %>% filter(weight > 40) %>% group_by(zipcode) %>% ...

You can install the core tidyverse packages collectively with:

install.packages("tidyverse")

More info: http://tidyverse.org

Specialty Packages

Ecology

Sub-Field Package(s)
Animal movement adehabitatHR/LT/MA, moveVis
Community Ecology vegan
Landscape Ecology landscapemetrics, spatialEco, SDMTools
Species Distribution Models dismo


What other packages are used in your work?

Installing and Loading Packages

‘Install’ = download

‘Load’ = bring into R’s memory so you can use it

RStudio Packages Tab


Console

install.packages("raster")

You can indicate which specific repository (web site) to install from from with the repos argument:

install.packages("raster", TRUE, repos="http://cran.cnr.berkeley.edu/")

install.packages("tlocoh", repos="http://R-Forge.R-project.org")


From GitHub

A lot of packages are on GitHub. Install them with the devtools package:

library(devtools)
install_github("ucanr-igis/uavimg")


Trouble Shooting Package Installation Problems

Dependencies. Most packages require other packages to work. That means they all have to be installed to work properly.

Package not available. A common error message:

## Warning: package 'Roster' is not available (for R version 3.4.4)

Most of the time, a warning like this means the package doesn’t exist at all. Check your spelling, capitalization, and whether it might be in a different repository.

Warning: package ‘timeDate’ was built under R version 3.1.2

Most of the time this means the package was built using a newer version of R. In other words, your R is not up-to-date. You can keep working, but make a note to update R in the near future.

Loading

‘Load’ = bring a package into memory so you can use it in R.

library(raster)
require(raster)

Unload a package:

detach("package:raster")

Update a package:

update.packages(oldPkgs="raster")  ## update a single package

update.packages()    ## update ALL installed packages

What’s in a package?

Content Cmd
Functions!
Index of all help topics help(package=“maptools”)
Vignettes browseVignettes(package = “raster”)
Bundled datasets data(package=“myPackge”)
Installation directory system.file(package = “maptools”)
Files system.file(“external/countries.csv”, package=“raster”)
Bundled demos demo(package=“sp”)
Run a demo demo(webmap)

View the datasets in the package tmap, run:

View the datasets that come with the ‘tmap’ package.

## First, make sure tmap is installed (if not, install it!!)
library(tmap)

## View datasets in the tmap package
data(package="tmap")

How about the maps package? Ans

library(maps)
data(package = "maps")

To see a list of datasets in all packages currently loaded, run data() without an argument.

## View datasets in ALL packages currently loaded
data()

To see all datasets in all installed packages, run

data(package = .packages(all.available = TRUE))

Duplicate Function Names

Occasionally two or more packages will have a function with the same name.

R will use whichever one was loaded first.

Best practice: use the package name and the :: reference to specify which package a function is from.

x <- sp::over()
x <- grDevices::over()

y <- raster::select()
y <- dplyr::select()

When you use the package_name::function_name syntax, you don’t actually have to first load the package with library().


Resolving Name Conflicts with the conflicted Package

When you call a function that exists in multiple packages, R uses whichever package was loaded first.

The conflicted package helps you avoid problems with duplicate function names, by specifying which one to prioritize no matter what order they were loaded.

library(conflicted)

# Set conflict preference
conflict_prefer("filter", "dplyr")
conflict_prefer("count", "dplyr")
conflict_prefer("select", "dplyr")

# From here on out, anytime we call select() or filter(), R will always use the dplyr version.

Summary

Today we saw:



Next: Basic GIS Concepts