Skip to content

Commit

Permalink
{linter} corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
salatak committed Feb 8, 2024
1 parent abcec2a commit 48b8c75
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 37 deletions.
42 changes: 25 additions & 17 deletions R/api_create_study.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
api__minimization_pocock <- function( # nolint: cyclocomp_linter.
api__minimization_pocock <- function(
# nolint: cyclocomp_linter.
identifier, name, method, arms, covariates, p, req, res) {

collection <- checkmate::makeAssertCollection()

checkmate::assert(
Expand All @@ -18,7 +18,8 @@ api__minimization_pocock <- function( # nolint: cyclocomp_linter.
checkmate::assert(
checkmate::check_choice(method, choices = c("range", "var", "sd")),
.var.name = "method",
add = collection)
add = collection
)

checkmate::assert(
checkmate::check_list(
Expand Down Expand Up @@ -55,43 +56,50 @@ api__minimization_pocock <- function( # nolint: cyclocomp_linter.
len = 2,
),
.var.name = "covariates1",
add = collection)
add = collection
)

checkmate::assert(
checkmate::check_names(
names(c_content),
permutation.of = c("weight", "levels"),
),
.var.name = "covariates2",
add = collection)
add = collection
)

# check covariate weight
checkmate::assert(
checkmate::check_numeric(c_content$weight,
lower = 0,
finite = TRUE,
len = 1,
null.ok = FALSE
lower = 0,
finite = TRUE,
len = 1,
null.ok = FALSE
),
.var.name = "weight",
add = collection)
add = collection
)

checkmate::assert(
checkmate::check_character(c_content$levels,
min.chars = 1,
min.len = 2,
unique = TRUE
min.chars = 1,
min.len = 2,
unique = TRUE
),
.var.name = "levels",
add = collection)
add = collection
)
}

# check probability
checkmate::assert(
checkmate::check_numeric(p, lower = 0, upper = 1, len = 1,
any.missing = FALSE, null.ok = FALSE),
checkmate::check_numeric(p,
lower = 0, upper = 1, len = 1,
any.missing = FALSE, null.ok = FALSE
),
.var.name = "p",
add = collection)
add = collection
)


if (length(collection$getMessages()) > 0) {
Expand Down
12 changes: 5 additions & 7 deletions R/api_randomize.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,25 @@ api__randomize_patient <- function(study_id, current_state, req, res) {

# Dispatch based on randomization method to parse parameters
params <-
switch(
method_randomization,
switch(method_randomization,
minimisation_pocock = do.call(
parse_pocock_parameters, list(db_connection_pool, study_id, current_state)
)
)
)

arm_name <-
switch(
method_randomization,
switch(method_randomization,
minimisation_pocock = do.call(
unbiased:::randomize_minimisation_pocock, params
)
)
)

arm <- dplyr::tbl(db_connection_pool, "arm") |>
dplyr::filter(study_id == !!study_id & .data$name == arm_name) |>
dplyr::select("arm_id" = "id", "name", "ratio") |>
dplyr::collect()

randomized_patient <- unbiased:::save_patient(study_id, arm$arm_id)
randomized_patient <- unbiased:::save_patient(study_id, arm$arm_id)

if (!is.null(randomized_patient$error)) {
res$status <- 503
Expand Down
26 changes: 13 additions & 13 deletions R/db.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,21 @@ create_study <- function(
r
}

save_patient <- function(study_id, arm_id){

r <- tryCatch({
randomized_patient <- DBI::dbGetQuery(
db_connection_pool,
"INSERT INTO patient (arm_id, study_id)
save_patient <- function(study_id, arm_id) {
r <- tryCatch(
{
randomized_patient <- DBI::dbGetQuery(
db_connection_pool,
"INSERT INTO patient (arm_id, study_id)
VALUES ($1, $2)
RETURNING id, arm_id",
list(arm_id, study_id)
)
},
error = function(cond) {
logger::log_error("Error randomizing patient: {cond}", cond=cond)
list(error = conditionMessage(cond))
}
list(arm_id, study_id)
)
},
error = function(cond) {
logger::log_error("Error randomizing patient: {cond}", cond = cond)
list(error = conditionMessage(cond))

Check warning on line 155 in R/db.R

View check run for this annotation

Codecov / codecov/patch

R/db.R#L154-L155

Added lines #L154 - L155 were not covered by tests
}
)

return(r)
Expand Down

0 comments on commit 48b8c75

Please sign in to comment.