/
plan.R
137 lines (127 loc) · 4.09 KB
/
plan.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Copyright (c) 2023 Merck & Co., Inc., Rahway, NJ, USA and its affiliates.
# All rights reserved.
#
# This file is part of the metalite program.
#
# metalite is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#' Create a analysis plan from all combination of variables
#'
#' This function is a wrapper of [base::expand.grid()].
#'
#' @param analysis A character value of analysis term name.
#' The term name is used as key to link information.
#' @param population A character value of population term name.
#' The term name is used as key to link information.
#' @param observation A character value of observation term name.
#' The term name is used as key to link information.
#' @param parameter A character value of parameter term name.
#' The term name is used as key to link information.
#' @param mock A numeric value of mock table number.
#' @param ... Additional arguments.
#'
#' @return A data frame containing the analysis plan.
#'
#' @export
#'
#' @examples
#' # Example 1
#' # Create an analysis plan of AE summary
#' # with any AE, drug-related AE, and serious AE
#' plan(
#' analysis = "ae_summary",
#' population = "apat",
#' observation = c("wk12", "wk24"),
#' parameter = "any;rel;ser"
#' )
#'
#' # Example 2
#' # Create an analysis plan of AE specific
#' # with any AE, drug-related AE, and serious AE
#' plan(
#' analysis = "ae_specific",
#' population = "apat",
#' observation = c("wk12", "wk24"),
#' parameter = c("any", "rel", "ser")
#' )
plan <- function(analysis, population, observation, parameter, mock = 1, ...) {
stopifnot(length(analysis) == 1)
x <- new_plan(analysis, population, observation, parameter, mock = mock, ...)
validate_plan(x)
}
#' Create a analysis plan from all combination of variables
#'
#' This function is a wrapper of [base::expand.grid()].
#'
#' @inheritParams plan
#'
#' @noRd
#'
#' @examples
#' metalite:::new_plan(
#' analysis = "ae_specific",
#' population = "apat",
#' observation = c("wk12", "wk24"),
#' parameter = c("any", "rel", "ser")
#' )
new_plan <- function(analysis, population, observation, parameter, mock = 1, ...) {
# create a data frame from all combinations of the supplied vectors or factors.
x <- expand.grid(
mock = mock,
analysis = analysis,
population = population,
observation = observation,
parameter = parameter,
...,
stringsAsFactors = FALSE # specifying if char vec are converted to factors
)
class(x) <- c("meta_plan", "data.frame")
x
}
#' Validate an analysis plan object
#'
#' @param plan A `meta_plan` object.
#'
#' @noRd
#'
#' @examples
#' x <- plan(analysis = "ae_summary", population = "apat", observation = "wk12", parameter = "any")
#' metalite:::validate_plan(x)
validate_plan <- function(plan) {
stopifnot(all(c("meta_plan", "data.frame") %in% class(plan)))
stopifnot(all(c("analysis", "population", "observation", "parameter") %in% names(plan)))
plan
}
#' Add additional analysis plan
#'
#' @param plan A `meta_plan` object.
#' @inheritParams plan
#'
#' @return A data frame containing analysis plans with new plans added.
#'
#' @export
#'
#' @examples
#' plan("ae_summary",
#' population = "apat",
#' observation = c("wk12", "wk24"), parameter = "any;rel"
#' ) |>
#' add_plan("ae_specific",
#' population = "apat",
#' observation = c("wk12", "wk24"), parameter = c("any", "rel")
#' )
add_plan <- function(plan, analysis, population, observation, parameter, ...) {
plan <- validate_plan(plan)
plan1 <- plan(analysis, population, observation, parameter, mock = max(plan$mock + 1), ...)
bind_rows2(plan, plan1)
}