R/delete_timing_vars.R

#' @title Delete Timing and Randomisation Variables
#' @description Remove from dataset \code{ds} unnecessary randomisation and timing variables created
#' by Gryphon. These variables are identified as any with aliases ending in "_rnd" or "_timing".
#' This is a simple wrapper around \code{delete_var_pattern}.
#' @param ds A CrunchDataset
#' @param confirm A boolean - if TRUE, user will be prompted to confirm the deletion in the console.
#' Default is FALSE.
#' @return Invisibly returns the original dataset.
#' @seealso \code{\link{delete_var_pattern}}
#' @export
delete_timing_vars <- function(ds, confirm = FALSE) {
  ds <- delete_var_pattern(ds, "(_rnd|_timing)$", confirm)
  return(invisible(ds))
}

#' @title Delete Variables of Alias Matching Pattern
#' @description Remove from dataset \code{ds} and variables whose alias match regex \code{pattern}.
#' @param ds A CrunchDataset
#' @param pattern A character regular expression matching the variable aliases to delete.
#' @param confirm A boolean - if TRUE, user will be prompted to confirm the deletion in the console.
#' @return Invisibly returns the original dataset.
#' @export
delete_var_pattern <- function(ds, pattern, confirm = FALSE) {
  if (confirm) {
    deleteVariables(ds, str_subset(aliases(allVariables(ds)), pattern))
  } else {
    with_consent(deleteVariables(ds, str_subset(aliases(allVariables(ds)), pattern)))
  }
  return(invisible(ds))
}
domjarkey/crunchscripts documentation built on June 6, 2019, 7:43 p.m.