Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define which variables or functions from a package are exported

My R package uses an internal variable x. If I load the package (I've only tried using devtools::load_all), then x doesn't appear in the ls() list, but it does have a value. How can I avoid this?

I'm fine with the user being able to access the variable with myPackage::x, but not simply x.

like image 844
Andreas Avatar asked Nov 18 '12 00:11

Andreas


People also ask

What is exported functions?

Export functions are functions that a module exposes to other modules. They can be thought of as the module's interface. Dependency Walker uses the exported list to check for unresolved external errors in the selected module.

How do I export a variable in JavaScript?

Use named exports to export multiple variables in JavaScript, e.g. export const A = 'a' and export const B = 'b' . The exported variables can be imported by using a named import as import {A, B} from './another-file. js' . You can have as many named exports as necessary in a file.

What is export function Golang?

In Go, a name is exported if it begins with a capital letter. For example, Pizza is an exported name, as is Pi , which is exported from the math package. pizza and pi do not start with a capital letter, so they are not exported.

What is imported function?

The import function imports text data by converting from external text representation to the internal format. The export function exports text data by converting from the internal format to the external text representation.


2 Answers

The load_all function has an export_all argument.

From ?load_all

If TRUE (the default), export all objects. If FALSE, export only the objects that are listed as exports in the NAMESPACE file.

So, try using export_all=FALSE in your load_all call.

like image 137
GSee Avatar answered Oct 14 '22 20:10

GSee


Try building the package first, and check whether the problem still exists. The exports from a package are defined in the NAMESPACE file. When you use devtools::load_all, the namespace isn't loaded ( see here). Read more about this and building a package in the manual Writing R extensions.

You might be using a default export pattern in your NAMESPACE file. Check it in your package, and if it looks like this:

exportPattern("^[^\\.]")

then the package exports everything from the namespace that doesn't start with a dot. So you either call it .x, or you change the exportPattern() to eg...

export(myfun1, myfun2) 

to export the functions myfun1 and myfun2 from the package. By explicitly defining what you want to export, you avoid that something's available when there's no need for that.

like image 6
Joris Meys Avatar answered Oct 14 '22 20:10

Joris Meys