R/sui-generis.R

Defines functions sg

Documented in sg

#' Sui generis: show the unique values of character and factor columns
#'
#' Ever get a new dataset and you just want to know all the unique values for
#' the character and/or factor columns? Perhaps so that you can filter or
#' summarise or whatnot on the actual data you need? Well this function will do
#' that for you.
#'
#' @param .data a \code{data.frame}
#'
#' @return a \code{list} where each element is a single column \code{tibble},
#'   referring to a character/factor column of \code{.data}, and contains the
#'   unique values
#' @export
#'
#' @examples
#' \dontrun{
#' sg(dplyr::storms) # Returns a three element list
#' sg(mtcars) # Returns a message and nothing else
#' }
sg <- function(.data) {
	assertthat::assert_that(
		is.data.frame(.data),
		msg = "`.data` must be a `data.frame`"
	)

	if (inherits(.data, "sf")) {
		.data <- strip_geometry(.data)
	}

	.data <- .data %>%
		dplyr::select(
			tidyselect::vars_select_helpers$where(is.character), # nolint
			tidyselect::vars_select_helpers$where(is.factor) # nolint
		)

	if (ncol(.data) == 0L) {
		message("`.data` has no factor or character columns.")
		return(invisible(NULL))
	}

	.data %>%
		names() %>%
		purrr::map(~ unique(.data[[.]])) %>%
  stats::setNames(names(.data))
}
baslat/sak documentation built on April 14, 2025, 4:14 p.m.