tags:

views:

90

answers:

2

Is there a simple way to check if R functions and packages have demo() and example() methods?

When building a package, does the package need to have the necessary objects so that demo() and example() can be called on it?

Edit: In trying to answer this, i checked the source code of demo()

demo(package=.packages(all.available = TRUE)) # check which packages have demo

+1  A: 

Neither examples nor demos are required to build a package.

The example() function can be run on any other function, and it just runs the commands in the "Examples:" section of the help file (excluding ones that have ## Not run:). Also see this related stackoverflow question.

For instance, look at ?example and then call example(example).

Regarding the idea of finding all functions that have examples: that's possible, although I'm not aware of any particularly easy way of doing it. I would probably use the existing examples function but alter it so that it doesn't execute the example functions and then run it across all functions in my installed packages. But that's probably not worth the effort because it will return a HUGE list of functions (most R documentation has an example). You're better of just trying example(function.name) whenever you're curious about a specific task.

You've already answered your own question about demo(): demo(package=.packages(all.available = TRUE)).

Shane
+1  A: 

You can always look at the sources of package 'myPkg', or even run R CMD check myPkg over the sources as that will extract a file myPkg-Ex.R containing the examples.

Dirk Eddelbuettel