Skip to content

Commit 2f789e6

Browse files
committed
Style code (GHA)
1 parent 81f5dbc commit 2f789e6

File tree

6 files changed

+197
-184
lines changed

6 files changed

+197
-184
lines changed

R/medications.R

Lines changed: 136 additions & 128 deletions
Large diffs are not rendered by default.

R/mockdata-generators.R

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@
5757
#'
5858
#' @export
5959
create_cat_var <- function(var_raw, cycle, variable_details, variables = NULL,
60-
length, df_mock, prop_NA = NULL, seed = 100) {
61-
60+
length, df_mock, prop_NA = NULL, seed = 100) {
6261
# Level 1: Get variable details for this raw variable + cycle
6362
var_details <- get_variable_details_for_raw(var_raw, cycle, variable_details, variables)
6463

@@ -106,7 +105,6 @@ create_cat_var <- function(var_raw, cycle, variable_details, variables = NULL,
106105
new = sample(labels, length, replace = TRUE),
107106
stringsAsFactors = FALSE
108107
)
109-
110108
} else {
111109
# Case with NA values using NA codes
112110
set.seed(seed)
@@ -192,9 +190,8 @@ create_cat_var <- function(var_raw, cycle, variable_details, variables = NULL,
192190
#'
193191
#' @export
194192
create_con_var <- function(var_raw, cycle, variable_details, variables = NULL,
195-
length, df_mock, prop_NA = NULL, seed = 100,
196-
distribution = "uniform") {
197-
193+
length, df_mock, prop_NA = NULL, seed = 100,
194+
distribution = "uniform") {
198195
# Level 1: Get variable details for this raw variable + cycle
199196
var_details <- get_variable_details_for_raw(var_raw, cycle, variable_details, variables)
200197

@@ -261,7 +258,7 @@ create_con_var <- function(var_raw, cycle, variable_details, variables = NULL,
261258

262259
# Handle infinity
263260
if (is.infinite(overall_min)) overall_min <- 0
264-
if (is.infinite(overall_max)) overall_max <- overall_min + 100 # Arbitrary upper bound
261+
if (is.infinite(overall_max)) overall_max <- overall_min + 100 # Arbitrary upper bound
265262

266263
# Level 2: Extract NA codes (if prop_NA specified)
267264
na_labels <- NULL
@@ -285,13 +282,12 @@ create_con_var <- function(var_raw, cycle, variable_details, variables = NULL,
285282
if (distribution == "normal") {
286283
# Normal distribution centered at midpoint
287284
midpoint <- (overall_min + overall_max) / 2
288-
spread <- (overall_max - overall_min) / 4 # Use 1/4 of range as SD
285+
spread <- (overall_max - overall_min) / 4 # Use 1/4 of range as SD
289286

290287
values <- rnorm(n_regular, mean = midpoint, sd = spread)
291288

292289
# Clip to range
293290
values <- pmax(overall_min, pmin(overall_max, values))
294-
295291
} else {
296292
# Uniform distribution (default)
297293
values <- runif(n_regular, min = overall_min, max = overall_max)

R/mockdata-helpers.R

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@
5454
#'
5555
#' # Exclude derived variables
5656
#' cycle1_original <- get_cycle_variables("cycle1", variables, variable_details,
57-
#' include_derived = FALSE)
57+
#' include_derived = FALSE
58+
#' )
5859
#'
5960
#' @seealso \code{\link{parse_variable_start}}
6061
#'
6162
#' @export
6263
get_cycle_variables <- function(cycle, variables, variable_details,
63-
include_derived = TRUE) {
64+
include_derived = TRUE) {
6465
# Basic validation
6566
if (is.null(cycle) || cycle == "") {
6667
return(data.frame(
@@ -99,7 +100,9 @@ get_cycle_variables <- function(cycle, variables, variable_details,
99100
# Extract raw variable names using parse_variable_start
100101
cycle_vars$variable_raw <- sapply(cycle_vars$variableStart, function(vs) {
101102
raw_name <- parse_variable_start(vs, cycle)
102-
if (is.null(raw_name)) return(NA_character_)
103+
if (is.null(raw_name)) {
104+
return(NA_character_)
105+
}
103106
return(raw_name)
104107
})
105108

@@ -181,10 +184,11 @@ get_cycle_variables <- function(cycle, variables, variable_details,
181184
#'
182185
#' @export
183186
get_raw_variables <- function(cycle, variables, variable_details,
184-
include_derived = FALSE) {
187+
include_derived = FALSE) {
185188
# Get all cycle variables (harmonized)
186189
cycle_vars <- get_cycle_variables(cycle, variables, variable_details,
187-
include_derived = include_derived)
190+
include_derived = include_derived
191+
)
188192

189193
# Remove rows with NA raw variable names (e.g., DerivedVar that couldn't be parsed)
190194
cycle_vars <- cycle_vars[!is.na(cycle_vars$variable_raw), ]
@@ -301,7 +305,8 @@ get_variable_details_for_raw <- function(var_raw, cycle, variable_details, varia
301305
# Only match if variableStart is EXACTLY the var_raw (no :: or [] or Func:: or DerivedVar::)
302306
plain_matches <- variable_details[
303307
variable_details$variableStart == var_raw &
304-
grepl(cycle, variable_details$databaseStart, fixed = TRUE), ]
308+
grepl(cycle, variable_details$databaseStart, fixed = TRUE),
309+
]
305310

306311
matches <- plain_matches
307312
}
@@ -385,24 +390,19 @@ get_variable_categories <- function(var_details, include_na = FALSE) {
385390
# If values not expanded, just use min-max representation
386391
all_values <- c(all_values, as.character(value))
387392
}
388-
389393
} else if (parsed$type == "single_value") {
390394
# Single numeric value
391395
all_values <- c(all_values, as.character(parsed$value))
392-
393396
} else if (parsed$type == "continuous") {
394397
# For continuous ranges, keep as-is (don't expand)
395398
# These will be used for continuous variable generation
396399
all_values <- c(all_values, as.character(value))
397-
398400
} else if (parsed$type == "special") {
399401
# Special codes: copy, else, NA::a, NA::b
400402
all_values <- c(all_values, parsed$value)
401-
402403
} else if (parsed$type == "function") {
403404
# Function calls: Func::function_name
404405
all_values <- c(all_values, parsed$value)
405-
406406
} else {
407407
# Unknown type, use raw value
408408
all_values <- c(all_values, as.character(value))

R/mockdata-parsers.R

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@
5959
#' @export
6060
parse_variable_start <- function(variable_start, cycle) {
6161
# Basic validation
62-
if (is.null(variable_start) || is.null(cycle)) return(NULL)
63-
if (variable_start == "" || cycle == "") return(NULL)
62+
if (is.null(variable_start) || is.null(cycle)) {
63+
return(NULL)
64+
}
65+
if (variable_start == "" || cycle == "") {
66+
return(NULL)
67+
}
6468

6569
# Trim whitespace
6670
variable_start <- trimws(variable_start)
@@ -99,8 +103,8 @@ parse_variable_start <- function(variable_start, cycle) {
99103
# Strategy 3: Plain format "varname"
100104
# Check if it's NOT a DerivedVar, Func, or database-prefixed format
101105
if (!grepl("^DerivedVar::", variable_start) &&
102-
!grepl("^Func::", variable_start) &&
103-
!grepl("::", variable_start, fixed = TRUE)) {
106+
!grepl("^Func::", variable_start) &&
107+
!grepl("::", variable_start, fixed = TRUE)) {
104108
# Simple variable name, use as-is
105109
return(trimws(variable_start))
106110
}
@@ -165,9 +169,9 @@ parse_variable_start <- function(variable_start, cycle) {
165169
#' # Returns: list(min=30, max=Inf, min_inclusive=TRUE, max_inclusive=FALSE, type="continuous")
166170
#'
167171
#' # Special cases
168-
#' parse_range_notation("NA::a") # Returns: list(type="special", value="NA::a")
169-
#' parse_range_notation("copy") # Returns: list(type="special", value="copy")
170-
#' parse_range_notation("else") # Returns: list(type="special", value="else")
172+
#' parse_range_notation("NA::a") # Returns: list(type="special", value="NA::a")
173+
#' parse_range_notation("copy") # Returns: list(type="special", value="copy")
174+
#' parse_range_notation("else") # Returns: list(type="special", value="else")
171175
#'
172176
#' @note Adapted from cchsflow v4.0.0 (2025-07-27) - universal across recodeflow projects
173177
#' @export
@@ -293,7 +297,6 @@ parse_range_notation <- function(range_string, range_type = "auto", expand_integ
293297
min_inclusive = min_inclusive,
294298
max_inclusive = max_inclusive
295299
))
296-
297300
} else if (range_type == "continuous") {
298301
return(list(
299302
type = "continuous",

mockdata-tools/README.qmd

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,13 @@ source("R/mockdata-parsers.R")
246246
var_start <- "cycle1::amsdmva1, [ammdmva1]"
247247
248248
# What does cycle1 parse to?
249-
parse_variable_start(var_start, "cycle1") # Should return "amsdmva1"
249+
parse_variable_start(var_start, "cycle1") # Should return "amsdmva1"
250250
251251
# What does cycle2 parse to?
252-
parse_variable_start(var_start, "cycle2") # Should return "ammdmva1"
252+
parse_variable_start(var_start, "cycle2") # Should return "ammdmva1"
253253
254254
# What does cycle6 parse to?
255-
parse_variable_start(var_start, "cycle6") # Should return "ammdmva1"
255+
parse_variable_start(var_start, "cycle6") # Should return "ammdmva1"
256256
```
257257

258258
### Example 3: Understand range notation parsing
@@ -261,9 +261,9 @@ parse_variable_start(var_start, "cycle6") # Should return "ammdmva1"
261261
source("R/mockdata-parsers.R")
262262
263263
# Test different range notations
264-
parse_range_notation("[7,9]") # Returns: "7,8,9"
265-
parse_range_notation("[18.5,25)") # Returns: "18.5,25" with note "(continuous)"
266-
parse_range_notation("else") # Returns: "else"
264+
parse_range_notation("[7,9]") # Returns: "7,8,9"
265+
parse_range_notation("[18.5,25)") # Returns: "18.5,25" with note "(continuous)"
266+
parse_range_notation("else") # Returns: "else"
267267
268268
# Complex example from variable_details
269269
recodes <- "[18.5,25) :: 1; [7,9] :: .a(Not applicable); else :: .b(Missing)"
@@ -281,7 +281,7 @@ variables <- read.csv("inst/extdata/variables.csv", stringsAsFactors = FALSE)
281281
282282
# Find variables with mixed format (cycle::var, [var])
283283
mixed_format <- variables[grepl("::", variables$variableStart) &
284-
grepl("\\[", variables$variableStart), ]
284+
grepl("\\[", variables$variableStart), ]
285285
286286
cat("Found", nrow(mixed_format), "variables using mixed format\n\n")
287287
@@ -304,7 +304,7 @@ variable_details <- read.csv("inst/extdata/variable-details.csv", stringsAsFacto
304304
# Get all categorical variables for cycle1
305305
cycle1_vars <- get_cycle_variables("cycle1", variables, variable_details)
306306
categorical <- cycle1_vars[cycle1_vars$variableType == "Categorical" &
307-
!grepl("^DerivedVar::", cycle1_vars$variableStart), ]
307+
!grepl("^DerivedVar::", cycle1_vars$variableStart), ]
308308
309309
cat("Generating", nrow(categorical), "categorical variables for cycle1\n")
310310
@@ -384,19 +384,19 @@ if (length(duplicates) > 0) {
384384

385385
4. **Test generation**:
386386
```{r eval=FALSE}
387-
source("R/mockdata-parsers.R")
388-
source("R/mockdata-helpers.R")
389-
source("R/mockdata-generators.R")
387+
source("R/mockdata-parsers.R")
388+
source("R/mockdata-helpers.R")
389+
source("R/mockdata-generators.R")
390390
391-
variables <- read.csv("inst/extdata/variables.csv", stringsAsFactors = FALSE)
392-
variable_details <- read.csv("inst/extdata/variable-details.csv", stringsAsFactors = FALSE)
391+
variables <- read.csv("inst/extdata/variables.csv", stringsAsFactors = FALSE)
392+
variable_details <- read.csv("inst/extdata/variable-details.csv", stringsAsFactors = FALSE)
393393
394-
df <- data.frame(id = 1:100)
395-
result <- create_cat_var("my_new_var", "cycle1", df, variables, variable_details)
394+
df <- data.frame(id = 1:100)
395+
result <- create_cat_var("my_new_var", "cycle1", df, variables, variable_details)
396396
397-
if (!is.null(result)) {
398-
table(result$my_new_var)
399-
}
397+
if (!is.null(result)) {
398+
table(result$my_new_var)
399+
}
400400
```
401401

402402
### Task: Fix a variable that fails to parse
@@ -408,11 +408,11 @@ if (length(duplicates) > 0) {
408408

409409
2. **Test parsing manually**:
410410
```{r eval=FALSE}
411-
source("R/mockdata-parsers.R")
411+
source("R/mockdata-parsers.R")
412412
413-
# Try parsing the variableStart
414-
parse_variable_start("cycle1::old_name, [new_name]", "cycle1")
415-
parse_variable_start("cycle1::old_name, [new_name]", "cycle2")
413+
# Try parsing the variableStart
414+
parse_variable_start("cycle1::old_name, [new_name]", "cycle1")
415+
parse_variable_start("cycle1::old_name, [new_name]", "cycle2")
416416
```
417417

418418
3. **Common fixes**:

tests/testthat/test-mockdata.R

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ test_that("get_cycle_variables filters by exact cycle match", {
8585
cycles <- strsplit(db_start, ",")[[1]]
8686
cycles <- trimws(cycles)
8787
expect_true("cycle1" %in% cycles,
88-
info = paste("Variable", cycle1_vars$variable[i], "should have cycle1 in databaseStart"))
88+
info = paste("Variable", cycle1_vars$variable[i], "should have cycle1 in databaseStart")
89+
)
8990
}
9091
})
9192

@@ -106,8 +107,10 @@ test_that("get_cycle_variables uses exact match (not substring)", {
106107

107108
# If this variable is in cycle1_meds but NOT in cycle1, that's an error
108109
if ("cycle1_meds" %in% cycles && !"cycle1" %in% cycles) {
109-
fail(paste("Found cycle1_meds-only variable in cycle1 results:",
110-
cycle1_vars$variable[i]))
110+
fail(paste(
111+
"Found cycle1_meds-only variable in cycle1 results:",
112+
cycle1_vars$variable[i]
113+
))
111114
}
112115
}
113116

@@ -144,7 +147,8 @@ test_that("get_raw_variables returns unique raw variable names", {
144147

145148
# Check that all variable_raw are unique
146149
expect_equal(nrow(raw_vars), length(unique(raw_vars$variable_raw)),
147-
info = "All raw variable names should be unique")
150+
info = "All raw variable names should be unique"
151+
)
148152
})
149153

150154
test_that("get_raw_variables groups harmonized variables correctly", {
@@ -158,7 +162,8 @@ test_that("get_raw_variables groups harmonized variables correctly", {
158162
for (i in 1:nrow(raw_vars)) {
159163
harmonized_list <- strsplit(raw_vars$harmonized_vars[i], ", ")[[1]]
160164
expect_equal(raw_vars$n_harmonized[i], length(harmonized_list),
161-
info = paste("Count should match list length for", raw_vars$variable_raw[i]))
165+
info = paste("Count should match list length for", raw_vars$variable_raw[i])
166+
)
162167
}
163168
})
164169

@@ -172,7 +177,8 @@ test_that("get_raw_variables excludes derived variables by default", {
172177

173178
# Should not have NA in variable_raw (DerivedVar returns NA)
174179
expect_true(all(!is.na(raw_vars$variable_raw)),
175-
info = "No NA raw variable names when derived excluded")
180+
info = "No NA raw variable names when derived excluded"
181+
)
176182
})
177183

178184
# ==============================================================================

0 commit comments

Comments
 (0)