Skip to content

Commit

Permalink
feat: remove redundant library calls from get_rcode_libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
averissimo committed Dec 19, 2023
1 parent a66f068 commit 209795a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
34 changes: 25 additions & 9 deletions R/get_rcode_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
9 changes: 7 additions & 2 deletions R/module_nested_tabs.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion man/get_rcode_libraries.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 28 additions & 6 deletions tests/testthat/test-rcode_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
})

Expand Down

0 comments on commit 209795a

Please sign in to comment.