Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you determine the namespace of a function?

Tags:

namespaces

r

Given a function, how do you determine which namespace it has come from?

For example, if I type mean.default at the command prompt, the output includes the fact that it is in the base package. I want to be able to do something like getNamespace(mean.default) and have it return "base" (or the actual base environment).

There is a getNamespace function but seems to only accept package names rather than function names.

print.function uses internal code to retrieve the namespace. I got as far as browsing do_printfunction in src/main/print.c but then gave up.

like image 254
Richie Cotton Avatar asked Jun 21 '11 17:06

Richie Cotton


People also ask

How do you call a namespace function in C++?

Namespaces in C++ You only need to prefix the function you wish to call with namespace_name:: -- similar to how you would call a static member function of a class. Another convenience of namespaces is that they allow you to use the same function name, when it makes sense to do so, to perform multiple different actions.

What is a namespace in programming?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.


1 Answers

I very recently learned about find() which seems to do just this.

R> find("ls") [1] "package:base" R> find("na.locf") [1] "package:zoo" 
like image 144
Dirk Eddelbuettel Avatar answered Sep 18 '22 15:09

Dirk Eddelbuettel