R/sanitize_identifiers.R

Defines functions sanitize_identifiers

sanitize_identifiers <- function(x,
                                 regex = "[^a-zA-Z0-9_]",
                                 warningMsg = "Found (and removed) illegal characters in code identifiers %s. They were changed to %s.") {

  if (is.null(x)) {
    return(x);
  }

  if (all(is.na(x))) {
    return(x);
  }

  x <- trimws(x);

  sanitizedIds <-
    gsub(regex, "", x);

  illegalIds <- (x != sanitizedIds);

  if (any(illegalIds, na.rm=TRUE)) {
    warning(
      sprintf(
        message,
        vecTxtQ(x[illegalIds]),
        vecTxtQ(sanitizedIds[illegalIds])
      )
    );
  }

  return(sanitizedIds);

}

Try the preregr package in your browser

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

preregr documentation built on May 31, 2023, 7:10 p.m.