compact_list: Remove empty elements from lists

View source: R/utils_compact.R

compact_listR Documentation

Remove empty elements from lists

Description

Remove empty elements from lists

Usage

compact_list(x, remove_na = FALSE)

Arguments

x

A list or vector.

remove_na

Logical to decide if NAs should be removed.

Note

By default, compact_list() does not remove "empty" elements from deeper list levels. Only if an element on the first level of a list is "empty", it is removed.

Examples

compact_list(list(NULL, 1, c(NA, NA)))
compact_list(c(1, NA, NA))
compact_list(c(1, NA, NA), remove_na = TRUE)

# remove only NULL on top level, don't change deeper lists
compact_list(list(
  a = 1,
  NULL,
  b = list(NULL, list(1, 2, 3), list(list(x = 3, y = 4, NULL))),
  NULL
))

insight documentation built on June 29, 2026, 1:07 a.m.