R/servers.R

#'@title set HTTP and HTTPS proxies
#'@description set the HTTP and HTTPS proxies when running R on
#'one of the Wikimedia servers
#'
#'@examples
#'\dontrun{
#'#This will fail in the cluster
#'devtools::install_github("ironholds/urltools")
#'
#'#This will work
#'set_proxies()
#'devtools::install_github("ironholds/urltools")
#'}
#'
#'@seealso \code{\link{get_package}}, which requires
#'this to work if run on the cluster.
#'@export
set_proxies <- function(){
  Sys.setenv("http_proxy" = "http://webproxy.eqiad.wmnet:8080")
  Sys.setenv("https_proxy" = "http://webproxy.eqiad.wmnet:8080")
}

#'@title Load a package, installing it if you don't already have it.
#'@description takes a package name, checks if it's installed, installs
#'it if it isn't, and loads it either way.
#'
#'@param package_name the name of the package, as a string
#'
#'@seealso \code{\link{set_proxies}}, which must be run
#'first if you're calling this on the Analytics cluster.
#'@export
get_package <- function(package_name){
  existing_packages <- rownames(installed.packages())
  if(!package_name %in% existing_packages){
    install.packages(package_name, repos = c(CRAN = mirror), depend = TRUE)
  }
  library(package_name, character.only = TRUE)
  return(invisible())
}

#'@title load an entire directory of R files
#'@description instead of running source() on a series of files
#'in a ./R directory, just load the contents of all the .R files
#'in a provided directory. This loads to the global namespace,
#'so be aware.
#'
#'@param directory the directory.
#'
#'@return nothing, if it worked.
#'
#'@export
source_directory <- function(directory){
  files <- list.files(directory, pattern = "\\.R$", full.names = TRUE, ignore.case = TRUE)
  lapply(files, source, local = globalenv())
  return(invisible())
}
Ironholds/olivr documentation built on May 7, 2019, 6:40 a.m.