Skip to content

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

5 messages · Chee Chen, Jeff Newmiller, Gabor Grothendieck +1 more

#
The normal expectations of an R user is that useful functions you want to share are in packages, which include help files. There is no way to both avoid the package development process and offer help to the user within R. Read the Writing R Extensions document for the most up-to-date information about this process. RStudio has some nice enhancements for making this process a little quicker, but not so much that you can escape reading the documentation I just mentioned. I happen to like using roxygen2 for building help files, but this just means reading even more documentation.

If you are determined to use the source function instead of building a package, then you cannot take advantage of the users' familiarity with the help system. However, you could just put your instructions in comments in your R file and hope they will look there.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
Chee Chen <chen418 at purdue.edu> wrote:

            
#
On Sat, Feb 2, 2013 at 6:31 PM, Chee Chen <chen418 at purdue.edu> wrote:
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:
whatever
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
#
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:
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 Sun, Feb 3, 2013 at 1:50 AM, Bert Gunter <gunter.berton at gene.com> wrote:
To further build on this try the above idea using the comment function:
[1] "Help goes here"

or combine it with the redefinition of ? like like this:

`?` <- function(...) if (!is.null(doc <-
comment(get(match.call()[[2]])))) cat(doc, "\n") else help(...)
?f    #  displays:  Help goes here
?dim   # normal help

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com