R/utils.R

Defines functions drop_nulls rmv_class add_class

Documented in add_class drop_nulls rmv_class

#' @title Adding or Removing Classes
#'
#' @description Add or remove R classes in a pipeline-friendly way.
#'
#' @name add_rmv_class
NULL

#' @describeIn add_rmv_class add the class
#' @export
add_class <- function(x, new_class) {
    assert_that(is.character(new_class))
    class(x) <- union(class(x), new_class)
    return(x)
}

#' @describeIn add_rmv_class remove the class
#' @export
rmv_class <- function(x, old_class) {
    assert_that(is.character(old_class))
    class(x) <- setdiff(class(x), old_class)
    return(x)
}

#' @title
#' Remove NULLs from List
#' @export
drop_nulls <- function(x) {
    x[!vapply(x, is.null, FUN.VALUE = logical(1))]
}
tjpalanca/tjutils documentation built on Jan. 20, 2021, 2:01 p.m.