The namespace mechanism of R allows one to export
functions which then are visible to the user. Furthermore, it allows to import
functions from other packages. Whereas the benefit of export is obvious, I have more problems understanding the benefit of import.
One benefit seems to be, that one can use functions from other packages without attaching the package and thereby saving memory. This is exemplified in section 1.6.4 in the writing R extensions manual.
However, there must be other benefits of the import function. Especially, section 1.6.6 (that deals with S4 classes) shows the namespace
of the stats4 package:
export(mle)
importFrom("graphics", plot)
importFrom("stats", optim, qchisq)
## For these, we define methods or (AIC, BIC, nobs) an implicit generic:
importFrom("stats", AIC, BIC, coef, confint, logLik, nobs, profile,
update, vcov)
exportClasses(mle, profile.mle, summary.mle)
## All methods for imported generics:
exportMethods(coef, confint, logLik, plot, profile, summary, show, update, vcov)
## implicit generics which do not have any methods here
export(AIC, BIC, nobs)
Here there are functions imported which are neither S4 classes nor generics (where it would make sense to use import as well, as documented in the example in that section), but functions like plot
from the graphics
package which are automatically loaded when R starts.
Therefore my question is, what is the benefit of importing functions like plot
, optim
or qchisq
?
See Question&Answers more detail:
os