[R] Wrong function when import from module using get

Jakub Jankiewicz jcubic at jcubic.pl
Tue Jun 27 10:06:16 CEST 2017


Hi,

I found this issue:

when I have 3 modules:

* AnalysisA
* AnalysisB
* AnalysisC

I load all modules at the beginning from list of modules:

analyses <- list('AnalysisA', 'AnalysisB', 'AnalysisC')

for (module in analyses) {
   library(module, character.only = TRUE)
}

and I want to add a function isValid to each module, but I've added it only
for AnalysisB. When I run this code:

# value <- <selected by user>

flags <- sapply(analyses, function(module) {
    namespace <- getNamespace(module)
    tryCatch({
        isValid <- get("isValid", namespace)
        isValid(value)
    }, error = function(e) {
        TRUE
    })
})

# show analyses

analyses[flags]

then when R call get for AnalysisA it throw error, for AnalysisB it
return function but for AnalysisC it return function from AnalysisB and don't
throw error.

As a workaround instead of tryCatch I've used:

if ("isValid" %in% ls(namespace)) {
    isValid <- get("isValid", namespace)
    isValid(value)
} else {
    TRUE
}

Is this a bug? Should this be reported?

I'm using R 3.4.0 on windows.

--
Jakub Jankiewicz, Web Developer
http://jcubic.pl/me



More information about the R-help mailing list