Jakub Jankiewicz
2017-Jun-27 08:06 UTC
[R] Wrong function when import from module using get
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
Jakub Jankiewicz
2017-Jun-27 08:15 UTC
[R] Wrong function when import from module using get
By module I mean a package instaled using: R CMD INSTAL /path/ On Tue, 27 Jun 2017 10:06:16 +0200 Jakub Jankiewicz <jcubic at jcubic.pl> wrote:> 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-- Jakub Jankiewicz, Web Developer http://jcubic.pl/me