Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R demo() and example() methods?

Tags:

r

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
like image 260
harshsinghal Avatar asked Nov 18 '09 14:11

harshsinghal


2 Answers

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)).  
like image 188
Shane Avatar answered Oct 21 '22 00:10

Shane


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.

like image 31
Dirk Eddelbuettel Avatar answered Oct 21 '22 02:10

Dirk Eddelbuettel