iago-lito
iago-lito

Reputation: 3218

Choose function to load from an R package

I like using function reshape from the matlab package, but I need then to specify base::sum(m) each time I want to sum the elements of my matrix or else matlab::sum is called, which only sums by columns..

I need loading package gtools to use the rdirichlet function, but then the function gtools::logit masks the function pracma::logit that I like better..

I gess there are no such things like:

library(loadOnly = "rdirichlet", from = "gtools")

or

library(loadEverythingFrom = "matlab", except = "sum")

.. because functions from the package matlab may internaly work on the matlab::sum function. So the latter must be loaded. But is there no way to get this behavior from the point of view of the user? Something that would feel like:

library(pracma)
library(matlab)
library(gtools)
sum <- base::sum
logit <- pracma::logit

.. but that would not spoil your ls() with all these small utilitary functions?

Maybe I need defining my own default namespace?

Upvotes: 1

Views: 850

Answers (2)

Matthew Plourde
Matthew Plourde

Reputation: 44634

To avoid spoiling your ls, you can do something like this:

.ns <- new.env()
.ns$sum <- base::sum
.ns$logit <- pracma::logit
attach(.ns)

Upvotes: 3

chakalakka
chakalakka

Reputation: 474

To my knowledge there is no easy answer to what you want to achieve. The only dirty hack I can think of is to download the source of the packages "matlab", "gtools", "pracma" and delete the offending functions from their NAMESPACE file prior to installation from source (with R CMD INSTALL package).

However, I would recommend using the explicit notation pracma::logit, because it improves readability of your code for other people and yourself in the future.

This site gives a good overview about package namespaces: http://r-pkgs.had.co.nz/namespace.html

Upvotes: 1

Related Questions