[R] R help output in separate window

Jim Lemon jim.lemon at uts.edu.au
Tue Jun 24 03:35:14 CEST 2003


Peter Dalgaard's interest in a different method of help display led me to 
combine R's method of finding the help files, which is much better than the 
search method I initially used, with a system call to display the help file 
in an arbitrary method in another window.

For the original request of using "less" in an xterm, the user would have to 
create a batch file (in *NIX or equivalent method in other OSs) similar to 
the following:

#!/bin/sh
xterm -T $1 -e less $1

which I call "startless", and store it in a file in the user's path. Then 
insert the following lines in something like the .Rprofile file:

options(pager="startless")
source("HDfile")

where HDfile is the path and file name of the following file
(for me, "/home/jim/R/helpdisp.R"):

# retrieves the R help filename corresponding to a particular
# type of display. Defaults to "help" (sort-of-text) and will
# currently cope with HTML and any other display where the
# name of the display program and the directory name of the
# help files are the same.

get.help.filename<-function(topic,display.type="help") {
 lib.loc <- .libPaths()
 packages <- .packages(all.available = TRUE, lib.loc = lib.loc)
 files<-character(0)
 for (lib in lib.loc) {
  for (pkg in packages) {
   INDEX <- system.file(package = pkg, lib.loc = lib)
   file <- index.search(topic, INDEX, "AnIndex",display.type)
   if(length(file) && file != "") files<-c(files,file)
  }
 }
 return(files)
}

# Calls get.help.filename for the name of the relevant help file
# and if a filename is returned, calls the pager, browser or
# other program to display the file. The name of the display
# program must be the same as the directory for the appropriate
# files in any format other than "help" or HTML.

help.display<-function(topic,display.type=c("help","html")) {
 topic=substitute(topic)
 if (is.name(topic))
  topic <- as.character(topic)
 else if (!is.character(topic))
  stop("Unimplemented help feature")
 helpfile<-get.help.filename(topic,display.type)
 if(length(helpfile)) {
  if(display.type == "help")
   system(paste(options("pager"),helpfile,"&"))
  else {
   if(display.type == "html")
    system(paste(options("browser"),helpfile,"&"))
   # if the user has specified a display type, try to run it.
   else {
    if(system(paste(display.type,helpfile,"&")))
     cat("Error starting display type",display.type,"\n")
   }
  }
 }
 else cat("Can't find help for",topic,"\n")
 invisible(helpfile)
}

A lot of this is simply what I hope is the relevant code lifted from the 
help() function. I think that separating the filename finding and display 
calling parts of the function make it a lot easier to follow. I welcome 
suggestions and improvements.

Jim

Feel free to ignore any garbage beneath this line.




DISCLAIMER\ ====================================================... {{dropped}}




More information about the R-help mailing list