R/detach_all.R

Defines functions detach_all

Documented in detach_all

#' Detach all non-essential objects from all environments
#' 
#' This function detaches all non-essential objects from all environments and is
#' a simple rewrite of a function available through the codes provided by
#' the Choice Modeling Centre in Leeds.
#' 
#' @examples
#' detach_all()
#' 
#' @export

detach_all <- function() {
  subset_environment <- substring(search(), 1L, 8L) != "package:" &
    search() != ".GlobalEnv" & search() != "Autoloads" & 
    search() != "CheckExEnv" & search() != "tools:rstudio" &
    search() != "TempEnv"
  
  object_positions <- seq_len(length(search()))[subset_environment]
  
  for (i in seq_len(length(object_positions))) {
    if (length(object_positions) > 0L){
      detach(pos = object_positions[1L])
      object_positions <- seq_len(length(search()))[subset_environment]
    }
  }
}
edsandorf/gizmo documentation built on Oct. 7, 2022, 3:45 p.m.