[R] How to export a function from a package and access it only by specifying the namespace?

Peng Yu pengyu.ut at gmail.com
Wed Dec 2 05:42:29 CET 2009


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_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'




More information about the R-help mailing list