Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create documentation for an R project

I would like to create documentation for my R code. The code is a part of an R project but not of a package. Is there a way I can display documentation for code not in a package using the built-in help viewer without creating a full package?

like image 207
amarchin Avatar asked Jul 10 '26 06:07

amarchin


1 Answers

You can use roxygen style comments above your functions if you're fine with keeping your documentation in the code files directly. You wouldn't easily be able to view you documentation using the typical ?your_function syntax though. There might be a way around that with some hacks to generate the documentation and place them somewhere in the help search path but that seems like more work than necessary.

If you're willing to include the roxygen2 style documentation inside the function you can get the nice ?your_function syntax to view the documentation if you're willing to load the docstring package. It's a package I wrote pretty much exactly for your use case - where you have code you want to document but haven't either taken the time or don't care to place it in a package. I suggest either reading the README on the github page for docstring or view the vignette provided on the cran page for docstring.

Here's an example session using docstring:

library(docstring)

square <- function(x){

    #' Square a number
    #'
    #' Calculates the square of the input
    #'
    #' @param x the input to be squared

    return(x^2)
}

# This will display the documentation for square
# just like any other help file would be displayed
?square
like image 169
Dason Avatar answered Jul 13 '26 10:07

Dason