Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a sensible way to do something like docstrings in R? [closed]

This is not just a coding style question. If you know python (and I think also Ruby has something like this), you can have a docstring in a function, such that you can readily get that string by issuing a "help" command. e.g.:

def something(t=None):     '''Do something, perhaps to t      t : a thing         You may not want to do this     '''     if t is not None:         return t ** 2     else:         return 'Or maybe not' 

Then help(something) returns the following:

Help on function something in module __main__:  something(t=None)     Do something, perhaps to t      t : a thing         You may not want to do this 

The way things work in R, you can get the full text of the defined code snippet, so you could see comments (including those at the beginning of the function), but that can be a lot of scrolling and visual filtering. Is there any better way?

like image 414
Dav Clark Avatar asked May 09 '11 00:05

Dav Clark


People also ask

Does R have docstrings?

The docstring package is a package for R that provides the ability to display something analagous to Python's docstrings within R.

What are the three types of docstrings?

Let us know the most commonly used docstring formats out there in the wild, which are namely- Google, NumPy, and Sphinx docstring formats.

Are docstrings necessary?

Docstrings are not necessary for non-public methods, but you should have a comment that describes what the method does. This comment should appear after the "def" line.

Do all functions need docstrings?

Every function you create ought to have a docstring. They're in triple-quoted strings and allow for multi-line text.


2 Answers

I recently wrote a package to do just this task. The docstring package allows one to write their documentation as roxygen style comments within the function they are documenting. For example one could do

square <- function(x){     #' Square a number      return(x^2) } 

and then to view the documentation either call the docstring function

docstring(square) 

or use the built in ? support and do

?square 

The comments can either be a single chunk like shown above or fully roxygen style to take advantage of some of the keywords provided

square <- function(x){      #' Square a number     #'     #' Calculates the square of the input     #'     #' @param x the input to be squared      return(x^2) } 

This is on CRAN now: https://cran.r-project.org/package=docstring so you can just install using

install.packages("docstring") 

or if you want the latest development version you can install from github:

library(devtools) install_github("dasonk/docstring") 
like image 88
Dason Avatar answered Sep 20 '22 15:09

Dason


You can add any attributes you like to R objects, including function. So something like

describe <- function(obj) attr(obj, "help") foo <- function(t=NULL) ifelse(!is.null(t), t^2, "Or maybe not") attr(foo, "help") <- "Here is the help message" 

produces more or less the desired output

> foo(2) [1] 4 > foo() [1] "Or maybe not" > describe(foo) [1] "Here is the help message" 
like image 21
chl Avatar answered Sep 22 '22 15:09

chl