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 package
    • detach("package:packagename"): Unload a package
    • remove.packages("packagename"): Unintall package
  • Other functions
    • library(): Display all installed packages
    • library(help=packagename): Describe a package
    • search(): Display all loaded packages
    • ls("package:packagename"): Display all objects within a package
    • packagename::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)

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

ls("package:MASS") # Display content of the package MASS

detach("package:MASS") # Unload the package

library() # Package is not installed anymore

14.6.2 Exercise: Packages

  1. Use your usual script to save your code. Install the package plotly.
  2. Load the package plotly.
  3. Display the content of the package plotly.
  4. Unload the package plotly.
  5. Uninstall the package plotly.

14.6.3 Solution: Packages (HOMEWORK)