Working with data in Python or R, we often load several packages. In some cases, two packages (e.g. foo
and bar
) might each contain some function (e.g. do_stuff
).
The way this is managed in Python to prevent ambiguity or surprises is like:
from foo import do_stuff
from bar import other_function # (does not load/import do_stuff() from bar)
In R, all the code I see just imports whole packages with
multiple library(package_name)
statements. I would think this would lead to very difficult-to-catch bugs. For example, see Reordering factor gives different results, depending on which packages are loaded. In fact this occurred even though "there is no masking, since reorder.factor doesn't exist in base."
I expected the general answer to this problem to be something like the from package import function
code above, but it wasn't. In fact the accepted (and only) answer just explains why the problem exists (not to downplay that contribution). There's a workaround provided in a comment of the answer, but that workaround is specific to that particular function (reorder
).
Is there a general way that I can import only a specific function from a specific package in R? So that I can be deliberate and unambiguous about where all of the function calls in my code come from and ensure that they do what I think they're doing?
package::function
syntax. If you want skip using that syntax always you could do something likefunction <- package::function
and then use it as is. – Kovno