R/objects_of_class.R

Defines functions objects_of_class

Documented in objects_of_class

#' List objects of a certain class.
#'
#' @param class (character) The name of class.
#' @param all.names (logical) If `TRUE`, all object names are returned.
#'           If `FALSE`, names which begin with a `.` are omitted.
#' @param envir The environment to search for objects in.
#'
#' @return Character vector.
#' @export
objects_of_class <- function(class = NULL,
                             all.names = FALSE,
                             envir = parent.frame()) {
  checkmate::assert_character(class, null.ok = TRUE)

  all_variable_names <- objects(envir, all.names = all.names)

  if (length(all_variable_names) == 0 || is.null(class)) {
    return(all_variable_names)
  } else {
    # Object names of class to return
    mget(all_variable_names, envir = envir) %>%
      purrr::keep(~ inherits(.x, class)) %>%
      names()
  }
}
GegznaV/BioStat documentation built on Aug. 14, 2020, 9:30 p.m.