R/llookup.R

Defines functions llookup llookup_string llookup_all .llookup_backend

Documented in llookup llookup_all llookup_string

#
#   Install Package:           'Cmd + Shift + B'
#   Check Package:             'Cmd + Shift + E'
#   Test Package:              'Cmd + Shift + T'
#
#   Update documentation:       devtools::document()
#

# Fast implementation of variable and value labels lookup tables
# llookup / label lookup

.llookup_backend <- function(df) {

  get_varlabel <- function(x) {

    ifelse(
      has_label(df[[x]]),
      attr(df[[x]], "label"),
      NA_character_
    )

    }

  tibble::as_tibble(sapply(names(df), get_varlabel), rownames = "varname") %>%
    dplyr::rename(label = value)

}

#' Table of all variable names and variable labels.
llookup_all <- function(df,
                        print_max = Inf) {

  .llookup_backend(df) %>%
    print(n = print_max)

}

#' Table of variable names and variable labels that match the string.
llookup_string <- function(df,
                           string,
                           print_max = Inf) {

  # TODO: Implement case_igno
  # TODO: Highlight string

  .llookup_backend(df) %>%
    dplyr::filter(label %>% stringr::str_detect(string) | varname %>% stringr::str_detect(string)) %>%
    print(n = print_max)

}

#' Table of variable names and variable labels. All or match by string.
llookup <- function(df,
                    string = NULL,
                    print_max = Inf) {

  if (is.null(string)) {

    llookup_all(df, print_max = print_max)

  } else {

    llookup_string(df, string = string, print_max = print_max)

  }

}
adviceas/adviceverse documentation built on Jan. 9, 2021, 11:58 a.m.