Skip to content

Commit

Permalink
corrects regex
Browse files Browse the repository at this point in the history
  • Loading branch information
averissimo committed Jan 30, 2024
1 parent 8b6be45 commit 8bfdd4a
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions R/get_rcode_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ get_rcode_libraries <- function(dataset_rcode) {
packages <- rev(vapply(utils::sessionInfo()$otherPkgs, base::`[[`, character(1), "Package"))

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"
# Extract all lines that start with library()
library_regex <- "(^l|.*<-|.*[ ;=\\({]l)ibrary\\(([[:alpha:]][[:alnum:].]*)\\)$"

user_libraries <- Filter(
function(.x) grepl("(^l|.*<-|.*[ ;=\\({]l)ibrary\\(([a-z][[:alnum:].]*)\\)$", .x),
function(.x) grepl(library_regex, .x),
# function(.x) grepl("library\\(.*\\)$", .x),
vapply(strsplit(dataset_rcode, "\n")[[1]], trimws, character(1))
)

# Keep only library name
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",
library_regex, "\\2",
# Strip out comments
vapply(user_libraries, function(.x) as.character(str2expression(.x)), character(1L))
)
Expand Down

0 comments on commit 8bfdd4a

Please sign in to comment.