2.5 How to use packages?
Base R comes with many packages already installed. Look at packages
to see which ones are already installed. There are currently 22421
packages on Cran: see here (to check all packages and their status)
(repository for all packages). No one uses all packages so do not try to install all of them. Simply install what you need!! RMarkdown will let you know if you are running a specific code that lacks a package and asks you to download it.
2.5.1 Installation
The best option is to use the menu above (under Tools) and click Install packages
, or type in install.packages(“package.name”). Make sure to always have install dependencies
ticked (using the first option).
2.5.2 Loading
Use the following to load a package: library(package.name)
. Once the package is loaded, you can use any of its functions directly into your code. Sometimes you may need to specify to use a particular function from within a particular package, in this case use: package.name::function
. We will most probably not use this today, but this is something you need to know about otherwise undesirable results may occur (or even errors!).
2.5.3 Finding packages and help
Under the Files pane (right bottom), click on the menu Packages and you will have access to all installed packages. Click on a package and you can see the associated help files. You can also type the following to find help: ?package.name. ??function e.g.,
## starting httpd help server ... done
Or try clicking on the function name to find details of what to specify: e.g., scroll on lmer
(assuming lme4
is installed). Do a Ctrl/Cmd + left mouse click on a function to display options.
## function (formula, data = NULL, REML = TRUE, control = lmerControl(),
## start = NULL, verbose = 0L, subset, weights, na.action, offset,
## contrasts = NULL, devFunOnly = FALSE)
## {
## mc <- mcout <- match.call()
## missCtrl <- missing(control)
## if (!missCtrl && !inherits(control, "lmerControl")) {
## if (!is.list(control))
## stop("'control' is not a list; use lmerControl()")
## warning("passing control as list is deprecated: please use lmerControl() instead",
## immediate. = TRUE)
## control <- do.call(lmerControl, control)
## }
## mc$control <- control
## mc[[1]] <- quote(lme4::lFormula)
## lmod <- eval(mc, parent.frame(1L))
## mcout$formula <- lmod$formula
## lmod$formula <- NULL
## if (is.matrix(y <- model.response(lmod$fr)) && ncol(y) >
## 1) {
## stop("can't handle matrix-valued responses: consider using refit()")
## }
## devfun <- do.call(mkLmerDevfun, c(lmod, list(start = start,
## verbose = verbose, control = control)))
## if (devFunOnly)
## return(devfun)
## if (identical(control$optimizer, "none"))
## stop("deprecated use of optimizer=='none'; use NULL instead")
## opt <- if (length(control$optimizer) == 0) {
## s <- getStart(start, environment(devfun)$pp)
## list(par = s, fval = devfun(s), conv = 1000, message = "no optimization")
## }
## else {
## optimizeLmer(devfun, optimizer = control$optimizer, restart_edge = control$restart_edge,
## boundary.tol = control$boundary.tol, control = control$optCtrl,
## verbose = verbose, start = start, calc.derivs = control$calc.derivs,
## use.last.params = control$use.last.params)
## }
## cc <- checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv,
## lbound = environment(devfun)$lower)
## mkMerMod(environment(devfun), opt, lmod$reTrms, fr = lmod$fr,
## mc = mcout, lme4conv = cc)
## }
## <bytecode: 0x00000266e95b8708>
## <environment: namespace:lme4>