R/lapply_depth.R

Defines functions lapply_depth

Documented in lapply_depth

#' lapply at some list dept
#'
#' Very simple replacement for purrr::map_depth to remove package dependency,
#' but not very robust.  Internal package use only in select cases.
#'
#' @param .x [list] List to apply function to
#' @param .depth [integer] Depth to apply function at
#' @param .f [function] Function to apply
#' @param ... [any] Additional arguments to pass to .f
#'
#' @return [list] List with function applied at target depth
lapply_depth <- function(.x, .depth, .f, ...) {
   assert_named_list(.x)
    if (.depth == 0) {
      return(.f(.x, ...))  # Apply function directly if at target depth
   }
   if (!is.list(.x)) {
      return(.x)  # If .x is not a list, return as-is (handles mixed structures)
   }
   lapply(.x, function(elem) lapply_depth(elem, .depth - 1, .f, ...))
}

Try the vmTools package in your browser

Any scripts or data that you put into this service are public.

vmTools documentation built on Aug. 8, 2025, 7:28 p.m.