Skip to content
Prev 205535 / 398506 Next

Building static HTML help pages in R 2.10.x on Windows

Thanks, tools:::fetchRdDB() was the clue I needed.  I understand the
frightening triple colon means this bit of magic is internal, and
likely to change at a moment's notice.  Using it wasn't exactly
trivial, either: see the code below for how I generated the static
HTML for all installed packages.

Since wanting static HTML help pages for bookmarking or reading while
R is not running sounds kind of reasonable, it would be nice if
something like this were available for Windows users of R.  It would
be a shame if everybody else had to figure this out in detail, too.

Again, thanks for the pointer.  R is fun again! :-)

------------------------------
library("tools")                                       # fetchRdDB(), Rd2HTML(), et al.

makeStaticHTMLHelp <- function(libs = .libPaths(), verbose = TRUE) {
  maybeCat <- function(msg, ...) { if (verbose) cat(sprintf(msg, ...)) }

  subDirectories <- function(d) {                      # Directories under d (not files!)
    basename(rownames(subset(file.info(dir(path = d, full.names = TRUE)), subset = isdir)))
  }                                                    #
  
  makeHTML <- function(pkgRdDB, lib, pkg, key, links) {# Write key's doc to an html file 
    Rd2HTML(pkgRdDB[[key]],                            #   extract doc from Rd data
            out            = file.path(lib, pkg, "html", paste(key, "html", sep = ".")),
            package        = pkg,                      #   use this pkg's name
            Links          = links,                    #   use HTML links if non-null
            no_links       = is.null(links),           #   no links if arg is null
            stages         = c("install","render"),    #   run appropriate Sexpr forms
            outputEncoding = "",                       #   native encoding of this system
            dynamic        = FALSE)                    #   this is, of course, static
  }                                                    #

  ## *** NB: make.packages.html() prints a message even if verbose == FALSE?!
  maybeCat(if (make.packages.html(lib.loc = libs, verbose = verbose))
             "done.\n"                                 # Succeeded updating packages.html file
           else                                        # But success is apparently not always 
             "FAILED?!\n")                             #  guaranteed!

  maybeCat("Finding HTML links... ")                   # Find HTML links between doc pages
  links <- findHTMLlinks()                             #  just 1ce, outside the loops below
  maybeCat("found %d links... done.\n", length(links)) #
  
  sapply(libs, function(lib) {                         # Map over libraries
    maybeCat("Generating static HTML for packages in library %s...\n", lib)
    sapply(subDirectories(lib), function(pkg) {        #   Map over packages in this library
      maybeCat("  package %s... ", pkg)                #   Fetch pkg's Rd docs (for ALL keys)
      tryCatch({                                       #   In case can't read .rdb file
        pkgRdDB <- tools:::fetchRdDB(file.path(lib, pkg, "help", pkg), key = NULL)
        sapply(names(pkgRdDB), function(key) {         #     Map over keys in this pkg's docs
          tryCatch(makeHTML(pkgRdDB, lib, pkg, key, links),
                   error = function(e) {               #     If error, retry w/o links
                     maybeCat("retrying %s without links... ", key)
                     tryCatch(makeHTML(pkgRdDB, lib, pkg, key, NULL),
                              error = function(e) { maybeCat("FAILED without links?! ") })
                   })                                  #
        })                                             #     Done with this key
      }, error = function(e) { maybeCat("Couldn't read .rdb file?! ") })
      maybeCat("done.\n")                              #   Done with this package
    })                                                 # Done with this library
  })                                                   # Done.

  invisible(NA)                                        # Return nothing of interest
}                                                      # 
------------------------------

Thread (28 messages)

Uwe Ligges Building static HTML help pages in R 2.10.x on Windows Jan 2 Steve Rowley Building static HTML help pages in R 2.10.x on Windows Jan 6 Dieter Menne Building static HTML help pages in R 2.10.x on Windows Jan 7 Michal Kulich Building static HTML help pages in R 2.10.x on Windows Jan 7 Duncan Murdoch Building static HTML help pages in R 2.10.x on Windows Jan 7 Michael Friendly Building static HTML help pages in R 2.10.x on Windows Jan 7 Duncan Murdoch Building static HTML help pages in R 2.10.x on Windows Jan 7 Michal Kulich Building static HTML help pages in R 2.10.x on Windows Jan 7 Gabor Grothendieck Building static HTML help pages in R 2.10.x on Windows Jan 7 Duncan Murdoch Building static HTML help pages in R 2.10.x on Windows Jan 7 Duncan Murdoch Building static HTML help pages in R 2.10.x on Windows Jan 7 Michal Kulich Building static HTML help pages in R 2.10.x on Windows Jan 7 Duncan Murdoch Building static HTML help pages in R 2.10.x on Windows Jan 7 Jonathan Baron Building static HTML help pages in R 2.10.x on Windows Jan 7 Duncan Murdoch Building static HTML help pages in R 2.10.x on Windows Jan 7 Kevin Wright Building static HTML help pages in R 2.10.x on Windows Jan 7 Duncan Murdoch Building static HTML help pages in R 2.10.x on Windows Jan 7 Kevin Wright Building static HTML help pages in R 2.10.x on Windows Jan 7 Duncan Murdoch Building static HTML help pages in R 2.10.x on Windows Jan 7 Hadley Wickham Building static HTML help pages in R 2.10.x on Windows Jan 7 Hadley Wickham Building static HTML help pages in R 2.10.x on Windows Jan 7 Michal Kulich Building static HTML help pages in R 2.10.x on Windows Jan 7 Dieter Menne Building static HTML help pages in R 2.10.x on Windows Jan 8 Duncan Murdoch Building static HTML help pages in R 2.10.x on Windows Jan 8 Duncan Murdoch Building static HTML help pages in R 2.10.x on Windows Jan 8 Dieter Menne Building static HTML help pages in R 2.10.x on Windows Jan 8 Hadley Wickham Building static HTML help pages in R 2.10.x on Windows Jan 8 Dieter Menne Building static HTML help pages in R 2.10.x on Windows Jan 9