A.Ajakh
2008-Aug-14 23:15 UTC
[R] Documenting a function: How to get the comments in a function similar to args()
Hi All, Imagine that we have a function defined as follows: foo <- # comments line 1 # comments line 2 # etc.. function(x){ x <- 2 } I can use args(foo) to get the arguments for foo. Is there any function to get the comment lines at the beginning of the function?>From what I understand the prompt() function allows one to create a .Rd filewhich can be compiled but I am not there yet. On page 31 of "Programming with Data" (J. Chambers) one can read that the help ? function does what I want but It does not work the same way in R I guess. I use R version 2.7.0 on Win XP. Thank you for your time. AA.
stephen sefick
2008-Aug-15 00:22 UTC
[R] Documenting a function: How to get the comments in a function similar to args()
What are the comments line at the beginning of the function? if you just type in the function name then you get the code, and it seems that R removes the comment lines to make the code more readable. I just looked at a function in a package that I created and know that there are comments in there. If you need the comments I would look in the source package. I believe that if you go to the folder where packages are installed then you can get to the sorces. I think this is what you want. On Thu, Aug 14, 2008 at 7:15 PM, A.Ajakh <aajakh at stonebrook.ws> wrote:> Hi All, > > Imagine that we have a function defined as follows: > foo <- > # comments line 1 > # comments line 2 > # etc.. > function(x){ > x <- 2 > } > I can use args(foo) to get the arguments for foo. Is there any function to > get > the comment lines at the beginning of the function? > >From what I understand the prompt() function allows one to create a .Rd file > which > can be compiled but I am not there yet. > On page 31 of "Programming with Data" (J. Chambers) one can read that the > help ? function does what I want but It does not work the same way in R I > guess. > I use R version 2.7.0 on Win XP. > Thank you for your time. > AA. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis
Gabor Grothendieck
2008-Aug-15 00:28 UTC
[R] Documenting a function: How to get the comments in a function similar to args()
Try this (provided the comments are actually _in_ the function -- in the example you gave they are not).> foo <-+ function(x){ + # comments line 1 + # comments line 2 + # etc.. + x <- 2 + }> > grep("^#", attr(foo, "source"), value = TRUE)[1] "# comments line 1" "# comments line 2" "# etc.." Also be aware of the keep.source option: ?options On Thu, Aug 14, 2008 at 7:15 PM, A.Ajakh <aajakh at stonebrook.ws> wrote:> Hi All, > > Imagine that we have a function defined as follows: > foo <- > # comments line 1 > # comments line 2 > # etc.. > function(x){ > x <- 2 > } > I can use args(foo) to get the arguments for foo. Is there any function to > get > the comment lines at the beginning of the function? > >From what I understand the prompt() function allows one to create a .Rd file > which > can be compiled but I am not there yet. > On page 31 of "Programming with Data" (J. Chambers) one can read that the > help ? function does what I want but It does not work the same way in R I > guess. > I use R version 2.7.0 on Win XP. > Thank you for your time. > AA. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Carl Witthoft
2008-Aug-15 21:40 UTC
[R] Documenting a function: How to get the comments in a function similar to args()
Along the lines of Gabor's method, presumably you could put a "spare" argument in your function: foo<- function(x,help=FALSE,...) { helpout<-c(comment, comment, end of comments) if(something_is_wrong_with_inputs || help=TRUE) { print(helpout) stop #or trycatch, or whatever you prefer } [remainder of function] } I know that's sloppy but I'm new to R. Carl
AAR
2008-Aug-21 18:28 UTC
[R] Documenting a function: How to get the comments in a function similar to args()
Thanks to everyone who responded. It was all very helpful. AA. A.Ajakh wrote:> > Hi All, > > Imagine that we have a function defined as follows: > foo <- > # comments line 1 > # comments line 2 > # etc.. > function(x){ > x <- 2 > } > I can use args(foo) to get the arguments for foo. Is there any function to > get > the comment lines at the beginning of the function? >>From what I understand the prompt() function allows one to create a .Rdfile> which > can be compiled but I am not there yet. > On page 31 of "Programming with Data" (J. Chambers) one can read that the > help ? function does what I want but It does not work the same way in R I > guess. > I use R version 2.7.0 on Win XP. > Thank you for your time. > AA. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- View this message in context: http://www.nabble.com/Documenting-a-function%3A-How-to-get-the-comments-in-a-function-similar-to-args%28%29-tp18991676p19094321.html Sent from the R help mailing list archive at Nabble.com.