Search code examples
rcran

R help page as object


Is there a nice way to extract get the R-help page from an installed package in the form of an R object (e.g a list). I would like to expose help pages in the form of standardized JSON or XML schemas. However getting the R-help info from the DB is harder than I thought.

I hacked together a while ago to get the HTML of an R help manual page. However I would rather have a general R object that contains this information, that I can render to JSON/XML/HTML, etc. I looked into the helpr package from Hadley, but this seems to be a bit of overkill for my purpose.


Solution

  • So below what I hacked together. However I yet have to test it on many help files to see if it generally works.

    Rd2list <- function(Rd){
        names(Rd) <- substring(sapply(Rd, attr, "Rd_tag"),2);
        temp_args <- Rd$arguments;
    
        Rd$arguments <- NULL;
        myrd <- lapply(Rd, unlist);
        myrd <- lapply(myrd, paste, collapse="");
    
        temp_args <- temp_args[sapply(temp_args , attr, "Rd_tag") == "\\item"];
        temp_args <- lapply(temp_args, lapply, paste, collapse="");
        temp_args <- lapply(temp_args, "names<-", c("arg", "description"));
        myrd$arguments <- temp_args;
        return(myrd);
    }
    
    getHelpList <- function(...){
        thefile <- help(...)
        myrd <- utils:::.getHelpFile(thefile);
        Rd2list(myrd);
    }
    

    And then you would do something like:

    myhelp <- getHelpList("qplot", package="ggplot2");
    cat(jsonlite::toJSON(myhelp));