[R] Question: write an R script with help information available to the user

Bert Gunter gunter.berton at gene.com
Sun Feb 3 07:50:12 CET 2013

A  related approach which, if memory serves, was originally in S eons
ago, is to define a "doc" attribute of any function (or object, for
that matter)  that you wish to document that contains text for
documentation and a doc() function of the form:

doc <- function(obj) cat(attr(obj,"doc"))

used as:

> f- function(x) NULL
> attr(f,"doc") <- "Some text\n\n"
> doc(f)
> doc(f)
Some text

This is pretty primitive, but I suppose you could instead have the
attribute point to something like an HTML file and the doc() function
open it in a web browser, which is basically what R's built-in package
document system does anyway. Except you wouldn't have to build a
package and don't have to learn or follow R's procedures. Which means
you don't get  R's standardization and organization and no one but a
private bunch of users will be able to use your function. But maybe
that's sufficient for your needs.

-- Bert

On Sat, Feb 2, 2013 at 10:01 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> On Sat, Feb 2, 2013 at 6:31 PM, Chee Chen <chen418 at purdue.edu> wrote:
>> Dear All,
>> I would like to ask a question on how to incorporate into an R script help information for the user. I vaguely recall that I saw some instructions on an R manual, but am not able to figure them out. Hereunder is the basic setting:
>> 1.  I finished writing an R script, my_script.r, that is a function (the function is named "my_func") for a statistical procedure. This means that the user only needs to: source("directory_of_script\my_script.r"), then run, my_func(arg1, arg2, ..), to execute the procedure.
>> 2. I would like to add some lines into "my_script.r", so that a user can get information on this file and function by using "?my_func"
> You may wish to create a package from your script; however, if you
> would prefer not then a workaround would be to redefine the ? function
> in your script to provide some help.  For example if you had this line
> in your script:
> `?` <- function(...) if (match.call()[[2]] == "mytopic")
> cat("whatever\n") else help(...)
> then this would work:
>> ?mytopic
> whatever
>> ?dim
> where the last line launches normal help.  If you wish to display a
> file rather than cat strings to the console then use: file.show
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374

More information about the R-help mailing list