2

I was wondering whether there is a special command to list all demo programs (R-scripts) of a package and to inspect their content without running them, i.e. without using

demo(name_of_demo_file)

In particular, I am looking for an approach to achieve this without downloading the sources and browsing the demo directory.

Ueli Hofstetter
  • 2,409
  • 4
  • 29
  • 52

1 Answers1

2

Running demo(package = "stats") displays a list of the demos. While the function doesn't let you access the code without running it, you could extract that from the package source. You wouldn't need to re-download the source since you already have it installed, and can find it within R with system.file.

For instance, you could write a short function:

print_demo_code <- function(demo, package) {
    demo_file <- system.file("demo", paste0(demo, ".R"), package = package)
    cat(readLines(demo_file), sep = "\n")
}

For example:

print_demo_code("nlm", package = "stats")

displays:

#  Copyright (C) 1997-2009 The R Core Team

### Helical Valley Function
### Page 362 Dennis + Schnabel

require(stats); require(graphics)

theta <- function(x1,x2) (atan(x2/x1) + (if(x1 <= 0) pi else 0))/ (2*pi)
## but this is easier :
theta <- function(x1,x2) atan2(x2, x1)/(2*pi)
...
David Robinson
  • 77,383
  • 16
  • 167
  • 187