Skip to content

[Bioc-devel] A quick questions on writing R functions

5 messages · Li, Dongmei, Elena Grassi, Gabriel Becker

#
Hi,

I'm developing an R package and got the following suggestions for revising the functions:

"When a function returns a named list, a good practise is to start the
  \value section with the following:

    A named list with the following components:

  and then to itemize the components"

This is an example of my current function:

eval<-
  function (ord_vec_t, ord_vec_pvalue, eval_matrix_t, eval_matrix_pvalue, repetition) 
  {
    rowsdim <- length(ord_vec_t)
    p_pvalue <- rep(NA, rowsdim)
    p_tstat <- rep(NA, rowsdim)
    for (g in 1:rowsdim) {
      p_pvalue[g] <- sum(eval_matrix_pvalue[g, ] <= ord_vec_pvalue[g])/repetition
      p_tstat[g] <- sum(abs(eval_matrix_t[g, ]) >= abs(ord_vec_t[g]))/repetition
    }
    mylist <- list(p_pvalue = p_pvalue, p_tstat = p_tstat)
    return(mylist)
  }

Anyone could offer some suggestions on revising the function based on the comments? Thanks so much for all your help!

Best,
Dongmei
#
I would guess that you need to add to the man page of the function in
the value section something like:

A named list with the following components:
\itemize{
   \item \code{"p_pvalue"}: description of p_pvalue
   \item \code{"p_tstat"}: description of p_tstat
}


HTH,
E.
#
Thanks so much for your help!

Best,
Dongmei

-----Original Message-----
From: Bioc-devel [mailto:bioc-devel-bounces at r-project.org] On Behalf Of Elena Grassi
Sent: Friday, February 20, 2015 9:21 AM
To: bioc-devel at r-project.org
Subject: Re: [Bioc-devel] A quick questions on writing R functions

I would guess that you need to add to the man page of the function in the value section something like:

A named list with the following components:
\itemize{
   \item \code{"p_pvalue"}: description of p_pvalue
   \item \code{"p_tstat"}: description of p_tstat }


HTH,
E.

--
$ pom

_______________________________________________
Bioc-devel at r-project.org mailing list
https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_bioc-2Ddevel&d=AwICAg&c=4sF48jRmVAe_CH-k9mXYXEGfSnM3bY53YSKuLUQRxhA&r=azjhQW59umxfk67jsIXeg7AS2ugqKLkykGYJFRwaLto&m=J2T8bBUcSl6vh6O7pYO8JNH6znXfiquU3BzhpWRVcZ8&s=0mcP3kRv5ooga3Yl8OAQtY3zlITMV80ezUsRsshMnos&e=
#
Dongmei,

This isn't really the right list for this type of question. That said, I
can give you a few pointers.

The actual section of the manual you quoted is about documenting your
function, not the code of the function itself.

That said, "eval" is not a good name for your function. It doesn't tell the
caller what the function is going to do, and it will mask the eval function
in base (this is unlikely to actually cause any problems, but it's bad
practice).

This function could also be vectorized if you turn it into a an elementwise
comparsion of two matrices followed by a call to colSums. Even if not, I
suspect that reptition is simply the length of the vectors you pass in? If
so, just call mean on the logical vectors, no need to add them and divide
as a separate step (though admittedly that isn't really going to save you
much).

~G


On Fri, Feb 20, 2015 at 6:10 AM, Li, Dongmei <Dongmei_Li at urmc.rochester.edu>
wrote:

  
    
#
Thanks so much for your comments and help!

Best,
Dongmei

From: Gabe Becker [mailto:becker.gabe at gene.com]
Sent: Friday, February 20, 2015 9:33 AM
To: Li, Dongmei
Cc: bioc-devel at r-project.org
Subject: Re: [Bioc-devel] A quick questions on writing R functions

Dongmei,

This isn't really the right list for this type of question. That said, I can give you a few pointers.

The actual section of the manual you quoted is about documenting your function, not the code of the function itself.

That said, "eval" is not a good name for your function. It doesn't tell the caller what the function is going to do, and it will mask the eval function in base (this is unlikely to actually cause any problems, but it's bad practice).

This function could also be vectorized if you turn it into a an elementwise comparsion of two matrices followed by a call to colSums. Even if not, I suspect that reptition is simply the length of the vectors you pass in? If so, just call mean on the logical vectors, no need to add them and divide as a separate step (though admittedly that isn't really going to save you much).

~G
On Fri, Feb 20, 2015 at 6:10 AM, Li, Dongmei <Dongmei_Li at urmc.rochester.edu<mailto:Dongmei_Li at urmc.rochester.edu>> wrote:
Hi,

I'm developing an R package and got the following suggestions for revising the functions:

"When a function returns a named list, a good practise is to start the
  \value section with the following:

    A named list with the following components:

  and then to itemize the components"

This is an example of my current function:

eval<-
  function (ord_vec_t, ord_vec_pvalue, eval_matrix_t, eval_matrix_pvalue, repetition)
  {
    rowsdim <- length(ord_vec_t)
    p_pvalue <- rep(NA, rowsdim)
    p_tstat <- rep(NA, rowsdim)
    for (g in 1:rowsdim) {
      p_pvalue[g] <- sum(eval_matrix_pvalue[g, ] <= ord_vec_pvalue[g])/repetition
      p_tstat[g] <- sum(abs(eval_matrix_t[g, ]) >= abs(ord_vec_t[g]))/repetition
    }
    mylist <- list(p_pvalue = p_pvalue, p_tstat = p_tstat)
    return(mylist)
  }

Anyone could offer some suggestions on revising the function based on the comments? Thanks so much for all your help!

Best,
Dongmei





_______________________________________________
Bioc-devel at r-project.org<mailto:Bioc-devel at r-project.org> mailing list
https://stat.ethz.ch/mailman/listinfo/bioc-devel<https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_bioc-2Ddevel&d=AwMFaQ&c=4sF48jRmVAe_CH-k9mXYXEGfSnM3bY53YSKuLUQRxhA&r=azjhQW59umxfk67jsIXeg7AS2ugqKLkykGYJFRwaLto&m=qqsvuTQftkkIrTwQWWynG_Y_UWBGtmC2pUjlk0C4Tss&s=I1Rjmnpu9a8_u14tWmTe-mwgwqTItRQOpIy_9uork8Y&e=>



--
Gabriel Becker, Ph.D
Computational Biologist
Genentech Research