Skip to content

Commit

Permalink
pr: suggestion by @chlebowa
Browse files Browse the repository at this point in the history
  • Loading branch information
averissimo committed Dec 22, 2023
1 parent 209795a commit 8b6be45
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
13 changes: 5 additions & 8 deletions R/get_rcode_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
get_rcode_libraries <- function(dataset_rcode) {
packages <- rev(vapply(utils::sessionInfo()$otherPkgs, base::`[[`, character(1), "Package"))

parsed_libraries <- c()
if (!missing(dataset_rcode)) {
parsed_libraries <- 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),
function(.x) grepl("(^l|.*<-|.*[ ;=\\({]l)ibrary\\(([a-z][[:alnum:].]*)\\)$", .x),
# function(.x) grepl("library\\(.*\\)$", .x),
vapply(strsplit(dataset_rcode, "\n")[[1]], trimws, character(1))
)

# Keep only library name
parsed_libraries <- gsub(
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
Expand All @@ -26,10 +26,7 @@ get_rcode_libraries <- function(dataset_rcode) {
}

# put it into reverse order to correctly simulate executed code
paste(
"library(", Filter(Negate(function(.x) .x %in% parsed_libraries), packages), ")",
collapse = "\n", sep = ""
)
paste(c(sprintf("library(%s)", setdiff(packages, parsed_libraries)), "\n"), collapse = "\n")
}

get_rcode_str_install <- function() {
Expand Down
1 change: 0 additions & 1 deletion R/module_nested_tabs.R
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ srv_nested_tabs.teal_module <- function(id, datasets, modules, is_module_specifi
code <- c(
get_rcode_str_install(),
loaded_libs,
"",
dataset_rcode
)

Expand Down

0 comments on commit 8b6be45

Please sign in to comment.