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
Newbut the old report has a query with the sameIdentifier,Event,RepetitionandField(each of them when both reports have it;Descriptionwhen they don't haveField), 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.
Arguments
- old
Data frame containing the previous (older) query report. Must include
Identifier(text, factor or number),DescriptionandQuerycolumns (character or factor).- new
Data frame containing the newer query report. Must include
Identifier(text, factor or number),DescriptionandQuerycolumns (character or factor). Ifnewcontains aCodecolumn, 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";NAis not accepted.- return_viewer
Logical; if
TRUE(default) an HTML table (knitr/kable + kableExtra) summarizing the counts per state is produced and returned in theresultselement of the returned list. IfFALSE, no HTML viewer is produced (useful for non-interactive runs).
Value
A list of class REDCapDM_report, which prints a short summary (see print.REDCapDM_report()), with two elements:
queriesA data frame containing all queries present in either
oldornew. A factor columnModificationindicates the state for each row (levels:Pending,Solved,Miscorrected,New). The function also reassignsCodevalues so codes are consistent perIdentifier: 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).resultsIf
return_viewer = TRUE, an HTMLknitr::kable(styled withkableExtra) summarising totals per state. Ifreturn_viewer = FALSE, this isNULL.
Details
Requirements:
Both
oldandnewmust be data frames.Both data frames must contain at least the following columns:
Identifier(text, factor or number),Description, andQuery.Every query must have an
Identifier(the record identifier of the data).A
Codecolumn is optional; the codes ofoldare used to sort the queries, and the codes of the result are assigned again (see the value). IfCodeexists innew, 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.
Notes and edge cases
Column types: If
Identifier,DescriptionorQueryare factors, they will be used in the comparison — it is recommended to convert them to character prior to callingcheck_queries()to avoid factor-level mismatches.Sorting: The rows are sorted by
Identifierin natural order (e.g."100-2"before"100-10").Identifieris 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,EventandRepetition(when both reports have them),Field(when both reports have it; otherwiseDescription) andQuery.Eventis the unique event name andFieldthe name of the field: changing the display labels of events, arms or fields doesn't solve their queries. Their labels (EventLabelandDescription) and the other columns (e.g.LinkorDAG) are taken from the new report, so a query whose label or link changed is still one pending query.Reports created before
EventLabelexisted:rd_query()could store event labels inEventwhen factor labels were available. Reports fromrd_event()already stored unique event names there, but used labels in their defaultQuerytext. Recognised legacy labels and default missing-event query texts are normalised to unique event names. Shared labels cannot be resolved automatically.Miscorrected detection: A
Miscorrectedlabel is assigned to a query that would otherwise beNewwhen 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 sameFieldwhen both reports have it, or else the sameDescription. This signals a likely re-issued or modified query. New queries without such a query in the old report areNew, 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)
#> Identifier Description Query Code Modification
#> 1 100-1 age check is.na(age) 100-1-1 Pending
#> 2 100-2 weight check is.na(weight) 100-2-1 Solved
#> 3 200-1 lab miss missing lab result 200-1-1 Solved
#> 4 200-1 lab miss missing lab result (clarify) 200-1-2 Miscorrected
#> 5 300-1 new query is.na(x) 300-1-1 New
# HTML summary (if in an RMarkdown or interactive viewer)
res$results
#> <table class="table table-striped table-condensed" style="width: auto !important; margin-left: auto; margin-right: auto;">
#> <caption>My Query Comparison</caption>
#> <thead>
#> <tr>
#> <th style="text-align:center;border-bottom: 1px solid grey"> State </th>
#> <th style="text-align:center;border-bottom: 1px solid grey"> Total </th>
#> </tr>
#> </thead>
#> <tbody>
#> <tr>
#> <td style="text-align:center;"> Solved </td>
#> <td style="text-align:center;"> 2 </td>
#> </tr>
#> <tr>
#> <td style="text-align:center;"> Pending </td>
#> <td style="text-align:center;"> 1 </td>
#> </tr>
#> <tr>
#> <td style="text-align:center;"> Miscorrected </td>
#> <td style="text-align:center;"> 1 </td>
#> </tr>
#> <tr>
#> <td style="text-align:center;"> New </td>
#> <td style="text-align:center;"> 1 </td>
#> </tr>
#> </tbody>
#> </table>
