Tornar as chamadas da biblioteca em R mais fácil

Em R, se você tiver uma chamada de biblioteca (<pacote>), ele assume que o pacote já está instalado. Se não for, o R carregará um menu interativo solicitando o repo a partir do qual deseja instalar.

Se você não quer lidar com o incômodo de ter certeza de que está instalado, use isto. Isso deve tornar mais fácil pegar repositórios, digamos no Github, e executá-los. Edite a linha de repos conforme necessário, se você não for dos Estados Unidos.

#' Installs the package, if it exists, and bypasses the interactive
#' prompt.
#'
#' The default location, if not specified, is at .libPaths()[1] which is
#; usually /usr/lib/R/site-library
#'
#' @param pack the package that will be installed
#' @param ... any other arguments passed for install.packages
#' @return NULL this is a statement. Use other functions to check if
#' install works
#' @examples
#' install('e1071')
#' @seealso code{link{lib}}
install
= function (pack, ...) {

install
.packages(pack,
dependencies
=TRUE, repos='http://cran.us.r-project.org',
...
)
}
inst
= install # Function alias

#' Checks if a package is installed
#'
#' @param pack the package we will check
#' @param ... any other arguments for library
#' @return a boolean value if it is installed correctly
#' @seealso code{link{lib}}
libCheck
= function (pack, ...){

# Load a lib from .libPaths()[1]

return (suppressWarnings(
library
(pack,
logical
.return = TRUE, character.only = TRUE,
...)
)
)
}

#' Checks if a package is installed and loads it. If not, it will install and then
#' try to load it
#'
#' @param pack the package that will be checked
#' @param ... other arguments passed into libCheck
#' @return a boolean stating if it is loaded
lib
= function (pack, ...){

if (libCheck(pack, ...)){
# Loaded successfully
return (TRUE)
}
else{
# Try to install and load
install
(pack)
return (libCheck(pack, ...))
}
}