check_queries: Check for Changes Between Two Query Reports

View source: R/check_queries.R

check_queriesR Documentation

Check for Changes Between Two Query Reports

Description

[Stable]

Compare an older query report (old) with a newer one (new) and classify each query into one of four statuses:

  • Pending — the same query is present in both reports (no change detected),

  • Solved — the query was present in the old report but is absent from the new report,

  • New — the query appears in the new report but was not present in the old report,

  • Miscorrected — a special case where a query in the new report is marked as New but the old report has a query with the same Identifier, Event, Repetition and Field (each of them when both reports have it; Description when they don't have Field), suggesting a re-issued or modified query for the same record and field.

The function returns a detailed comparison table of queries with a Modification factor (one of the four statuses) and an HTML summary table showing counts per status.

Usage

check_queries(old, new, report_title = NULL, return_viewer = TRUE)

Arguments

old

Data frame containing the previous (older) query report. Must include Identifier (text, factor or number), Description and Query columns (character or factor).

new

Data frame containing the newer query report. Must include Identifier (text, factor or number), Description and Query columns (character or factor). If new contains a Code column, it will be removed at the start of processing.

report_title

Optional single string used as the caption for the HTML summary table. With NULL (the default), the caption is "Comparison report"; NA is not accepted.

return_viewer

Logical; if TRUE (default) an HTML table (knitr/kable + kableExtra) summarizing the counts per state is produced and returned in the results element of the returned list. If FALSE, no HTML viewer is produced (useful for non-interactive runs).

Details

Requirements:

  • Both old and new must be data frames.

  • Both data frames must contain at least the following columns: Identifier (text, factor or number), Description, and Query.

  • Every query must have an Identifier (the record identifier of the data).

  • A Code column is optional; the codes of old are used to sort the queries, and the codes of the result are assigned again (see the value). If Code exists in new, it is removed at the beginning of the routine to avoid conflicts with re-assigned codes.

The function merges the two reports, constructs composite keys used for comparison, classifies each row into a modification state, detects and re-labels Miscorrected cases, reassigns a Code per Identifier to keep codes consistent, and returns a detailed dataset plus an optional HTML summary viewer.

Value

A list of class REDCapDM_report, which prints a short summary (see print.REDCapDM_report()), with two elements:

queries

A data frame containing all queries present in either old or new. A factor column Modification indicates the state for each row (levels: Pending, Solved, Miscorrected, New). The function also reassigns Code values so codes are consistent per Identifier: a code is the number of each query of its record in this result, not a permanent identifier of the query (the same query can have another code in another comparison).

results

If return_viewer = TRUE, an HTML knitr::kable (styled with kableExtra) summarising totals per state. If return_viewer = FALSE, this is NULL.

Notes and edge cases

  • Column types: If Identifier, Description or Query are factors, they will be used in the comparison — it is recommended to convert them to character prior to calling check_queries() to avoid factor-level mismatches.

  • Sorting: The rows are sorted by Identifier in natural order (e.g. "100-2" before "100-10"). Identifier is returned as a number only when every identifier is a plain number without leading zeros (e.g. "007" is kept as text).

  • Identity of a query: a query is the same in both reports when it has the same Identifier, Event and Repetition (when both reports have them), Field (when both reports have it; otherwise Description) and Query. Event is the unique event name and Field the name of the field: changing the display labels of events, arms or fields doesn't solve their queries. Their labels (EventLabel and Description) and the other columns (e.g. Link or DAG) are taken from the new report, so a query whose label or link changed is still one pending query.

  • Reports created before EventLabel existed: rd_query() could store event labels in Event when factor labels were available. Reports from rd_event() already stored unique event names there, but used labels in their default Query text. Recognised legacy labels and default missing-event query texts are normalised to unique event names. Shared labels cannot be resolved automatically.

  • Miscorrected detection: A Miscorrected label is assigned to a query that would otherwise be New when the old report has a query of the same record (Identifier), event (Event) and repetition (Repetition), when both reports have these columns, and of the same variable: the same Field when both reports have it, or else the same Description. This signals a likely re-issued or modified query. New queries without such a query in the old report are New, also when there are several of them for the same field.

See Also

Other query functions: rd_event(), rd_export(), rd_query()

Examples

# Minimal reproducible example
old <- data.frame(
  Identifier = c("100-1", "100-2", "200-1"),
  Description = c("age check", "weight check", "lab miss"),
  Query = c("is.na(age)", "is.na(weight)", "missing lab result"),
  Code = c("100-1-1", "100-2-1", "200-1-1"),
  stringsAsFactors = FALSE
)

new <- data.frame(
  Identifier = c("100-1", "200-1", "300-1"),
  Description = c("age check", "lab miss", "new query"),
  Query = c("is.na(age)", "missing lab result (clarify)", "is.na(x)"),
  stringsAsFactors = FALSE
)

res <- check_queries(old = old, new = new, report_title = "My Query Comparison")
# detailed table
head(res$queries)
# HTML summary (if in an RMarkdown or interactive viewer)
res$results


REDCapDM documentation built on Nov. 27, 2026, 9:06 a.m.