Friday, 15 January 2010

r - Can't pass a character string to require() in a function? -



r - Can't pass a character string to require() in a function? -

this question has reply here:

load r bundle character string 2 answers

so came across issue simple function i'm attempting write check if bundle available, , download if not.

however reason, when pass character string require via function fails recognise (see simple illustration below)...

this works...

p.f <- function(x) print(x) p.f("hello") [1] "hello"

as this...

(require("base")) [1] true

but doesn't...?

p.f <- function(x) (require(x)) p.f("base") loading required package: x [1] false warning message: in library(package, lib.loc = lib.loc, character.only = true, logical.return = true, : there no bundle called ‘x’

why fail recognise character string instead of x?

that’s consequence of way require evaluates arguments. can around passing character.only argument:

p.f <- function(x) (require(x, character.only = true))

r

No comments:

Post a Comment