Attachment 'local.R'

Download

   1 #' Collection of Local Functions
   2 #'
   3 #' A miscellaneous collection of utility and special purpose functions.
   4 #' 
   5 #' Ideally this package should be installed at start up, which can be done by adding \code{options(defaultPackages = c(getOption("defaultPackages"), "local"))} to \code{.Rprofile}.
   6 #' 
   7 #' Also, this package expects that the path to the intial working directory (home folder) is set in \code{.Rprofile} using the likes of \code{options(home = "/Users/username/R")} (for MacOS, where 'username' is the users login name).
   8 #'
   9 #' For a complete list, use \code{library(help = "local")}.
  10 #'
  11 #' @author Ian Riley \email{ian@riley.asia}
  12 #' @docType package
  13 #' @section Version:
  14 #' \Sexpr{packageVersion("local")}
  15 #' @name local
  16 "_PACKAGE"
  17 
  18 self <- "local"
  19 options(installing_package = TRUE)
  20 source('~/.Rprofile')
  21 options(installing_package = NULL)
  22 home_path <- function() ifelse(is.null(options()$home), getwd(), options()$home)
  23 self_path <- file.path(home_path(), paste0(self, "-package"))
  24 self_url <- desc::desc_get_urls(self_path)[1]
  25 
  26 #' Document and install self
  27 #'
  28 #' Load required libraries, \code{devtools} and \code{roxygen2}, then document and install this  or specified package.
  29 #' @keywords internal
  30 #' @return Nothing returned.
  31 install_self <- function(package = NULL, path = NULL)
  32 {
  33 	caller <- as.character(match.call())[1] 
  34 	if (is.null(package))
  35 		package <- unlist(strsplit(caller, ':::'))[1]
  36 	if (package == "install_self")
  37 		stop("no package name provided")
  38 	if (is.null(path))
  39 		if (package == self)
  40 	 		path <- self_path
  41 		else {
  42 			package <- gsub("-package$", "", package)
  43 			path <- file.path(home_path(), paste0(package, "-package"))
  44 		}
  45 	
  46 	message(sprintf("'%s' requested installation of '%s' from '%s'", caller, package, path))
  47 	require(devtools)
  48 	require(roxygen2)
  49 	document(path)
  50 	install(path)
  51 	library(package, character.only = TRUE)
  52 }
  53 
  54 #' Set working directory to package root
  55 #'
  56 #' @keywords internal
  57 #' @return Current working directory.
  58 pkgdir <- function(package = NULL)
  59 {
  60 	if (is.null(package)) package <- self
  61 	setwd(file.path(home_path(), paste0(package, "-package")))
  62 	message("Working directory set to: ", getwd())
  63 }
  64 
  65 isLoaded <- function()
  66 {
  67 	isNamespaceLoaded(self)	
  68 }
  69 
  70 local <- isLoaded()

You are not allowed to attach a file to this page.