Dasonk / docstring

Provides docstring like functionality to R functions without requiring the need to create a package.
57 stars 6 forks source link

Please make docstrings work with question mark. #6

Closed nfultz closed 7 years ago

nfultz commented 7 years ago

If a user-defined function is carrying around a docstring, it would be nice if ?f showed it.

Dasonk commented 7 years ago

It would be. I haven't particularly solved the issue of how to do that. I think the SOS package provides good insight into how to accomplish the task but I haven't had the time to actually implement that yet.

Dasonk commented 7 years ago

Specificially... this https://github.com/cran/sos/blob/master/R/findFn2.R

I wouldn't want to break the current ? by adding my own version in.

Dasonk commented 7 years ago

I've implemented this in commit https://github.com/Dasonk/docstring/commit/8d6a71bd14a80b9405e80867021dc46f6f69cf1a

Dasonk commented 7 years ago

This seems to be working well enough for the time being. Right now ? will only work if the function is defined in the global environment. If a function with docstring is stored in a different environment then you'll need to use docstring directly instead of relying on ? to access the docstring help. I don't see this as a major set-back.

square <-function(x){

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

    return(x^2)
}

e <- new.env()
e$test <- square

?e$test # won't work and can't find a way to make `?` work here
docstring(e$test) # also won't work but can fix it by...
docstring(e$test, "test") # works
Dasonk commented 7 years ago

@nfultz Does this fulfill the use case you were asking for?