|
| 1 | +# the provider-question registry ----------------------------------------------- |
| 2 | +# |
| 3 | +# Every dataset carries `metadata/{provider}/{dataset}/questions.csv`: what we |
| 4 | +# could not settle from the data, with the evidence that raised it. 17 files and |
| 5 | +# 136 questions accumulated four spellings of "done" (`open` / `answered` / |
| 6 | +# `resolved` / `wontfix`) and two of "normal" (`normal` / `medium`), because each |
| 7 | +# ingest notebook read the CSV with a bare `read_csv()` and sorted by its own |
| 8 | +# hand-written factor level vector — a status nobody's vector listed simply sorted |
| 9 | +# to the bottom and was never seen again. |
| 10 | +# |
| 11 | +# So the vocabulary lives here, in one validated read that every notebook calls. |
| 12 | + |
| 13 | +#' The controlled vocabulary of the question registry |
| 14 | +#' |
| 15 | +#' `status`: |
| 16 | +#' \describe{ |
| 17 | +#' \item{`open`}{asked, no answer and no proposal} |
| 18 | +#' \item{`proposed`}{**we have an answer to approve, not a problem to hand over** |
| 19 | +#' — `proposed_answer` holds what we did or suggest, and the provider is |
| 20 | +#' confirming it} |
| 21 | +#' \item{`answered`}{settled; `answer` holds the resolution} |
| 22 | +#' \item{`wontfix`}{closed without an answer, deliberately} |
| 23 | +#' } |
| 24 | +#' |
| 25 | +#' `priority`: `blocker` (the ingest cannot be released as-is), `high`, `normal`, |
| 26 | +#' `low`. |
| 27 | +#' |
| 28 | +#' @return Character vector of the allowed values. |
| 29 | +#' @export |
| 30 | +#' @concept registry |
| 31 | +#' @examples |
| 32 | +#' question_statuses() |
| 33 | +#' question_priorities() |
| 34 | +question_statuses <- function() c("open", "proposed", "answered", "wontfix") |
| 35 | + |
| 36 | +#' @rdname question_statuses |
| 37 | +#' @export |
| 38 | +question_priorities <- function() c("blocker", "high", "normal", "low") |
| 39 | + |
| 40 | +QUESTION_COLS <- c( |
| 41 | + "label", "id", "question", "context", "status", "priority", |
| 42 | + "proposed_answer", "answer", "asked_date", "answered_date", "who", |
| 43 | + "related_table", "related_field") |
| 44 | + |
| 45 | +#' Read a dataset's `questions.csv`, validated and ranked |
| 46 | +#' |
| 47 | +#' The single reader for the provider-question registry. Reads strictly |
| 48 | +#' (`na = ""`, everything character, so a date or an id like `01` is never |
| 49 | +#' silently retyped), checks the controlled vocabulary, and returns the questions |
| 50 | +#' ranked `blocker` → `low` then by `label`. |
| 51 | +#' |
| 52 | +#' Two identifiers, deliberately: |
| 53 | +#' * **`id`** — `{provider}_{dataset}_{nn}`, globally unique and durable. This is |
| 54 | +#' what a cross-dataset reference or an issue tracker cites. |
| 55 | +#' * **`label`** — the short form (`Q15`), unique *within* the dataset. This is |
| 56 | +#' what prose in a notebook says, and what the rendered table shows first, so |
| 57 | +#' "see Q15" resolves for a reader. |
| 58 | +#' |
| 59 | +#' @param path path to a `questions.csv` |
| 60 | +#' @param validate error on an unknown `status`/`priority`, a duplicate `label`, |
| 61 | +#' or a `label` that disagrees with `id` (default TRUE) |
| 62 | +#' |
| 63 | +#' @return A [tibble][tibble::tibble], all columns character, ranked. |
| 64 | +#' @export |
| 65 | +#' @concept registry |
| 66 | +#' @importFrom readr read_csv cols col_character |
| 67 | +#' @examples |
| 68 | +#' \dontrun{ |
| 69 | +#' read_questions("metadata/calcofi/ctd-cast/questions.csv") |
| 70 | +#' } |
| 71 | +read_questions <- function(path, validate = TRUE) { |
| 72 | + stopifnot("questions.csv not found" = file.exists(path)) |
| 73 | + # na = "" and all-character: an `asked_date` of "" must stay empty rather than |
| 74 | + # becoming the string "NA" on the next write (see R/registry.R) |
| 75 | + d <- readr::read_csv(path, na = "", show_col_types = FALSE, |
| 76 | + col_types = readr::cols(.default = readr::col_character())) |
| 77 | + |
| 78 | + miss <- setdiff(QUESTION_COLS, names(d)) |
| 79 | + if (length(miss)) |
| 80 | + stop("questions registry ", path, " is missing column(s): ", |
| 81 | + paste(miss, collapse = ", "), |
| 82 | + "\n Expected: ", paste(QUESTION_COLS, collapse = ", "), call. = FALSE) |
| 83 | + |
| 84 | + if (isTRUE(validate)) { |
| 85 | + check_registry_na_strings(d, path) |
| 86 | + |
| 87 | + bad <- setdiff(stats::na.omit(unique(d$status)), question_statuses()) |
| 88 | + if (length(bad)) |
| 89 | + stop("unknown question status in ", path, ": ", paste(bad, collapse = ", "), |
| 90 | + "\n Allowed: ", paste(question_statuses(), collapse = " | "), |
| 91 | + call. = FALSE) |
| 92 | + |
| 93 | + bad <- setdiff(stats::na.omit(unique(d$priority)), question_priorities()) |
| 94 | + if (length(bad)) |
| 95 | + stop("unknown question priority in ", path, ": ", paste(bad, collapse = ", "), |
| 96 | + "\n Allowed: ", paste(question_priorities(), collapse = " | "), |
| 97 | + call. = FALSE) |
| 98 | + |
| 99 | + dup <- unique(d$label[duplicated(d$label)]) |
| 100 | + if (length(dup)) |
| 101 | + stop("duplicate question label(s) in ", path, ": ", |
| 102 | + paste(dup, collapse = ", "), |
| 103 | + "\n `label` must be unique within a dataset — it is what prose cites.", |
| 104 | + call. = FALSE) |
| 105 | + |
| 106 | + # `label` is AUTHORED, not derived from `id`. For 16 of the 17 registries it |
| 107 | + # is mechanically `Q` + the id's numeric suffix, but `calcofi/hydro-master` |
| 108 | + # carries two id namespaces (`hydro_master_*` and `recon_*`) that would |
| 109 | + # collide on that rule, and its ids are cited by name in the protocol and the |
| 110 | + # CTD ingest — so the id stays and the label disambiguates (`Q01` / `QR01`). |
| 111 | + off <- which(is.na(d$label) | !grepl("^Q[0-9A-Za-z]+$", d$label)) |
| 112 | + if (length(off)) |
| 113 | + stop("malformed question label(s) in ", path, ": ", |
| 114 | + paste(sprintf("%s (id %s)", d$label[off], d$id[off]), collapse = "; "), |
| 115 | + "\n Expected the short display form, e.g. Q15.", call. = FALSE) |
| 116 | + } |
| 117 | + |
| 118 | + d[order(match(d$priority, question_priorities()), d$label), , drop = FALSE] |
| 119 | +} |
| 120 | + |
| 121 | +#' Render a question registry as the standard notebook table |
| 122 | +#' |
| 123 | +#' The `## Questions for Data Providers` section every ingest notebook ends with. |
| 124 | +#' One call so the 16 notebooks cannot show different columns in different orders |
| 125 | +#' — which they did, each with its own hand-written priority factor. |
| 126 | +#' |
| 127 | +#' Columns that are empty for every question are dropped, so a dataset with no |
| 128 | +#' answers yet does not render two blank columns. |
| 129 | +#' |
| 130 | +#' @param x a path to a `questions.csv`, or a data.frame from [read_questions()] |
| 131 | +#' @param caption table caption |
| 132 | +#' @param page_length rows per page |
| 133 | +#' |
| 134 | +#' @return A [DT::datatable()] htmlwidget. |
| 135 | +#' @export |
| 136 | +#' @concept registry |
| 137 | +#' @importFrom DT datatable |
| 138 | +#' @examples |
| 139 | +#' \dontrun{ |
| 140 | +#' questions_datatable(here::here(cc$questions_file)) |
| 141 | +#' } |
| 142 | +questions_datatable <- function(x, caption = "Questions for data providers (ranked)", |
| 143 | + page_length = 25) { |
| 144 | + d <- if (is.character(x)) read_questions(x) else x |
| 145 | + keep <- c("label", "priority", "status", "question", "context", |
| 146 | + "proposed_answer", "answer", "related_table", "related_field") |
| 147 | + keep <- intersect(keep, names(d)) |
| 148 | + d <- d[, keep, drop = FALSE] |
| 149 | + # an all-empty column is noise, not information |
| 150 | + d <- d[, vapply(d, function(v) any(!is.na(v) & nzchar(v)), logical(1)), drop = FALSE] |
| 151 | + |
| 152 | + DT::datatable( |
| 153 | + d, caption = caption, rownames = FALSE, |
| 154 | + options = list(pageLength = page_length, scrollX = TRUE, dom = "tip", |
| 155 | + columnDefs = list(list(width = "60px", targets = 0)))) |
| 156 | +} |
0 commit comments