R/potentials.R

Defines functions envObs insertLayer remove_geom

# Potential additions
remove_geom <- function(ggplot2_object, geom_type) {
  # Delete layers that match the requested type.
  layers <- lapply(ggplot2_object$layers, function(x) {
    if (class(x$geom)[1] == geom_type) {
      NULL
    } else {
      x
    }
  })
  # Delete the unwanted layers.
  layers <- layers[!sapply(layers, is.null)]
  ggplot2_object$layers <- layers
  ggplot2_object
}


# Insert a layer into a plot list (ie. under an existing one)
# https://stackoverflow.com/questions/20249653/insert-layer-underneath-existing-layers-in-ggplot2-object
insertLayer <- function(P, after=0, ...) {
  #  P     : Plot object
  # after  : Position where to insert new layers, relative to existing layers
  #  ...   : additional layers, separated by commas (,) instead of plus sign (+)

  if (after < 0)
    after <- after + length(P$layers)

  if (!length(P$layers))
    P$layers <- list(...)
  else
    P$layers <- append(P$layers, list(...), after)

  return(P)
}


#' Show memory usage for all objects
#'
envObs <- function(){
  data.frame('object' = ls()) %>%
    dplyr::mutate(size_unit = object %>%sapply(. %>% get() %>% object.size %>% format(., unit = 'auto')),
                  size = as.numeric(sapply(strsplit(size_unit, split = ' '), FUN = function(x) x[1])),
                  unit = factor(sapply(strsplit(size_unit, split = ' '), FUN = function(x) x[2]), levels = c('Gb', 'Mb', 'Kb', 'bytes'))) %>%
    dplyr::arrange(unit, dplyr::desc(size)) %>%
    dplyr::select(-size_unit)
}
TheZetner/utilitarian documentation built on Aug. 13, 2022, 12:31 p.m.