Function commenting conventions in R
Asked Answered
L

4

27

I'm fairly new to R, and I have been defining some of my own functions in script files. I'm intending for others to re-use them later, and I can't find any guides on R function commenting conventions. Is there any way for me to make help("my_function_name") show some help? If not, do I just document the function in the script file, so that someone has to print out (or open the source of) a script to see the comments?

Thanks,

Hamy

Lynx answered 12/6, 2011 at 20:47 Comment(0)
P
23

Updating this question December 2019 as the R-universe has changed since 2011 when originally written

My recommended resource is now http://r-pkgs.had.co.nz/

Original answer (links are mostly out of date)

The canonical way to document your functions and make them accessible to others is to make a package. In order for your package to pass the build checks, you have to supply sufficiently detailed help files for each of your functions / datasets.

Check out http://cran.r-project.org/doc/manuals/R-exts.html#Creating-R-packages

This blog post from Rob J Hyndman was very useful and one of the easiest for me to follow: http://robjhyndman.com/researchtips/building-r-packages-for-windows/

I've started using roxygen to assist in making & compiling packages as of late: http://roxygen.org/

Lots of good resources and people to help when you have questions!

Pejorative answered 12/6, 2011 at 20:56 Comment(3)
The link to the blog post of Rob J Hyndman is broken. Alternatively, the pdf can now be found here.Unrig
The 2nd and 3rd urls are 404Mcmahan
@Mcmahan - I added a new reference that's accurate as of 2019...impressive this question still gets traction nearly a decade later.Pejorative
P
19

Another (and lower key) alternative you could look into are the comment() and attr() functions to add some meta data to your functions. Here's a quick and silly example:

FOO <- function(x,y) {
 x + y 
}

attr(FOO, "comment") <- "FOO performs simple addition"

#This can be arbitrary. "comment" is special. see ?comment for details.
attr(FOO, "help") <- "FOO expects two numbers, and it will add them together"

You can then see everything associated with FOO by using attributes():

> attributes(FOO)
$source
[1] "function(x,y) {" " x + y "         "}"              

$comment
[1] "FOO performs simple addition"

$help
[1] "FOO expects two numbers, and it will add them together"

Or extract specific parts:

> attr(FOO, "help")
[1] "FOO expects two numbers, and it will add them together"
attr(FOO, "comment")
[1] "FOO performs simple addition"

And in the case of comment, use comment():

> comment(FOO)
[1] "FOO performs simple addition"

In the long term, writing your own package will almost certainly be worth the overhead and time investment, but if for some reason that isn't practical in the short term - here's another option.

Pejorative answered 12/6, 2011 at 22:10 Comment(1)
+1 I do this all the time, especially for saved workspaces and functions that aren't overly complicated but you want to know "what they do" quickly.Prostomium
A
7

You will have to put functions into a package (which makes porting function REALLY easy). I have written a short post about it a while ago with links (I hope they still work) to some relevant documents that expand the subject.

You can generate help files "on the fly" using roxygen, inlinedocs.

Aerate answered 12/6, 2011 at 20:54 Comment(1)
Is that a roxygen echo? echo?Pejorative
I
1

If you are not building a package, you can use docstring package, which offers similar functions as Python's 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)
}

You can see the comments by calling ?square. enter image description here output

This tutorial might be helpful.

Infracostal answered 3/11, 2023 at 22:35 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.