From 8b6be45102d4f7da25bb162bd60ca2b42ccc4852 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Fri, 22 Dec 2023 14:41:56 +0000 Subject: [PATCH] pr: suggestion by @chlebowa --- R/get_rcode_utils.R | 13 +++++-------- R/module_nested_tabs.R | 1 - 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/R/get_rcode_utils.R b/R/get_rcode_utils.R index 00ac33fb3f..6d1d36d2ef 100644 --- a/R/get_rcode_utils.R +++ b/R/get_rcode_utils.R @@ -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 @@ -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() { diff --git a/R/module_nested_tabs.R b/R/module_nested_tabs.R index fa232a42c0..f51f108c11 100644 --- a/R/module_nested_tabs.R +++ b/R/module_nested_tabs.R @@ -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 )