Overview
This article describes how to extend gsm.core by
creating new “modules”, including metrics, reports and shiny apps that
can be run using the standard gsm pipeline described in
these articles: Data
Analysis and DataReporting.
The existing gsm data pipeline can be used to capture a
monitoring ‘snapshot’ for a study that includes a variety of “modules”
including metrics
and reports.
This article provides detailed specifications for creating new modules, a description of the directory structure for the yaml workflows that comprise a module pipeline, and links to resources that can be used to configure study-level gsm pipelines that utilize these extensions.
Module Configuration
gsm modules will typically be part of an R package and
have a YAML configuration file - {module_name}.yaml - saved
under inst/workflow/<n>_<stage>/
(e.g. 2_metrics for metrics, 4_modules for
reports and apps). Module config files include 3 key properties:
-
meta: (required) Report metadata used for centralized library and fields referenced instepssection of workflow. -
spec: (optional, recommended) Data table requirement for the report. Typically fromMapped_*orReporting_*data. -
steps: (required) Functions to produce the reporting output fromMapped_*and/orReporting_*data to final output (html,csvorshinyApp).
Detailed specifications for each of these sections are provided below.
meta Specification
The meta section of a workflow YAML provides key
metadata describing the module. It must include the following
fields:
-
Name: Name of the reporting output. -
Type: The workflow type, e.g. “Mapped”, “Analysis” (metrics), “Reporting” or “Report”. Together withID, it forms the result name ({Type}_{ID}, e.g.Analysis_kri0001) returned byworkr::RunWorkflows(). -
ID: The unique ID for the module. This should be the same as the workflow file name, without extension. -
Description: A one-line description of the module specified in the workflow. -
Active: optional A boolean (true/false) indicating whether the workflow is active. Workflows withActive: falseare filtered out of workflow lists. If missing ortrue, the workflow is included. -
Priority: optional A number specifying the priority of the workflow within the workflow list, with lower numbers having higher priority, and running first. This is used when workflows within the same directory require outputs from other workflows, and thus need to be run in a particular order. If missing, will be set to 0 to run first. -
Details: optional A more detailed description of the module specified in the workflow. -
Repo: optional Package repo and version. Should be compatible with therepoparameter inremotes::install_github(). -
Status: optional The validation status of the reporting output. Valid values:-
Qualified: Output has been qualified via our qualification process specified here. -
Pilot: Output is being used by pilot studies and is maintained in a package repository. -
Prototype: Output is created using custom scripts on an ad-hoc basis.
-
Additional meta header fields for
Modules:
-
Permission: optional Level of permissions for viewing that match theUsersargument in the Study Configuration file. Common values:Admin,Users. -
Output: The output of the workflow, including format. Each workflow should only produce a single reporting output. -
ExampleURL: optional Location of a sample report. For html reports, this is typically a page on the pkgdown site (ending with “/{ModuleID}.html”), or a sample app deployed on shinyapps.io.
Additional meta header required fields for
{gsm.kri} metrics:
-
GroupLevel: The level at which the metric is calculated. Common values:Site,Country. -
Abbreviation: Abbreviation of the metric. -
Metric: Full name of the metric. -
Numerator: The numerator of the metric. -
Denominator: The denominator of the metric. -
AnalysisType: The analysis type. In normal approximation workflows it is passed to thestrTypeparameter ofgsm.core::Analyze_NormalApprox()(accepted values:rate,binary). For other models, such asidentity, it is workflow and reporting metadata only and is not passed to the analysis function (Analyze_Identity()has nostrTypeparameter). Common values:rate,binary,identity -
Model: The model used to calculate the metric. Common values:Normal Approximation,Identity(other models, such asPoissonandFisher, are available in gsm.core). -
Score: The score used to calculate the metric. Common values:Z-Score,Adjusted Z-Score,P-Value. -
Threshold: The thresholds to flag, as a comma-separated string. Parsed withgsm.core::ParseThreshold()and passed to thevThresholdparameter ofgsm.core::Flag(). -
Flag: optional The numeric values assigned to each flag, as a comma-separated string. Parsed withgsm.core::ParseThreshold(bSort = FALSE)and passed to thevFlagparameter ofgsm.core::Flag(). -
RiskScoreWeight: optional The risk score weight assigned to each flag value, as a comma-separated string. Parsed withgsm.core::ParseThreshold(bSort = FALSE)and passed to thevRiskScoreWeightparameter ofgsm.core::Flag(). -
GenerateRiskSignal: optional A boolean (true/false) indicating whether a risk signal should be generated when this metric produces a flag. If missing ortrue, a risk signal is generated. Only applicable to metric workflows. We use this flag in our internal systems, and provide it for you to integrate metrics into your own workflows. -
AccrualThreshold: The minimum threshold needed to apply a flag. -
AccrualMetric: The metric on which to apply the minimum threshold needed to apply a flag. Accepted values:Denominator,Numerator,Difference.
meta example
A simple meta section for a report might look like
this:
# File: report_example.yaml
meta:
ID: report_example
Name: An example report
Description: A report that is an example
Type: Report
Repo: gsm.example v1.0.0
Status: Qualified
Permission: Users
Output: html
ExampleURL: <pkgdown site URL>/report_example.html
spec Specification
The spec section of the workflow YAML specifies the data
requirements for the workflow, including the data tables that are
required and the columns that are needed from each table. The
gsm data pipeline is designed to be highly customizable,
but for the purposes of this article, we will assume usage of the
standard gsm data model described in the Data
Model article. For this standard use case, modules will pull
primarily from the “Mapped” and “Reporting” data layers.
The spec section of the workflow YAML is formatted as a
list of data tables, with each table containing a list of columns.
Finally, each column contains the following parameters:
-
type: Character field describing the data type of the column. Use the expected R class (e.g.character,integer,numeric,Date;timestampforPOSIXct). This parameter is optional. -
source_col: Character field containing the column name from the source data frame. Only necessary when source column name differs from target column name.
spec example: Metric Module
Metric specs are typically pulled from the
mapped data layer. For example, the spec
section for the AE
KRI metric is:
spec:
Mapped_AE:
subjid:
type: character
Mapped_SUBJ:
subjid:
type: character
invid:
type: character
timeonstudy:
type: integer
So, in summary, the AE KRI metric requires two data tables,
Mapped_AE and Mapped_SUBJ, both of which are
from the mapped data layer. The Mapped_AE table must have a
character Subject ID column called subjid while
Mapped_SUBJ must have character Subject ID
(subjid) and Investigator ID (invid) columns
and an integer timeonstudy column. All columns are required
for this metric. Note that other columns may be present in these tables
(perhaps due to a spec from a different module), but only
the columns listed in the spec section are required for the
metric to run.
For the nested GSM spec format shown above,
workr::RunWorkflow() checks that each listed data table is
present in lData, but does not check the nested column
names or their types. workr also supports column-name lists
under a table’s required_cols, required or
columns field; those lists are checked for missing columns,
but not column types. Column- and type-level checks for the nested GSM
format can be run with gsm.core::CheckSpec(), or applied
when ingesting raw data with gsm.mapping::ApplySpec().
spec examples: Report Module
Report modules most often pull data from the Reporting
data layer. For example, the Site-level
KRI report has the following entry in its spec
(excerpt):
spec:
Reporting_Results:
_all:
required: true
Note that the _all key word is used to specify that all
standard columns from the Reporting_Results data table are
expected and that the table is required - without it, the report can’t
run. The full spec of the report lists
Reporting_Metrics, Reporting_Groups and
Reporting_Bounds in the same way, as all four tables are
needed to produce the report (see the full example in the
/4_modules section below).
The Mapped data layer is also available for use in
reports and apps. Most typically, mapped data is used to drill down from
high-level metric findings (e.g. “Site 5 has an elevated AE rate
relative to other sites”) to site- or participant- level details
(e.g. “Participant 00016 from Site 5 had 5 AEs and 3 SAEs reported in
the last 3 months.”). For example, the Deep Dive app includes
both Reporting and Mapped data in its spec. Here is a
representative excerpt from the spec:
spec:
Reporting_Results:
_all:
required: true
Mapped_AE:
subjid:
type: character
aeterm:
type: character
aesev:
type: character
...
steps Specification
Finally, each module yaml configuration file should have a
steps property that describes in detail how the module is
run. The steps section is a list of functions that are run
in sequence to produce the final output. Each item in steps
has the following properties:
-
name: The name of the function to be run. This is typically a function that is available in one of the{gsm}packages. Use thepackage::functionform (e.g.gsm.core::Flag) for functions from packages that are not attached; theRepofield of themetaheader is not used to load packages. -
output: The name of the output of the function. This is the name of the data table that is created by the function. -
params: A list of parameters that are passed to the function. The parameters are specific to the function that is being run. See below for more details on how to specify parameters for each function.
Note: It is important to note that the default
behavior of workr::RunWorkflow() and
workr::RunWorkflows() is to return the last output
in the steps section of the workflow. Therefore, each yaml file,
regardless of which directory it is in, should only produce one output,
whether that be a data table, list, html output, deployed shiny app, or
any other object needed to produce the module output.
The steps section is the most complex part of the module
configuration and will vary greatly depending on the module type and the
specific requirements of the module. workr provides the
standard workflow-runtime functions used to execute module yaml files,
while gsm.core provides many of the analytical functions
that those steps call. See ?workr::RunWorkflow() for more
details.
steps[]$params Specification
After processing the YAML meta and spec
sections, workr::RunWorkflow() calls
workr::RunStep() for each step in the steps
section of the YAML. The params section of each step is
passed to workr::RunStep() as a list of parameters along
with a copy of the metadata header (lMeta), any data
(lData) and the spec (lSpec).
workr::RunStep() then parses each parameter value
(paramVal) in the following order:
- If
paramValislMeta,lDataorlSpec, the full object is passed. - If
paramValmatches a field name inlMeta, that field’s value is passed (e.g.strGroupLevel: GroupLevelpasseslMeta$GroupLevel). - If
paramValmatches an object name inlData, that object is passed (e.g.dfInput: Analysis_InputpasseslData$Analysis_Input). - If
paramValis anexpr()/exprs()call, it is parsed into an R expression. - Otherwise,
paramValis passed as a string (e.g.strGroupCol: invid). Vectors are passed as they are.
Because lMeta and lData are checked first,
a string parameter that happens to match a meta field or a
data object name will be replaced by that value. Finally, the parsed
parameters are passed to the function specified in the name
field of the step.
steps examples
Metric steps example
In the example below, the steps to produce the AE analysis output is
specified. Here, Threshold, Flag,
RiskScoreWeight, GroupLevel,
AnalysisType, AccrualThreshold and
AccrualMetric are specified in the meta
section of the workflow (see kri0001.yaml),
and are accessed via the paramVal process discussed above.
Note that AnalysisType must be rate here,
since gsm.core::Analyze_NormalApprox() defaults to
strType = "binary". As a default, the output of these steps
as run with workr::RunWorkflow() would be a list of data
tables, as specified in the final list step of the
workflow. The element names of this list
(e.g. Analysis_Summary) are used by downstream reporting
workflows, such as gsm.reporting::BindResults(), and should
not be changed.
steps:
- name: gsm.core::ParseThreshold
output: vThreshold
params:
strThreshold: Threshold
- name: gsm.core::ParseThreshold
output: vRiskScoreWeight
params:
strThreshold: RiskScoreWeight
bSort: false
- name: gsm.core::ParseThreshold
output: vFlag
params:
strThreshold: Flag
bSort: false
- name: gsm.core::Input_Rate
output: Analysis_Input
params:
dfSubjects: Mapped_SUBJ
dfNumerator: Mapped_AE
dfDenominator: Mapped_SUBJ
strSubjectCol: subjid
strGroupCol: invid
strGroupLevel: GroupLevel
strNumeratorMethod: Count
strDenominatorMethod: Sum
strDenominatorCol: timeonstudy
- name: gsm.core::Transform_Rate
output: Analysis_Transformed
params:
dfInput: Analysis_Input
- name: gsm.core::Analyze_NormalApprox
output: Analysis_Analyzed
params:
dfTransformed: Analysis_Transformed
strType: AnalysisType
- name: gsm.core::Flag
output: Analysis_Flagged
params:
dfAnalyzed: Analysis_Analyzed
vThreshold: vThreshold
vFlag: vFlag
vRiskScoreWeight: vRiskScoreWeight
nAccrualThreshold: AccrualThreshold
strAccrualMetric: AccrualMetric
- name: gsm.core::Summarize
output: Analysis_Summary
params:
dfFlagged: Analysis_Flagged
- name: list
output: lAnalysis
params:
ID: ID
Analysis_Input: Analysis_Input
Analysis_Transformed: Analysis_Transformed
Analysis_Analyzed: Analysis_Analyzed
Analysis_Flagged: Analysis_Flagged
Analysis_Summary: Analysis_Summary
Report steps example
In this example, the steps to produce a site-level KRI report is
displayed. Here, the only inputs are the Reporting_* data,
which goes through a simple filtering process via
workr::RunQuery before the Charts and Report are created in
the following two functions
steps:
- name: workr::RunQuery
output: Reporting_Results_Site
params:
df: Reporting_Results
strQuery: "SELECT * FROM df WHERE GroupLevel == 'Site'"
- name: workr::RunQuery
output: Reporting_Metrics_Site
params:
df: Reporting_Metrics
strQuery: "SELECT * FROM df WHERE GroupLevel == 'Site'"
- name: gsm.kri::MakeCharts
output: lCharts_Site
params:
dfResults: Reporting_Results_Site
dfGroups: Reporting_Groups
dfBounds: Reporting_Bounds
dfMetrics: Reporting_Metrics_Site
- name: gsm.kri::Report_KRI
output: lReport
params:
lCharts: lCharts_Site
dfResults: Reporting_Results_Site
dfGroups: Reporting_Groups
dfMetrics: Reporting_Metrics_Site
Directory Structure for Workflows
Each extension that produces report(s) will have a
workflow directory in the inst of the package
that follows a standard structure. The standard gsm pipeline uses 4
folders in which to store the yaml workflow files that map data
(1_mappings, in {gsm.mapping}), perform
analysis (2_metrics, e.g. in {gsm.kri}),
produce reporting data (3_reporting, in
{gsm.reporting}), and generate the output of a module
(4_modules, e.g. in {gsm.kri}). An extension
only needs to include the folders for the stages it provides. Each
module output requires its own unique yaml in the 4_modules
folder, which will take inputs generated from the previous three
stages.
/1_mappings
The mappings folder in the {gsm.mapping} package
contains all of the mappings from Raw_* data to
Mapped_* data. Each file within this directory is to be
named for the data table it is creating, minus the Mapped_
prefix (e.g. AE.yaml creates Mapped_AE). The
yamls will contain the three required sections, which are discussed in
detail in the Module Configuration section above. The yamls
in this folder will be combined via
gsm.mapping::CombineSpecs() to create a master spec that
defines all of the Raw_* tables and columns required by the
mappings.
Below are two examples of these mapping yaml files- the first which requires no transformations, and is very simple, and the second which requires multiple steps to produce the desired mapped data.
Mapped_AE mapping yaml file
meta:
Type: Mapped
ID: AE
Description: Adverse Event Data Mapping
Priority: 1
spec:
Raw_AE:
subjid:
type: character
aeser:
type: character
steps:
- output: Mapped_AE
name: =
params:
lhs: Mapped_AE
rhs: Raw_AE
Mapped_DATACHG mapping yaml file
meta:
Type: Mapped
ID: DATACHG
Description: Data Changes Data Mapping
Priority: 2
spec:
Raw_DATACHG:
subject_nsv:
type: character
source_col: subjectname
n_changes:
type: integer
Mapped_SUBJ:
subjid:
type: character
subject_nsv:
type: character
steps:
# Merge [ subjid ] onto EDC domains.
- output: Temp_SubjectLookup
name: dplyr::select
params:
.data: Mapped_SUBJ
subjid: subjid
subject_nsv: subject_nsv
- output: Mapped_DATACHG
name: dplyr::left_join
params:
x: Raw_DATACHG
"y": Temp_SubjectLookup
by: subject_nsv
/2_metrics
The metrics directory contains all of the workflows that perform
analysis steps, converting mapped data into metrics that are displayed
in a report. In the case of {gsm.kri}, these metrics are
the standard Key Risk Indicators, calculated at both the site- and
country-level, whose analysis functions are discussed in the Data
Analysis Step-by-Step article. Each yaml in this folder produces a list
of analysis data tables that capture the formatted input table, the
transformed table, the analyzed table, the flagged table, and the
summary table. In general, these yamls should at least provide a summary
table that contains statistics about the metric at the specified level
of aggregation.
Examples of these yamls can be found above in the
Module Configuration section, and the functions they call
are walked through step-by-step in the Data
Analysis article.
/3_reporting
The reporting directory is intended to hold all of the workflows that
produce the data that is required for the module outputs. This typically
requires the stacking of analysis data from all of the relevant metrics
into a single results data frame that can be surfaced in a
report, or multiple reports. Additionally, any further information that
must be taken from the analysis output, such as study/site/group/metric
metadata and supporting statistics will be constructed through workflows
in this folder.
Examples of these yamls can be found in the 3_reporting
folder of {gsm.reporting}, and the functions they call
are walked through step-by-step in the Data
Reporting article.
/4_modules
The modules directory contains the final workflow(s) of the reporting
pipeline. These workflows each produce a single output based on the data
tables that have been produced in the previous directories. These module
workflows will contain all of the necessary meta information, as
detailed in the Module Configuration section above, along
with the data tables required, and steps to produce it, so that
workr::RunWorkflow() can take this workflow and produce the
module output.
Below is an example of the module yaml workflow for the KRI Site
Report in {gsm.kri}
meta:
Type: Report
ID: report_kri_site
Output: html
Name: Site-Level Key Risk Indicator Report
Description: A report summarizing key risk indicators at the site level
Active: true
spec:
Reporting_Results:
_all:
required: true
Reporting_Metrics:
_all:
required: true
Reporting_Groups:
_all:
required: true
Reporting_Bounds:
_all:
required: true
steps:
- name: workr::RunQuery
output: Reporting_Results_Site
params:
df: Reporting_Results
strQuery: "SELECT * FROM df WHERE GroupLevel == 'Site'"
- name: workr::RunQuery
output: Reporting_Metrics_Site
params:
df: Reporting_Metrics
strQuery: "SELECT * FROM df WHERE GroupLevel == 'Site'"
- name: gsm.kri::MakeCharts
output: lCharts_Site
params:
dfResults: Reporting_Results_Site
dfGroups: Reporting_Groups
dfBounds: Reporting_Bounds
dfMetrics: Reporting_Metrics_Site
- name: gsm.kri::Report_KRI
output: lReport
params:
lCharts: lCharts_Site
dfResults: Reporting_Results_Site
dfGroups: Reporting_Groups
dfMetrics: Reporting_Metrics_Site