From 6ab60e9dbae28d1133f2da16d94d80a81a21ac2c Mon Sep 17 00:00:00 2001 From: Brancen Gregory Date: Sat, 15 Apr 2023 16:16:58 -0400 Subject: [PATCH] Draft of anonymizer function --- R/ojo_anonymize.R | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 R/ojo_anonymize.R diff --git a/R/ojo_anonymize.R b/R/ojo_anonymize.R new file mode 100644 index 0000000..78d3102 --- /dev/null +++ b/R/ojo_anonymize.R @@ -0,0 +1,31 @@ +#' @title Anonymize +#' +#' @description Anonymize one or more columns in a local tibble +#' +#' @param .data A local tibble +#' @param cols A list of columns to anonymize +#' @param ... Placeholder for future arguments +#' @param .keep A character string passed to the `keep` argument of `dplyr::mutate()`. One of "unused" (default), "all", "used", or "none". +#' @param .after Optionally, control where new columns should appear (the default is to replace the source columns with their anonymized version). See `dplyr::mutate()` for more details. +#' +#' @export +#' @return A local tibble with anonymized columns, with names prefixed with "xx_" +#' +#' @importFrom dplyr everything mutate across +#' @importFrom purrr map_chr +#' @importFrom digest digest +#' +ojo_anonymize <- function(.data, cols = dplyr::everything(), ..., .keep = "unused", .after = NULL) { + if (is.null(.after)) { + .after <- {{cols}} + } + + df <- .data |> + dplyr::mutate( + dplyr::across({{cols}}, ~purrr::map_chr(., ~digest::digest(., algo = "xxhash64")), .names = "xx_{.col}"), + .keep = .keep, + .after = .after + ) + + return(df) +}