R/remove_nameless_levels.R

Defines functions remove_nameless_levels

remove_nameless_levels <- function(x,
                                   newNames = "id",
                                   errorOnMissingNewName = TRUE) {
  if (!is.list(x)) {
    ### If x is not a list, just return it
    return(x);
  } else {
    if (!is.null(names(x))) {
      ### If x has names, pass all lists in x on and collect and return the results
      indicesOfLists <-
        which(lapply(x,
                     is.list));
      if (length(indicesOfLists) == 0) {
        ### If all elements of x are atomic, since x has names, we can just
        ### return x; nothing more to do here.
        return(x);
      } else {
        x[indicesOfLists] <-
          lapply(x[indicesOfLists],
                 remove_nameless_levels,
                 newNames = newNames,
                 errorOnMissingNewName = errorOnMissingNewName);
        return(x);
      }
    } else {
      ### If x does not have names, but is a list, check whether its elements
      ### all have at least one field with a name in newNames, and collect
      ### those, prioritizing based on the order provided in newNames; then
      ### name each element for its id
    }
  }
}

Try the justifier package in your browser

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

justifier documentation built on March 7, 2023, 6:59 p.m.