Working with Packages
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
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?
‘Install’ = download
‘Load’ = bring into R’s memory so you can use it
You can indicate which specific repository (web site) to install from from with the repos argument:
A lot of packages are on GitHub. Install them with the devtools package:
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:
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.
‘Load’ = bring a package into memory so you can use it in R.
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.
How about the maps package? Ans
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.
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.
Today we saw: