Namespace and conflicts in function names - r

I am used to Python, and to a syntax like
import mypackage
import myotherpackage as mop
x = mypackage.calc(y)
z = mop.calc(y)
Am I right in understanding that R, like Matlab, does not have a proper namespace? I am a beginner in R.
How do you address conflict names?
What if two packages have the same function names?
If in R you do
library(mylib)
library(my_second_lib)
and both libraries have the function calc(), does calling calc() mean calling the function of the most recently loaded package?
How do you test two versions of a package? Eg if you want to test the output of your new package vs the output of the old one, and they both have the same function names?

Related

R: data.table function not working in package [duplicate]

This is very simple question.
I am extending someone's package. It currently uses packages A, B and they are listed in the DESCRIPTION file.
If I need functions from package C - to add a package to the dependencies - do I just add the package in the DESCRIPTION file and that is all that is needed? Into what section - Depends or Imports? Are there more other steps to make? Do I need to use prefix C::functionInC() once my code needs to use a package C function?
Short answer:
Add C to Imports: and when using the C functions, use the double semicolon prefix.
Longer context:
The link below provides the following advice
http://r-pkgs.had.co.nz/namespace.html#imports
R functions
If you are using just a few functions from another package, my recommendation is to note the package name in the Imports: field of the DESCRIPTION file and call the function(s) explicitly using ::, e.g., pkg::fun().
If you are using functions repeatedly, you can avoid :: by importing the function with #importFrom pgk fun. This also has a small performance benefit, because :: adds approximately 5 µs to function evaluation time.
Alternatively, if you are repeatedly using many functions from another package, you can import all of them using #import package. This is the least recommended solution because it makes your code harder to read (you can’t tell where a function is coming from), and if you #import many packages, it increases the chance of conflicting function names.

Imported packages do not work with my package in R for some functions?

I built my own package. I imported the most important package that I need them in my package. In these packages there are some functions are not exported by the package (I did not find them in the namespace of the package). I need these functions. When I call them, I get an error that those funciton are not found. So, How I can solve this problem. Also, how does these packages uses this functions inside their packages without using #export!! any help please?
based on the answer:
I understand I do it like this inside my R code: I need the following function:
args <- preproc(c(as.list(environment()), call = match.call()),
check_matrix,
check_fammat,
check_parmat,
check_par2mat)
list2env(args, environment())
Then I must do like this:
VineCopula:::preproc()
Then how to call args?
You can call non exported functions with
packagename:::functionname()
It is however not recommended to do that since those functions might not be supported in future versions of packages.
If you want to use a non exported function from your own library inside your own library, you can just use functionname() altough some package developers still prefer packagename:::functionname().

Name space of base package needed?

Writing an R-package I use name spaces to use functions from existing packages, e.g. raster::writeRaster(...).
However, I am wondering if functions from the base package have also be used like this, e.g. base::sum(...). This might end up in very confusing code parts:
foo[base::which(base::sapply(bar, function())]
No you don't need to reference base packages like this. You only need to reference non-base packages to ensure they are loaded into the function environment when functions from your package are run, either by using :: or #import in the Roxegen notes at the top of your script. See why you don't need to reference base packages below:
http://adv-r.had.co.nz/Environments.html
"Package namespaces keep packages independent. For example, if package A uses the base mean() function, what happens if package B creates its own mean() function? Namespaces ensure that package A continues to use the base mean() function, and that package A is not affected by package B (unless explicitly asked for)."(Hadley Wickham)
The only time you need to reference base:: is if the namespace for your package contains a package that has an alternative function of the same name.

Difference between environment and namespace

I know what a namespace is from other languages but in R I just cannot find a difference between the environment and namespace. Could anyone explain this since in the tutorials I have read (as The Art of R Programming and others) I just cannot find a distinction?
A namespace is something specific to a package. It is defined as a list of directive that allow you to import functions from other packages to be used locally or to export your functions and classes to be used in R.
So if you have created in your package a function called foo you will add to your namespace something like export(foo) to make your function usable.
If you want to import function from a specific package to use them in yours, you will add import(thePackage)
The environment is simply the space where you associate names to values. You can see it as a context in which you can evaluate functions and expressions.

Code to export a function in R

Is it possible to export a function in R by evaluating an expression? What I would like to write something like
my.great.fun = function(x) x
export(my.great.fun)
in a R file in a package and have my.great.fun exported.
Then you can evaluate that in a loop, conditionally, define family of functions concisely etc. The immediate application would be generating and exporting setters and getters for an S4 class in two lines of code as opposed to as many lines as there are slots. I saw the package R.MethodsS3, but the export option doesn't seem to do anything other than setting an attribute, ditto the now un-exported export function therein. setGeneric for S4 doesn't have a similar option. I am a fairly navigated R developer with several packages under my belt, so it doesn't help to point me to standard documentation about the NAMESPACE file. roxygen #export is not a solution because it doesn't work in loops, is not in the language, is not first, second or even third class in R. It seems to me we can create functions, generics of S3 and S4 flavors, methods of S3 and S4 flavors all in the language, and the only bit that is missing is the export part: omission or hole in my knowledge? Thanks
You can add the function to an environment when calling export. Then in .onLoad you attach the environment. For instance, see rex_mode in package rex. Reading packages written by the R intelligentsia pays off.

Resources