From 209795ad661e3a9e04a6944fed6d7d42df08bc39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Tue, 19 Dec 2023 17:11:49 +0000 Subject: [PATCH] feat: remove redundant library calls from get_rcode_libraries --- R/get_rcode_utils.R | 34 +++++++++++++++++++++++-------- R/module_nested_tabs.R | 9 ++++++-- man/get_rcode_libraries.Rd | 2 +- tests/testthat/test-rcode_utils.R | 34 +++++++++++++++++++++++++------ 4 files changed, 61 insertions(+), 18 deletions(-) diff --git a/R/get_rcode_utils.R b/R/get_rcode_utils.R index 13f04a7f88..00ac33fb3f 100644 --- a/R/get_rcode_utils.R +++ b/R/get_rcode_utils.R @@ -4,17 +4,33 @@ #' #' @return Character object contain code #' @keywords internal -get_rcode_libraries <- function() { - packages <- vapply( - utils::sessionInfo()$otherPkgs, - function(x) paste0("library(", x$Package, ")"), - character(1) - ) - # put it into reverse order to correctly simulate executed code - paste(rev(packages), collapse = "\n") -} +get_rcode_libraries <- function(dataset_rcode) { + packages <- rev(vapply(utils::sessionInfo()$otherPkgs, base::`[[`, character(1), "Package")) + + parsed_libraries <- c() + if (!missing(dataset_rcode)) { + # Extract all lines with library() + # TODO: remove strings first as this will pass "this is a string with library(something) in it" + user_libraries <- Filter( + function(.x) grepl("library\\(.*\\)$", .x), + vapply(strsplit(dataset_rcode, "\n")[[1]], trimws, character(1)) + ) + # Keep only library name + parsed_libraries <- gsub( + # library(...) must be located at beginning of line, or have a valid character before + "(^l|.*<-|.*[ ;=\\({]l)ibrary\\(([a-z][a-zA-Z0-9.]*)\\)$", "\\2", + # Strip out comments + vapply(user_libraries, function(.x) as.character(str2expression(.x)), character(1L)) + ) + } + # put it into reverse order to correctly simulate executed code + paste( + "library(", Filter(Negate(function(.x) .x %in% parsed_libraries), packages), ")", + collapse = "\n", sep = "" + ) +} get_rcode_str_install <- function() { code_string <- getOption("teal.load_nest_code") diff --git a/R/module_nested_tabs.R b/R/module_nested_tabs.R index 73afb01be8..fa232a42c0 100644 --- a/R/module_nested_tabs.R +++ b/R/module_nested_tabs.R @@ -285,10 +285,15 @@ srv_nested_tabs.teal_module <- function(id, datasets, modules, is_module_specifi hashes <- calculate_hashes(datanames, datasets) + # list of code + dataset_rcode <- get_datasets_code(datanames, datasets, hashes) + loaded_libs <- get_rcode_libraries(dataset_rcode) + code <- c( get_rcode_str_install(), - get_rcode_libraries(), - get_datasets_code(datanames, datasets, hashes) + loaded_libs, + "", + dataset_rcode ) do.call( diff --git a/man/get_rcode_libraries.Rd b/man/get_rcode_libraries.Rd index f85a6885c4..f22881f394 100644 --- a/man/get_rcode_libraries.Rd +++ b/man/get_rcode_libraries.Rd @@ -4,7 +4,7 @@ \alias{get_rcode_libraries} \title{Generates library calls from current session info} \usage{ -get_rcode_libraries() +get_rcode_libraries(dataset_rcode) } \value{ Character object contain code diff --git a/tests/testthat/test-rcode_utils.R b/tests/testthat/test-rcode_utils.R index 1add79e18d..4428fb11bc 100644 --- a/tests/testthat/test-rcode_utils.R +++ b/tests/testthat/test-rcode_utils.R @@ -34,13 +34,35 @@ testthat::test_that("With teal.load_nest_code option is not character get_rcode_ ) }) - testthat::test_that("get_rcode_libraries returns current session packages", { - testthat::expect_true( - setequal( - strsplit(gsub("library\\(|\\)", "", get_rcode_libraries()), "\n")[[1]], - vapply(sessionInfo()$otherPkgs, FUN = `[[`, index = "Package", FUN.VALUE = character(1), USE.NAMES = FALSE) - ) + testthat::expect_setequal( + strsplit(gsub("library\\(|\\)", "", get_rcode_libraries()), "\n")[[1]], + vapply(sessionInfo()$otherPkgs, FUN = `[[`, index = "Package", FUN.VALUE = character(1)) + ) +}) + +testthat::test_that("get_rcode_libraries returns current session packages excluding testthat", { + # Make sure testthat is attached + require(testthat, quietly = TRUE) + testthat::expect_setequal( + setdiff( + vapply(sessionInfo()$otherPkgs, FUN = `[[`, index = "Package", FUN.VALUE = character(1)), + c(strsplit(gsub("library\\(|\\)", "", get_rcode_libraries("library(testthat)\n")), "\n")[[1]]) + ), + "testthat" + ) +}) + +testthat::test_that("get_rcode_libraries returns current session packages excluding testthat and teal", { + # Make sure testthat is attached + require(testthat, quietly = TRUE) + require(teal, quietly = TRUE) + testthat::expect_setequal( + setdiff( + vapply(sessionInfo()$otherPkgs, FUN = `[[`, index = "Package", FUN.VALUE = character(1)), + c(strsplit(gsub("library\\(|\\)", "", get_rcode_libraries("library(testthat)\nlibrary(teal)")), "\n")[[1]]) + ), + c("testthat", "teal") ) })