Skip to content

Commit

Permalink
Merge pull request #155 from pharmaverse/154-get_library-incorrectly-…
Browse files Browse the repository at this point in the history
…returns-non-function-objects-if-found

Closes #154
  • Loading branch information
bms63 authored Feb 1, 2023
2 parents 7a416bc + be518f0 commit 6064ea4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: logrx
Title: A Logging Utility Focus on Clinical Trial Programming Workflows
Version: 0.2.0
Version: 0.2.1
Authors@R:
c(
person(given = "Nathan",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ importFrom(tidyr,complete)
importFrom(tidyr,pivot_wider)
importFrom(utils,capture.output)
importFrom(utils,getParseData)
importFrom(utils,lsf.str)
importFrom(waiter,spin_solar)
importFrom(waiter,useWaiter)
importFrom(waiter,waiter_hide)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# logrx 0.2.1

- non-function objects are no longer returned as functions by `get_used_functions` (#154)

# logrx 0.2.0

- Major update release for logrx
Expand Down
18 changes: 16 additions & 2 deletions R/get.R
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,28 @@ get_used_functions <- function(file){
#' @importFrom dplyr mutate
#' @importFrom rlang .data
#' @importFrom purrr map
#' @importFrom utils lsf.str
#'
#' @return tibble that includes `library`
#'
#' @noRd
#'
get_library <- function(df){
search_lookup <- map(search(), objects)
names(search_lookup) <- search()

functions_only <- function(.x){
intersect(ls(.x), lsf.str(.x))
}

# do not search CheckExEnv, this is created while examples are executed
# during build
# T and F are given a delayedAssign within the CheckExEnv environment,
# and when we check this environments objects, the promise for T and F
# are evaluated, and return:
# stop("T used instead of TRUE"), stop("F used instead of FALSE")
search_environ <- search()[search() != "CheckExEnv"]

search_lookup <- map(search_environ, functions_only)
names(search_lookup) <- search_environ
df$library <- unlist(map(df$function_name, ~get_first(., search_lookup)))

df %>%
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/test-get.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,26 @@ test_that("used functions returned correctly when file doesn't contain all token
expect_identical(get_used_functions(filename), expected)
})

test_that("get_library returns correct function when a non-function
object of same name is available", {

writeLines('search <- "dummy object"', "dummy.R")

sys.source("dummy.R", envir = attach(NULL, name = "dummy"))

actual <- get_library(tibble(function_name = "search", SYMBOL_PACKAGE = NA))

unlink("dummy.R")
detach("dummy")

expected <- tibble::tribble(
~function_name, ~ SYMBOL_PACKAGE, ~library,
"search", NA, "package:base"
)

expect_identical(actual, expected)
})

test_that("parse does not fatal error when syntax issue occurs", {
filename <- test_path("ref", "ex4.R")

Expand Down

0 comments on commit 6064ea4

Please sign in to comment.