Bonono
Bonono

Reputation: 847

keep base package unmasked

Twice now I have encountered the problem of base functions being masked by other packages. is there a way of being able to load other packages in to library but not let them mask the base package?

Upvotes: 1

Views: 73

Answers (1)

Hong Ooi
Hong Ooi

Reputation: 57697

In general, it's always possible to use packages without also attaching their namespaces to the search path. The attaching, not the loading, is what causes functions with the same name to clash with each other. That is, instead of

library(pkg)
pkgfunc(a, b, ...)

do

loadNamespace("pkg")
pkg::pkgfunc(a, b, ...)

where the :: operator means to call the function that is exported from the given namespace. You might run into problems, but they should be few.

For programmers coming from other languages, this will be more familiar than the usual practice in R of calling library() every time you want to use a package. (I do find it a bit odd that, years after R made package namespaces mandatory to deal with this issue, people still load every package into the global environment.)

If you don't want to do this, then you can prefix the base function with base:: instead. This is an explicit reference to a function in the base package, and so will ignore other functions with the same name.

base::sum(1:10)
base::list(a=1, b=2, c=3)

Upvotes: 4

Related Questions