R/utils.R

Defines functions clean_pkg_name

Documented in clean_pkg_name

#' Clean a directory name into a valid R package name
#'
#' Converts underscores and hyphens to dots and removes other invalid
#' characters. R package names may only contain letters, numbers, and dots,
#' must start with a letter, and must not end with a dot.
#'
#' @param name Character. The directory name to clean.
#' @return A valid R package name.
#' @keywords internal
clean_pkg_name <- function(name) {
  # Replace underscores and hyphens with dots
  pkg <- gsub("[_-]", ".", name)
  # Remove any remaining invalid characters (keep letters, numbers, dots)
  pkg <- gsub("[^a-zA-Z0-9.]", "", pkg)
  # Must start with a letter
  pkg <- sub("^[^a-zA-Z]+", "", pkg)
  # Must not end with a dot
  pkg <- sub("[.]+$", "", pkg)
  # If empty after cleaning, use a fallback

  if (nchar(pkg) == 0) {
    stop("Cannot derive a valid R package name from '", name,
      "'. Please use at least one letter in the project name.", call. = FALSE)
  }

  pkg
}

Try the SCIproj package in your browser

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

SCIproj documentation built on March 18, 2026, 5:07 p.m.