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?
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.
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.
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 .
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 } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With