14.6 Packages
- Many objects (functions, data sets etc) are stored in packages (see here)
- Packages are written by various authors and can be loaded for an R session to use their content
- e.g. Yves Rosseel works on the package Lavaan (with others) which can be used to estimate structural equation models (open source!)
- Some packages are loaded permanently (e.g. base), others not (true?)
- Sometimes there are conflicts between packages (“function is masked”)
- Central functions
install.packages("packagename")
: Install package (is downloaded)install.packages("C:/.../crowdflower.tar.gz", repos = NULL, type = "source")
: Install package from hardrive
library(packagename)
: Load an installed packagedetach("package:packagename")
: Unload a packageremove.packages("packagename")
: Unintall package
- Other functions
library()
: Display all installed packageslibrary(help=packagename)
: Describe a packagesearch()
: Display all loaded packagesls("package:packagename")
: Display all objects within a packagepackagename::bar
: Load just use one object in a package (e.g. a function)
14.6.1 Example: Packages
# Create a matrix
M <- diag(5)
M[1:20]<- c(1:20)
M
ginv(M) # Funkcion ginv()
# ginv calculates the Moore-Penrose-Inverse of the matrix M
# But it doesn not work here.. why?
help.search("ginv") # function is in the package MASS
install.packages("MASS") # Install package
MASS::ginv(M) # Call function without loading the whole package
library(MASS) # Load whole package
ginv(M)
ls("package:MASS") # Display content of the package MASS
detach("package:MASS") # Unload the package
ginv(M)
search()
library()
remove.packages("MASS")
library() # Package is not installed anymore
14.6.2 Exercise: Packages
- Use your usual script to save your code. Install the package
plotly
. - Load the package
plotly
. - Display the content of the package
plotly
. - Unload the package
plotly
. - Uninstall the package
plotly
.