From 26345f2ba32c8bcdce41c51e46031e46251e3456 Mon Sep 17 00:00:00 2001 From: Edgar Manukyan Date: Mon, 2 Dec 2024 01:18:46 +0000 Subject: [PATCH] #107 lintr --- R/generate_code.R | 12 ++++++------ tests/testthat/test-generate_code.R | 18 ++++++++---------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/R/generate_code.R b/R/generate_code.R index 9e6c060e..183ae3ec 100644 --- a/R/generate_code.R +++ b/R/generate_code.R @@ -126,7 +126,7 @@ generate_code <- function(spec, domain, out_dir = ".") { ) |> unlist() |> remove_last_pipe() |> - append(cm_template_prefix, after = 0) |> + append(cm_template_prefix, after = 0L) |> styler::style_text() # Save the code to a file @@ -165,7 +165,7 @@ generate_one_var_code <- function(spec_var) { } # Remove the arguments that are missing - args <- purrr::discard(args, \(x) is.vector(x) && any(is.na(x))) + args <- purrr::discard(args, \(x) is.vector(x) && anyNA(x)) # Generate the function call generated_call <- rlang::call2( @@ -200,7 +200,7 @@ parse_into_c_call <- function(str_in) { admiraldev::assert_character_scalar(str_in) str_out <- str_in |> - stringr::str_split(",") |> + stringr::str_split(stringr::fixed(",")) |> unlist() |> stringr::str_trim() @@ -245,7 +245,7 @@ remove_last_pipe <- function(code_blocks) { # The last code block should not have a pipe operator code_blocks[len_code_block] <- code_blocks[len_code_block] |> - stringr::str_remove("%>%") + stringr::str_remove(stringr::fixed("%>%")) code_blocks } @@ -296,12 +296,12 @@ get_domain_spec <- function(spec, domain) { entity_sub_algorithm_temp = dplyr::if_else( mapping_algorithm %in% "condition_add", mapping_algorithm, - entity_sub_algorithm, + entity_sub_algorithm ), mapping_algorithm = dplyr::if_else( mapping_algorithm %in% "condition_add", entity_sub_algorithm, - mapping_algorithm, + mapping_algorithm ), entity_sub_algorithm = entity_sub_algorithm_temp ) |> diff --git a/tests/testthat/test-generate_code.R b/tests/testthat/test-generate_code.R index 0aff91d9..98cd64fb 100644 --- a/tests/testthat/test-generate_code.R +++ b/tests/testthat/test-generate_code.R @@ -10,12 +10,10 @@ test_that("generate_code works", { # Convert all NA to NA_character_ spec <- spec |> - dplyr::mutate( - dplyr::across( - .cols = dplyr::everything(), - .fns = ~dplyr::if_else(is.na(.x), NA_character_, .x) - ) - ) + dplyr::mutate(dplyr::across( + .cols = dplyr::everything(), + .fns = ~ dplyr::if_else(is.na(.x), NA_character_, .x) + )) domain <- "cm" @@ -24,12 +22,12 @@ test_that("generate_code works", { unlink(out_dir, recursive = TRUE, force = TRUE) dir.create(out_dir, showWarnings = FALSE, recursive = TRUE) - withr::with_options(list(width = 20), { + withr::with_options(list(width = 20L), { generate_code(spec, domain, out_dir) observed <- readLines(file.path(out_dir, paste0(domain, "_sdtm_oak_code.R"))) - expect_true(length(observed) > 10L) - expect_true(grepl("generate_oak_id_vars", observed) |> any()) - expect_true(grepl("assign_no_ct", observed) |> any()) + expect_gt(length(observed), 10L) + expect_true(grepl("generate_oak_id_vars", observed, fixed = TRUE) |> any()) + expect_true(grepl("assign_no_ct", observed, fixed = TRUE) |> any()) }) })