Peng Yu
2009-Dec-02 04:42 UTC
[R] How to export a function from a package and access it only by specifying the namespace?
I have the following test package. $ ls DESCRIPTION man NAMESPACE R $ cat DESCRIPTION Package: try.package Type: Package Title: What the package does (short line) Version: 1.0 Date: 2009-10-26 Author: Who wrote it Maintainer: Who to complain to <yourfault at somewhere.net> Description: More about what it does (maybe more than one line) License: What license is it under? LazyLoad: yes $ cat NAMESPACE export( my_test_g ) $ ls R my_test_f.R randomxx.R $ for f in R/*.R; do echo $f; cat $f;done R/my_test_f.R my_test_f<-function() { print("Hello") } R/randomxx.R my_test_g<-function() { print("Helloggg") } ---------------------------------------- I install the package by R CMD INSTALL -l /path/to/R_user --no-docs my_package `my_package' is the directory where the package is in. Then I try the package 'try.package' in an R session. I'm wondering why neither 'my_test_f' and 'try.package::my_test_f' work. Why 'my_test_g' can be accessed with 'try.package::' and without 'try.package::'? Is there a way to make 'my_test_g' accessible only by specifying the namespace 'try.package::'?> library(try.package) > try.package::my_test_gfunction () { print("Helloggg") } <environment: namespace:try.package>> my_test_gfunction () { print("Helloggg") } <environment: namespace:try.package>> my_test_fError: object "my_test_f" not found> try.package::my_test_fError: 'my_test_f' is not an exported object from 'namespace:try.package'
Sharpie
2009-Dec-02 05:27 UTC
[R] How to export a function from a package and access it only by specifying the namespace?
Peng Yu wrote:> > Then I try the package 'try.package' in an R session. I'm wondering > why neither 'my_test_f' and 'try.package::my_test_f' work. >The error message you got below clearly explains this-- you did not export my_test_f in your NAMESPACE file. To access unexported functions, you must use the ':::' operator: try.package:::my_test_f() Peng Yu wrote:> > Why 'my_test_g' can be accessed with 'try.package::' and without > 'try.package::'? >Because you exported it in the NAMESPACE file. Peng Yu wrote:> > Is there a way to make 'my_test_g' accessible only by specifying the > namespace 'try.package::'? >No. The purpose of the '::' operator is for those cases where multiple packages are loaded that each export a function with the same name. This is known as "masking" and the last loaded package will contribute the dominant function-- i.e. the function the gets called when the user types "functionName()" and not "packageName::functionName()". The "::" operator allows the selection of functions that are masked by the dominant function. If you really want to conceal a function from user-level code, don't export it and it will only be accessible via the ":::" operator. Peng Yu wrote:> >> library(try.package) >> try.package::my_test_g > function () > { > print("Helloggg") > } > <environment: namespace:try.package> >> my_test_g > function () > { > print("Helloggg") > } > <environment: namespace:try.package> >> my_test_f > Error: object "my_test_f" not found >> try.package::my_test_f > Error: 'my_test_f' is not an exported object from 'namespace:try.package' >-- View this message in context: http://n4.nabble.com/How-to-export-a-function-from-a-package-and-access-it-only-by-specifying-the-namespace-tp932776p932798.html Sent from the R help mailing list archive at Nabble.com.