R/validator.R

Defines functions summary.psychdsish_validation print.psychdsish_validation validator

Documented in validator

#' Validate a project directory structure against psych-ds-ish requirements
#'
#' This function checks whether a project directory follows a defined set
#' of structural and file-placement rules. The layout is loosely inspired by the
#' [psych-DS](https://psych-ds.github.io/) specification, but is **not**
#' fully psych-DS compliant. It adds extra conventions for reproducibility
#' and common analysis workflows, including Quarto file templates and
#' a `.gitignore` file. It does not comply with psych-ds's .json requirement.
#'
#' @param project_root Character scalar. Path to the project root directory.
#'   Defaults to `"../"`. The path is normalized internally.
#' @param exclude_dirs Character vector of directory names whose contents are
#'   ignored, in addition to all hidden directories (those whose name starts
#'   with a dot, e.g., `.git/` and `.Rproj.user/`). Defaults to the
#'   directories generated by 'renv', 'packrat', 'Quarto', and 'Node.js'.
#'   Hidden files at the project root, such as `.gitignore`, are still
#'   checked.
#' @param strict Logical. If `TRUE`, throw an error when any check fails,
#'   listing the failed checks and how to fix them. Useful in continuous
#'   integration (e.g., GitHub Actions), where the error makes the job fail.
#'   Defaults to `FALSE`.
#'
#' @details
#' The project's layout is detected from its folders: multi-study "by study"
#' if there are `study_1/`, `study_2/`, etc. folders at the project root,
#' multi-study "by type" if there are such folders inside `code/`,
#' `data/raw/`, etc., and single-study otherwise (see
#' [create_project_skeleton()]). The checks below are applied to each study's
#' folders. In multi-study projects, `.qmd`, `.Rmd`, and `.R` files for
#' analyses that combine studies may also be placed in a root-level `code/`
#' folder (by study) or anywhere in `code/` (by type).
#'
#' The validator performs several categories of checks:
#' \itemize{
#'   \item **Required directories** — Ensures that expected top-level and
#'         nested folders exist (e.g., `code/`, `data/raw/`, `methods/`).
#'   \item **Required files** — Confirms presence of a `readme.md` and
#'         a license file (e.g., `LICENSE`).
#'   \item **Filetype-location constraints** — Ensures specific file types
#'         (e.g., `.qmd`, `.csv`, `.rds`, `.png`) only appear in approved
#'         subdirectories and not elsewhere in the project.
#'   \item **Special HTML rule** — All `.html` files must be under `code/`
#'         or `methods/`.
#'   \item **Hygiene checks** — Optional rules that discourage spaces in
#'         filenames, check for `.gitignore` presence, and prevent data
#'         files being stored under `code/`.
#'   \item **Reproducibility checks** — Raw data files have not been modified
#'         or deleted since they were first committed to git (including
#'         uncommitted changes); rendered `.html` files are newer than their
#'         `.qmd` (using git commit times for committed, unmodified files, and
#'         file modification times otherwise); `README.md` no longer contains
#'         the template placeholders written by [create_project_skeleton()];
#'         and R code in `.R` files and in `.qmd`/`.Rmd` code chunks contains
#'         no `setwd()` calls or absolute file paths (e.g., `"~/"`,
#'         `"/Users/"`, `"C:/"`). Comment lines are ignored.
#'   \item **Codebook checks** — Every data file under `data/` is documented,
#'         by a codebook named after it (e.g.,
#'         `study-1_stage-processed_data.csv` ->
#'         `study-1_stage-processed_codebook.csv`, any data file extension),
#'         a psych-DS sidecar (`study-1_stage-processed_data.json`), or its
#'         columns being listed in `dataset_description.json`. `.csv`/`.tsv`
#'         codebooks no longer contain the "TO BE COMPLETED MANUALLY"
#'         placeholders written by the codebook chunk in
#'         `code/processing.qmd`. Undocumented raw data is a warning rather
#'         than a failure.
#'   \item **psych-DS checks** (reported as `"WARN"`) — Data file names follow
#'         the psych-DS convention of `key-value` pairs ending in `_data`
#'         (leniently for raw data, which often cannot be renamed); data files
#'         are described in `dataset_description.json` rather than only in a
#'         `.csv`/`.xlsx` codebook (see [write_dataset_description()]); and
#'         every codebook or `.json` under `data/` can be matched to the data
#'         file it describes.
#' }
#'
#' Checks that cannot be run are reported as `"SKIP"` rather than `"PASS"`,
#' e.g., the raw data check when the project is not a git repository, or the
#' `.html` check when nothing has been rendered yet. Checks that are worth
#' acting on but are not failures, such as a project that works but is not
#' fully psych-DS compliant, are reported as `"WARN"`. Neither causes
#' `strict = TRUE` to fail.
#'
#' @return
#' A tibble of class `psychdsish_validation` with one row per check
#' (failures first, then warnings and skipped checks), containing:
#' \describe{
#'   \item{Test}{Description of the check performed.}
#'   \item{Status}{`"PASS"`, `"FAIL"`, `"WARN"` (worth fixing, but not a
#'         failure), or `"SKIP"` (the check could not be run).}
#'   \item{Details / Guidance}{Additional information, such as instructions
#'         for fixing failures or offending file paths.}
#' }
#' Printing it shows coloured PASS/FAIL results with guidance for failures.
#' `summary()` returns a list with `project_root`, `layout` (`"single"`,
#' `"by_study"`, or `"by_type"`), `studies` (the study folder names),
#' `n_pass`, `n_fail`, `n_warn`, `n_skip`, and `passed` (`TRUE` if no check
#' failed). If `strict = TRUE` and any check
#' fails, an error is thrown instead.
#'
#' @examples
#' project <- file.path(tempdir(), "my_project")
#' create_project_skeleton(project, quiet = TRUE)
#'
#' # Validate the project and print the results
#' results <- validator(project)
#' results
#'
#' # Overall result
#' summary(results)$passed
#'
#' # Throw an error if any check fails, e.g., to fail a GitHub Actions job
#' try(validator(project, strict = TRUE))
#'
#' # Display the results as a table, e.g., in a Quarto report
#' if (requireNamespace("knitr", quietly = TRUE)) {
#'   knitr::kable(results)
#' }
#'
#' unlink(project, recursive = TRUE)
#'
#' @export
validator <- function(
  project_root = "../",
  exclude_dirs = c("renv", "packrat", "_site", "_book", "_freeze", "node_modules"),
  strict = FALSE
) {
  # --- Configuration ---
  project_root <- fs::path_abs(project_root)
  all_paths <- fs::dir_ls(
    project_root,
    recurse = TRUE,
    type = "any",
    fail = FALSE,
    all = TRUE
  )

  # single study, or multi-study by study (study_1/code/) or by type
  # (code/study_1/): folders are mapped from their single-study path
  detected <- detect_layout(project_root)
  lay <- detected$layout
  studies <- detected$studies
  expand <- function(logicals) layout_expand(logicals, lay, studies)
  label <- function(logicals) {
    unique(vapply(logicals, layout_label, "", layout = lay))
  }

  # Ignore files generated by other tools: RStudio's .Rproj.user/, .git/,
  # .quarto/, renv/, and so on. Hidden files (e.g., .gitignore) are kept.
  keep_path <- function(paths) {
    rel <- as.character(fs::path_rel(paths, start = project_root))
    vapply(
      strsplit(rel, "/", fixed = TRUE),
      function(parts) {
        dirs <- if (length(parts) > 1) parts[-length(parts)] else character(0)
        last <- parts[length(parts)]
        hidden_dir <- any(startsWith(dirs, ".")) ||
          (startsWith(last, ".") && fs::is_dir(fs::path(project_root, paste(parts, collapse = "/"))))
        !hidden_dir && !any(c(dirs, last) %in% exclude_dirs)
      },
      logical(1)
    )
  }
  all_paths <- all_paths[keep_path(all_paths)]

  # --- Helpers ---
  mk_test <- function(test, passed, details = "") {
    tibble::tibble(
      test = test,
      status = ifelse(passed, "PASS", "FAIL"),
      details = details
    )
  }

  mk_skip <- function(test, details) {
    tibble::tibble(test = test, status = "SKIP", details = details)
  }

  # WARN: worth fixing, but not a failure (e.g., a project that works but is
  # not fully psych-DS compliant). Does not affect `strict`.
  mk_warn <- function(test, passed, details = "") {
    tibble::tibble(
      test = test,
      status = ifelse(passed, "PASS", "WARN"),
      details = details
    )
  }

  exists_ci <- function(relpath) {
    parts <- fs::path_split(relpath)[[1]]
    cur <- project_root
    for (p in parts) {
      cand <- fs::dir_ls(
        cur,
        type = "any",
        recurse = FALSE,
        fail = FALSE,
        all = TRUE
      )
      hit <- cand[tolower(fs::path_file(cand)) == tolower(p)]
      if (length(hit) == 0) {
        return(FALSE)
      }
      cur <- hit[[1]]
    }
    TRUE
  }

  list_ext <- function(ext) {
    ix <- tolower(fs::path_ext(all_paths)) == tolower(ext)
    all_paths[ix & fs::is_file(all_paths)]
  }

  starts_with_any <- function(paths, allowed_dirs_rel) {
    allowed_abs <- fs::path_abs(fs::path(project_root, allowed_dirs_rel))
    purrr::map_lgl(paths, function(p) {
      pd <- fs::path_dir(p)
      any(startsWith(pd, allowed_abs))
    })
  }

  results <- tibble::tibble(
    test = character(),
    status = character(),
    details = character()
  )

  # --- 1) Required directories (must exist) ---
  required_dirs <- setdiff(
    layout_all_dirs(lay, if (lay == "single") "study_1" else studies),
    "tools"
  )
  for (d in required_dirs) {
    exists_dir <- fs::dir_exists(fs::path(project_root, d))
    results <- dplyr::bind_rows(
      results,
      mk_test(
        paste0("Directory exists: ", d),
        exists_dir,
        if (!exists_dir) "Create this directory." else ""
      )
    )
  }

  # --- 2) Required files (must exist) ---
  readme_ok <- exists_ci("readme.md")
  license_any_ok <- exists_ci("licence") ||
    exists_ci("license") ||
    exists_ci("LICENSE")

  results <- dplyr::bind_rows(
    results,
    mk_test(
      'File exists: readme.md',
      readme_ok,
      if (!readme_ok) "Add a README (readme.md)." else ""
    ),
    mk_test(
      'File exists: licence/license',
      license_any_ok,
      if (!license_any_ok) "Add a licence file (prefer `LICENSE`)." else ""
    )
  )

  license_paths <- all_paths[
    stringr::str_to_lower(fs::path_file(all_paths)) %in%
      c(
        "licence",
        "license",
        "license.txt",
        "licence.txt",
        "license.md",
        "licence.md",
        "license.rst",
        "licence.rst",
        "license"
      )
  ]
  if (length(license_paths) > 0) {
    canonical <- any(fs::path_file(license_paths) == "LICENSE")
    results <- dplyr::bind_rows(
      results,
      mk_test(
        "License name is canonical (LICENSE)",
        canonical,
        if (!canonical) {
          "Consider renaming to `LICENSE` (all caps, no extension)."
        } else {
          ""
        }
      )
    )
  }

  # --- 3) Filetype-location constraints ---
  constraints <- list(
    qmd = list(
      must_be_in = c("code", "tools"),
      must_exist_in_each = FALSE,
      forbidden_elsewhere = TRUE
    ),
    Rmd = list(
      must_be_in = c("code", "tools"),
      must_exist_in_each = FALSE,
      forbidden_elsewhere = TRUE
    ),
    R = list(
      must_be_in = c("code", "tools"),
      must_exist_in_each = FALSE,
      forbidden_elsewhere = TRUE
    ),
    #html  = list(must_be_in = c("code", "methods"), must_exist_in_each = FALSE, forbidden_elsewhere = TRUE),
    csv = list(
      must_be_in = c("data"),
      must_exist_in_each = FALSE,
      forbidden_elsewhere = TRUE
    ),
    xlsx = list(
      must_be_in = c("data"),
      must_exist_in_each = FALSE,
      forbidden_elsewhere = TRUE
    ),
    rds = list(
      must_be_in = c("data"),
      must_exist_in_each = FALSE,
      forbidden_elsewhere = TRUE
    ),
    tsv = list(
      must_be_in = c("data"),
      must_exist_in_each = FALSE,
      forbidden_elsewhere = TRUE
    ),
    dta = list(
      must_be_in = c("data"),
      must_exist_in_each = FALSE,
      forbidden_elsewhere = TRUE
    ),
    pdf = list(
      must_be_in = c(
        "data/outputs",
        "data/raw",
        "methods",
        "reports",
        "preregistration"
      ),
      must_exist_in_each = FALSE,
      forbidden_elsewhere = TRUE
    ),
    feather = list(
      must_be_in = c("data"),
      must_exist_in_each = FALSE,
      forbidden_elsewhere = TRUE
    ),
    sav = list(
      must_be_in = c("data"),
      must_exist_in_each = FALSE,
      forbidden_elsewhere = TRUE
    ),
    png = list(
      must_be_in = c("data/outputs/plots", "data/raw"),
      must_exist_in_each = FALSE,
      forbidden_elsewhere = TRUE
    ),
    docx = list(
      must_be_in = c(
        "reports",
        "methods",
        "preregistration",
        "data/outputs/results",
        "data/raw"
      ),
      must_exist_in_each = FALSE,
      forbidden_elsewhere = TRUE
    )
  )

  # HTML restriction: only under code/ or methods/
  html_files <- list_ext("html")
  if (length(html_files)) {
    allowed_dirs <- expand(c("code", "methods"))
    html_ok <- all(starts_with_any(html_files, allowed_dirs))
    offenders <- character(0)
    if (!html_ok) {
      offenders <- fs::path_rel(
        html_files[!starts_with_any(html_files, allowed_dirs)],
        start = project_root
      )
    }
    results <- dplyr::bind_rows(
      results,
      mk_test(
        paste0(
          "All .html files are under ",
          paste(label(c("code", "methods")), collapse = " or ")
        ),
        html_ok,
        if (!html_ok) {
          paste0(
            ".html files must be under code/ (if output of processing or analysis code) or methods/ (if part of the method delivery). Offenders: ",
            paste(offenders, collapse = "; ")
          )
        } else {
          ""
        }
      )
    )
  }

  check_constraint <- function(rule, ext) {
    files <- list_ext(ext)
    shown <- label(rule$must_be_in)
    rule$must_be_in <- expand(rule$must_be_in)

    # 3a) Required presence (if requested)
    if (isTRUE(rule$must_exist_in_each) && length(rule$must_be_in) > 0) {
      for (dir_req in rule$must_be_in) {
        under_dir <- files[starts_with_any(files, dir_req)]
        results <<- dplyr::bind_rows(
          results,
          mk_test(
            paste0("At least one .", ext, " in ", dir_req),
            length(under_dir) > 0,
            if (length(under_dir) == 0) {
              paste0("Add a .", ext, " file under ", dir_req, ".")
            } else {
              ""
            }
          )
        )
      }
    }

    # 3b) Forbidden elsewhere
    if (isTRUE(rule$forbidden_elsewhere)) {
      if (length(files)) {
        allowed_ok <- if (length(rule$must_be_in) == 0) {
          length(files) == 0
        } else {
          all(starts_with_any(files, rule$must_be_in))
        }
        offenders <- character(0)
        if (!allowed_ok) {
          allowed_mask <- starts_with_any(files, rule$must_be_in)
          offenders <- fs::path_rel(files[!allowed_mask], start = project_root)
        }
        results <<- dplyr::bind_rows(
          results,
          mk_test(
            paste0(
              "All .",
              ext,
              if (length(rule$must_be_in)) {
                paste0(
                  " files reside in ",
                  paste(shown, collapse = ", ")
                )
              } else {
                " files are absent"
              }
            ),
            allowed_ok,
            if (!allowed_ok) {
              paste0("Move/remove: ", paste(offenders, collapse = "; "))
            } else {
              ""
            }
          )
        )
      } else if (isFALSE(rule$must_exist_in_each)) {
        results <<- dplyr::bind_rows(
          results,
          mk_test(
            paste0("No .", ext, " files present (as expected)"),
            TRUE,
            ""
          )
        )
      }
    }
  }

  purrr::iwalk(constraints, check_constraint)

  # --- 4) Optional hygiene checks ---
  enable_hygiene <- TRUE

  if (enable_hygiene) {
    # No data-like files under code/ at all
    data_exts <- c(
      "csv",
      "xlsx",
      "tsv",
      "sav",
      "dta",
      "parquet",
      "feather",
      "rds"
    )
    under_code <- all_paths[
      starts_with_any(all_paths, expand("code")) & fs::is_file(all_paths)
    ]
    offenders <- under_code[tolower(fs::path_ext(under_code)) %in% data_exts]
    results <- dplyr::bind_rows(
      results,
      mk_test(
        paste0("No data files stored under ", paste(label("code"), collapse = " or ")),
        length(offenders) == 0,
        if (length(offenders)) {
          paste0(
            "Move: ",
            paste(
              fs::path_rel(offenders, start = project_root),
              collapse = "; "
            )
          )
        } else {
          ""
        }
      )
    )

    # .gitignore presence
    has_gitignore <- exists_ci(".gitignore")
    results <- dplyr::bind_rows(
      results,
      mk_test(
        "Has .gitignore",
        has_gitignore,
        if (!has_gitignore) {
          "Add a .gitignore (ignore .Rhistory, .RData, .Rproj.user, cache, large tmp files)."
        } else {
          ""
        }
      )
    )

    # Filenames: discourage spaces
    bad_names <- fs::path_file(all_paths[
      fs::is_file(all_paths) &
        stringr::str_detect(fs::path_file(all_paths), "\\s")
    ])
    results <- dplyr::bind_rows(
      results,
      mk_test(
        "No spaces in filenames",
        length(bad_names) == 0,
        if (length(bad_names)) {
          paste0("Rename: ", paste(bad_names, collapse = "; "))
        } else {
          ""
        }
      )
    )
  }

  # --- 5) Reproducibility checks ---
  rel <- function(p) as.character(fs::path_rel(p, start = project_root))

  # git helpers: all commands run from project_root and fail quietly
  git <- function(...) {
    out <- tryCatch(
      suppressWarnings(system2(
        "git",
        c("-C", shQuote(project_root), ...),
        stdout = TRUE,
        stderr = FALSE
      )),
      error = function(e) character(0)
    )
    if (!is.null(attr(out, "status"))) character(0) else out
  }
  in_git <- nzchar(Sys.which("git")) &&
    identical(git("rev-parse", "--is-inside-work-tree"), "true")
  git_prefix <- if (in_git) git("rev-parse", "--show-prefix") else character(0)
  git_prefix <- if (length(git_prefix)) git_prefix else ""
  # `git status --porcelain` paths are relative to the repository root
  strip_prefix <- function(paths) {
    ifelse(
      startsWith(paths, git_prefix),
      substring(paths, nchar(git_prefix) + 1),
      paths
    )
  }

  # Raw data unchanged since first committed
  raw_test <- "Raw data unchanged since first committed (git)"
  raw_dirs <- expand("data/raw")
  raw_dirs <- raw_dirs[fs::dir_exists(fs::path(project_root, raw_dirs))]
  if (length(raw_dirs) == 0) {
    results <- dplyr::bind_rows(
      results,
      mk_skip(raw_test, paste0("No ", label("data/raw"), "/ directory."))
    )
  } else if (!in_git) {
    results <- dplyr::bind_rows(
      results,
      mk_skip(
        raw_test,
        paste0(
          "Not a git repository (or git is not installed), so the history of ",
          label("data/raw"),
          "/ cannot be checked."
        )
      )
    )
  } else {
    # files modified (M) or deleted (D) in any commit after they were added
    changed_committed <- git(
      "log",
      "--diff-filter=MD",
      "--name-only",
      "--format=",
      "--relative",
      "--",
      shQuote(raw_dirs)
    )
    # uncommitted modifications or deletions of tracked files
    porcelain <- git("status", "--porcelain", "--", shQuote(raw_dirs))
    xy <- substr(porcelain, 1, 2)
    changed_uncommitted <- strip_prefix(
      substring(porcelain[grepl("[MD]", xy)], 4)
    )
    changed <- unique(c(changed_committed, changed_uncommitted))
    changed <- sort(changed[nzchar(changed) & basename(changed) != ".gitkeep"])
    results <- dplyr::bind_rows(
      results,
      mk_test(
        raw_test,
        length(changed) == 0,
        if (length(changed)) {
          paste0(
            "Modified or deleted since first committed: ",
            paste(changed, collapse = "; "),
            ". Raw data should be read-only: make changes in code instead, and restore the originals from git. ",
            "If the change was to remove private data, this is expected, but the private data are still in the git history ",
            "(and on GitHub, if pushed) and must be purged separately, e.g., with git filter-repo or BFG Repo-Cleaner."
          )
        } else {
          ""
        }
      )
    )
  }

  # Rendered .html files are newer than their .qmd
  html_test <- "Rendered .html files are up to date with their .qmd"
  qmds <- list_ext("qmd")
  htmls <- fs::path_ext_set(qmds, "html")
  has_html <- fs::file_exists(htmls)
  qmds <- qmds[has_html]
  htmls <- htmls[has_html]
  if (length(qmds) == 0) {
    results <- dplyr::bind_rows(
      results,
      mk_skip(html_test, "No rendered .html files found next to .qmd files.")
    )
  } else {
    # last commit time for committed, unmodified files; otherwise modification
    # time (which, after a fresh clone, only reflects the checkout)
    file_time <- function(f) {
      if (in_git && length(git("status", "--porcelain", "--", shQuote(f))) == 0) {
        ct <- git("log", "-1", "--format=%ct", "--", shQuote(f))
        if (length(ct) == 1 && nzchar(ct)) {
          return(as.numeric(ct))
        }
      }
      as.numeric(fs::file_info(f)$modification_time)
    }
    stale <- qmds[purrr::map2_lgl(qmds, htmls, function(q, h) {
      file_time(q) > file_time(h) + 1
    })]
    results <- dplyr::bind_rows(
      results,
      mk_test(
        html_test,
        length(stale) == 0,
        if (length(stale)) {
          paste0(
            "Changed since last rendered: ",
            paste(rel(stale), collapse = "; "),
            ". Re-render them (see the README's Reproducibility section)."
          )
        } else {
          ""
        }
      )
    )
  }

  # README customised
  readme_test <- "README has been customised (no template placeholders)"
  readme_file <- all_paths[
    fs::path_dir(all_paths) == project_root &
      tolower(fs::path_file(all_paths)) == "readme.md"
  ]
  if (length(readme_file) == 0) {
    results <- dplyr::bind_rows(
      results,
      mk_skip(readme_test, "No README.md found.")
    )
  } else {
    readme_lines <- readLines(readme_file[1], warn = FALSE)
    placeholders <- c(
      "# Project Title",
      "Add aims, data sources, and reproduction steps.",
      "Authors (Year). Title. URL."
    )
    found <- placeholders[purrr::map_lgl(placeholders, function(ph) {
      any(trimws(readme_lines) == ph)
    })]
    results <- dplyr::bind_rows(
      results,
      mk_test(
        readme_test,
        length(found) == 0,
        if (length(found)) {
          paste0(
            "Replace the template text in README.md: ",
            paste(paste0("'", found, "'"), collapse = "; ")
          )
        } else {
          ""
        }
      )
    )
  }

  # No setwd() or absolute paths in code
  code_files <- c(list_ext("R"), list_ext("qmd"), list_ext("Rmd"))
  code_files <- code_files[
    !grepl("(^|/)(\\.[^/]+|renv)/", rel(code_files))
  ]
  code_lines <- purrr::map_dfr(code_files, function(f) {
    lines <- readLines(f, warn = FALSE)
    keep <- rep(TRUE, length(lines))
    if (tolower(fs::path_ext(f)) %in% c("qmd", "rmd")) {
      # only lines inside R code chunks
      in_chunk <- FALSE
      for (i in seq_along(lines)) {
        if (!in_chunk && grepl("^\\s*```+\\s*\\{r", lines[i])) {
          in_chunk <- TRUE
          keep[i] <- FALSE
        } else if (in_chunk && grepl("^\\s*```+\\s*$", lines[i])) {
          in_chunk <- FALSE
          keep[i] <- FALSE
        } else {
          keep[i] <- in_chunk
        }
      }
    }
    # ignore comment lines (including #| chunk options)
    keep <- keep & !grepl("^\\s*#", lines)
    tibble::tibble(
      where = paste0(rel(f), ":", seq_along(lines))[keep],
      code = lines[keep]
    )
  })
  if (nrow(code_lines) == 0) {
    code_lines <- tibble::tibble(where = character(), code = character())
  }
  setwd_hits <- code_lines$where[grepl("\\bsetwd\\s*\\(", code_lines$code)]
  abs_pattern <- paste0(
    "[\"'](",
    "~[/\\\\]", # home directory
    "|/(Users|home|Volumes|mnt|media|opt|srv|tmp|var|private)/", # Unix
    "|[A-Za-z]:[/\\\\]", # Windows drive
    ")"
  )
  abs_hits <- code_lines$where[grepl(abs_pattern, code_lines$code)]
  results <- dplyr::bind_rows(
    results,
    mk_test(
      "No setwd() calls in code",
      length(setwd_hits) == 0,
      if (length(setwd_hits)) {
        paste0(
          "Remove setwd() from: ",
          paste(setwd_hits, collapse = "; "),
          ". Each .qmd runs from its own folder, so use relative paths (e.g., ../data/raw/)."
        )
      } else {
        ""
      }
    ),
    mk_test(
      "No absolute file paths in code",
      length(abs_hits) == 0,
      if (length(abs_hits)) {
        paste0(
          "Replace absolute paths with relative ones (e.g., ../data/raw/) in: ",
          paste(abs_hits, collapse = "; ")
        )
      } else {
        ""
      }
    )
  )

  # Codebooks, psych-DS file names, and psych-DS metadata ------------------
  data_exts <- c(
    "csv",
    "tsv",
    "xlsx",
    "sav",
    "dta",
    "parquet",
    "feather",
    "rds"
  )
  # psych-DS: "[key-value]_[key-value]_data.csv"; keys are lowercase letters,
  # values alphanumeric, and the name ends in "_data"
  psychds_pattern <- "^[a-z]+-[A-Za-z0-9]+(_[a-z]+-[A-Za-z0-9]+)*_data$"

  files_under <- function(logical) {
    dirs <- fs::path(project_root, expand(logical))
    dirs <- dirs[fs::dir_exists(dirs)]
    out <- unlist(lapply(dirs, function(d) {
      as.character(fs::dir_ls(d, type = "file", recurse = TRUE))
    }))
    if (is.null(out)) character(0) else out
  }
  stem <- function(p) fs::path_ext_remove(fs::path_file(p))
  is_codebook <- function(p) grepl("codebook", tolower(fs::path_file(p)))

  raw_files <- files_under("data/raw")
  processed_files <- files_under("data/processed")
  all_data_files <- c(raw_files, processed_files)
  all_data_files <- all_data_files[
    tolower(fs::path_ext(all_data_files)) %in% data_exts
  ]
  data_files <- all_data_files[!is_codebook(all_data_files)]
  codebook_files <- all_data_files[is_codebook(all_data_files)]

  # the documentation that belongs to a data file: "<stem>_codebook.<ext>" in
  # the same folder, or the psych-DS sidecar "<stem>.json"
  # data/ lives in a different place in each layout, and need not exist
  json_dirs <- fs::path(project_root, expand("data"))
  json_dirs <- json_dirs[fs::dir_exists(json_dirs)]
  json_files <- unlist(lapply(json_dirs, function(d) {
    as.character(fs::dir_ls(d, type = "file", recurse = TRUE))
  }))
  json_files <- if (is.null(json_files)) {
    character(0)
  } else {
    json_files[tolower(fs::path_ext(json_files)) == "json"]
  }
  expected_codebook <- function(p) {
    fs::path(fs::path_dir(p), paste0(sub("_data$", "", stem(p)), "_codebook"))
  }
  sidecar_of <- function(p) fs::path(fs::path_dir(p), paste0(stem(p), ".json"))

  # variables listed in the root dataset_description.json
  dd_path <- fs::path(project_root, "dataset_description.json")
  dd_variables <- character(0)
  if (fs::file_exists(dd_path)) {
    dd <- tryCatch(
      jsonlite::read_json(dd_path, simplifyVector = FALSE),
      error = function(e) NULL
    )
    dd_variables <- vapply(
      dd$variableMeasured %||% list(),
      function(v) if (is.list(v)) as.character(v$name %||% "") else as.character(v),
      ""
    )
  }
  # a .csv data file is covered when all of its columns are in that list
  covered_by_json <- function(p) {
    if (tolower(fs::path_ext(p)) != "csv" || length(dd_variables) == 0) {
      return(FALSE)
    }
    header <- tryCatch(
      names(utils::read.csv(p, nrows = 1, check.names = FALSE)),
      error = function(e) NULL
    )
    !is.null(header) && all(header %in% dd_variables)
  }

  codebook_test <- "Every data file has a codebook"
  raw_codebook_test <- "Every raw data file has a codebook"
  json_test <- "Data files are documented in dataset_description.json (psych-DS)"
  names_test <- "Processed data file names follow the psych-DS convention"
  raw_names_test <- "Raw data file names follow the psych-DS convention"
  ambiguous_test <- "Codebooks are named after the data file they describe"
  codebook_done_test <- "Codebooks are completed (no 'TO BE COMPLETED MANUALLY')"

  if (length(data_files) == 0) {
    results <- dplyr::bind_rows(
      results,
      mk_skip(codebook_test, "No data files under data/."),
      mk_skip(json_test, "No data files under data/."),
      mk_skip(names_test, "No data files under data/."),
      mk_skip(codebook_done_test, "No data files under data/.")
    )
  } else {
    documented <- function(p) {
      any(startsWith(codebook_files, as.character(expected_codebook(p)))) ||
        as.character(sidecar_of(p)) %in% json_files ||
        covered_by_json(p)
    }
    has_doc <- vapply(data_files, documented, logical(1))
    is_raw <- data_files %in% raw_files

    # processed data must be documented; raw data is a warning, as it often
    # arrives with names and contents that cannot be changed
    undocumented <- data_files[!has_doc & !is_raw]
    results <- dplyr::bind_rows(
      results,
      mk_test(
        codebook_test,
        length(undocumented) == 0,
        if (length(undocumented)) {
          paste0(
            "Document: ",
            paste(rel(undocumented), collapse = "; "),
            ". Add '<name>_codebook.csv' (or .xlsx) next to the data file, ",
            "or describe it in dataset_description.json. ",
            "The processing.qmd template contains a chunk that creates a codebook."
          )
        } else {
          ""
        }
      )
    )
    undocumented_raw <- data_files[!has_doc & is_raw]
    if (any(is_raw)) {
      results <- dplyr::bind_rows(
        results,
        mk_warn(
          raw_codebook_test,
          length(undocumented_raw) == 0,
          if (length(undocumented_raw)) {
            paste0(
              "Undocumented: ",
              paste(rel(undocumented_raw), collapse = "; "),
              ". Raw data should have a codebook too, e.g., '<name>_codebook.csv' ",
              "or the codebook supplied with the data."
            )
          } else {
            ""
          }
        )
      )
    }

    # psych-DS wants the metadata in JSON, not a .csv/.xlsx codebook
    csv_only <- data_files[
      has_doc & !vapply(data_files, covered_by_json, logical(1)) &
        !as.character(vapply(data_files, function(p) as.character(sidecar_of(p)), "")) %in% json_files
    ]
    results <- dplyr::bind_rows(
      results,
      mk_warn(
        json_test,
        length(csv_only) == 0,
        if (length(csv_only)) {
          paste0(
            "Documented only by a codebook file, which is not fully psych-DS compliant: ",
            paste(rel(csv_only), collapse = "; "),
            ". A .csv codebook is fine for psychdsish, but psych-DS expects the variables in ",
            "dataset_description.json: run psychdsish::write_dataset_description() to write it ",
            "from the codebooks."
          )
        } else {
          ""
        }
      )
    )

    # file names: strict for the files the project's own code writes
    bad_names <- data_files[!is_raw & !grepl(psychds_pattern, stem(data_files))]
    results <- dplyr::bind_rows(
      results,
      mk_warn(
        names_test,
        length(bad_names) == 0,
        if (length(bad_names)) {
          paste0(
            "Rename to pairs of 'key-value' separated by underscores, ending in '_data', ",
            "e.g., 'study-1_stage-processed_data.csv': ",
            paste(rel(bad_names), collapse = "; ")
          )
        } else {
          ""
        }
      )
    )
    if (any(is_raw)) {
      bad_raw <- data_files[is_raw & !grepl(psychds_pattern, stem(data_files))]
      results <- dplyr::bind_rows(
        results,
        mk_warn(
          raw_names_test,
          length(bad_raw) == 0,
          if (length(bad_raw)) {
            paste0(
              "These do not follow the psych-DS convention (e.g., ",
              "'study-1_stage-raw_data.csv'): ",
              paste(rel(bad_raw), collapse = "; "),
              ". Rename them only if the raw data have not been shared or committed yet; ",
              "otherwise leave them as they are and note the names in the codebook."
            )
          } else {
            ""
          }
        )
      )
    }

    # codebooks and .json files that do not belong to any data file
    expected_codebooks <- as.character(vapply(
      data_files,
      function(p) as.character(expected_codebook(p)),
      ""
    ))
    orphan_codebooks <- codebook_files[
      !vapply(
        codebook_files,
        function(p) any(startsWith(p, expected_codebooks)),
        logical(1)
      )
    ]
    orphan_json <- json_files[
      fs::path_file(json_files) != "directory_metadata.json" &
        !json_files %in%
          as.character(vapply(data_files, function(p) as.character(sidecar_of(p)), ""))
    ]
    orphans <- c(orphan_codebooks, orphan_json)
    results <- dplyr::bind_rows(
      results,
      mk_warn(
        ambiguous_test,
        length(orphans) == 0,
        if (length(orphans)) {
          paste0(
            "It is not clear which data file these document: ",
            paste(rel(orphans), collapse = "; "),
            ". Name a codebook after its data file with '_codebook' appended ",
            "(e.g., study-1_stage-processed_data.csv -> ",
            "study-1_stage-processed_codebook.csv), or, for a psych-DS sidecar, ",
            "give the .json the data file's name (study-1_stage-processed_data.json)."
          )
        } else {
          ""
        }
      )
    )

    # manual columns still containing the template placeholder (.csv/.tsv only)
    text_codebooks <- codebook_files[
      tolower(fs::path_ext(codebook_files)) %in% c("csv", "tsv")
    ]
    if (length(text_codebooks) == 0) {
      results <- dplyr::bind_rows(
        results,
        mk_skip(
          codebook_done_test,
          "No .csv or .tsv codebooks to check (other formats are not read)."
        )
      )
    } else {
      incomplete <- purrr::map_chr(text_codebooks, function(f) {
        sep <- if (tolower(fs::path_ext(f)) == "tsv") "\t" else ","
        cb <- tryCatch(
          utils::read.csv(f, sep = sep, colClasses = "character"),
          error = function(e) NULL
        )
        if (is.null(cb)) {
          return(paste0(rel(f), " (could not be read)"))
        }
        todo <- vapply(
          cb,
          function(col) sum(trimws(col) == "TO BE COMPLETED MANUALLY", na.rm = TRUE),
          integer(1)
        )
        todo <- todo[todo > 0]
        if (length(todo) == 0) {
          return(NA_character_)
        }
        paste0(
          rel(f),
          " (",
          paste(paste0(names(todo), ": ", todo), collapse = ", "),
          ")"
        )
      })
      incomplete <- incomplete[!is.na(incomplete)]
      results <- dplyr::bind_rows(
        results,
        mk_test(
          codebook_done_test,
          length(incomplete) == 0,
          if (length(incomplete)) {
            paste0(
              "Replace 'TO BE COMPLETED MANUALLY' (write 'none' where a column does not apply) in: ",
              paste(incomplete, collapse = "; ")
            )
          } else {
            ""
          }
        )
      )
    }
  }

  # --- 6) Present results ---
  results <- results |>
    dplyr::mutate(
      status = factor(status, levels = c("FAIL", "WARN", "SKIP", "PASS"))
    ) |>
    dplyr::arrange(status, test)

  n_fail <- sum(results$status == "FAIL")
  n_pass <- sum(results$status == "PASS")

  res <- results |>
    dplyr::mutate(details = ifelse(details == "", "-", details)) |>
    dplyr::rename(
      `Test` = test,
      `Status` = status,
      `Details / Guidance` = details
    )

  attr(res, "project_root") <- as.character(project_root)
  attr(res, "layout") <- lay
  attr(res, "studies") <- studies
  class(res) <- c("psychdsish_validation", class(res))

  if (strict && n_fail > 0) {
    failed <- res[res$Status == "FAIL", ]
    cli::cli_abort(c(
      "{n_fail} of {n_pass + n_fail} psych-DS-ish check{?s} failed in {.path {project_root}}.",
      stats::setNames(
        paste0(failed$Test, ": ", failed$`Details / Guidance`),
        rep("x", nrow(failed))
      )
    ))
  }

  res
}

#' @export
print.psychdsish_validation <- function(x, ...) {
  needed <- c("Test", "Status", "Details / Guidance")
  # fall back to the default tibble print if the structure has been modified
  if (!all(needed %in% names(x))) {
    return(NextMethod())
  }
  smry <- summary(x)
  line <- function(...) cat(..., "\n", sep = "")

  line(cli::rule(left = "psych-DS-ish validation"))
  if (!is.null(smry$project_root)) {
    line(cli::col_grey(smry$project_root))
  }
  if (!is.null(smry$layout)) {
    line(cli::col_grey(paste0(
      "Layout: ",
      layout_labels[[smry$layout]],
      if (length(smry$studies)) paste0(", ", length(smry$studies), " studies")
    )))
  }
  for (i in seq_len(nrow(x))) {
    if (x$Status[i] == "FAIL") {
      line(cli::col_red(paste(cli::symbol$cross, "FAIL")), " ", x$Test[i])
      details <- x$`Details / Guidance`[i]
      if (!is.na(details) && details != "-") {
        line("       ", cli::col_grey(details))
      }
    } else if (x$Status[i] == "WARN") {
      line(cli::col_yellow(paste(cli::symbol$warning, "WARN")), " ", x$Test[i])
      details <- x$`Details / Guidance`[i]
      if (!is.na(details) && details != "-") {
        line("       ", cli::col_grey(details))
      }
    } else if (x$Status[i] == "SKIP") {
      line(cli::col_yellow(paste(cli::symbol$circle, "SKIP")), " ", x$Test[i])
      line("       ", cli::col_grey(x$`Details / Guidance`[i]))
    } else {
      line(cli::col_green(paste(cli::symbol$tick, "PASS")), " ", x$Test[i])
    }
  }
  line(cli::rule())
  extra <- c(
    if (smry$n_warn > 0) sprintf("%d warning%s", smry$n_warn, if (smry$n_warn > 1) "s" else ""),
    if (smry$n_skip > 0) sprintf("%d skipped", smry$n_skip)
  )
  skipped <- if (length(extra)) paste0(" (", paste(extra, collapse = ", "), ")") else ""
  if (smry$passed) {
    line(cli::col_green(sprintf("All %d checks passed.", smry$n_pass)), skipped)
  } else {
    line(
      cli::col_red(sprintf(
        "%d of %d checks failed.",
        smry$n_fail,
        smry$n_pass + smry$n_fail
      )),
      skipped
    )
  }
  invisible(x)
}

#' @export
summary.psychdsish_validation <- function(object, ...) {
  n_fail <- sum(object$Status == "FAIL")
  list(
    project_root = attr(object, "project_root"),
    layout = attr(object, "layout"),
    studies = attr(object, "studies"),
    n_pass = sum(object$Status == "PASS"),
    n_fail = n_fail,
    n_warn = sum(object$Status == "WARN"),
    n_skip = sum(object$Status == "SKIP"),
    passed = n_fail == 0
  )
}

Try the psychdsish package in your browser

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

psychdsish documentation built on Nov. 29, 2026, 5:07 p.m.