Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

define a function in a specific namespace

Tags:

namespaces

r

I have created a package 'mypackage' (with a namespace 'mypackage' attached) In this package there is a function that I can call either with

'myfunction'

or

'mypackage::myfunction'

Now I want to replace myfunction by another version (updated).

I used to do

source(path)

where path is the path of a file where the updated 'myfunction' is defined

Now I moved to R 2.14.x and this system doesnt work because apparently R checks first if there is a function inside the same namespace, and if there is one, it uses this one and not the others.

My question: how can I push the updated function to be in the same namespace as the package one?

like image 221
RockScience Avatar asked Dec 14 '11 09:12

RockScience


People also ask

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

Declaring namespaces and namespace membersTypically, you declare a namespace in a header file. If your function implementations are in a separate file, then qualify the function names, as in this example. A namespace can be declared in multiple blocks in a single file, and in multiple files.

How do you call a function from namespace?

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.

How do I access variables from namespace?

The namespaces in some namespaces may also be nested. To access them we are required to use the scope resolution operator :: operator the number of times that are there to access them. When we want to access the variable sample , we need to use example1::example2::example3::sample .

What is nested namespace in C++?

Namespaces can be nested, which means that you can define one namespace inside another called a nested namespace, as shown below: namespace namespace_name1 { // code declarations namespace namespace_name2 { // code declarations } }


1 Answers

See ?assignInNamespace. For example

assignInNamespace("myfunction", foo, "mypackage")

will assign the object foo to the object named "myfunction" in namespace "mypackage". foo can be whatever object you want, even myfunction but you will need to be careful to ensure you call mypackage::myfunction if you also have myfunction in the global environment/workspace.

like image 144
Gavin Simpson Avatar answered Nov 20 '22 12:11

Gavin Simpson