[R] How to modify a S4 function (in the package NADA)

Sarah Goslee sarah.goslee at gmail.com
Thu Nov 15 15:10:38 CET 2012


Hi,

Your reproducible example isn't reproducible for me, as I get an error message.

But regardless, the easiest thing to do is write your own wrapper,
something like:

mycenfit <- function(x, y) {
  result <- cenfit(x, y)
  c(result, sum = result$n * result$mean)
}

No need to change anything in the package itself. Incidentally, using
attach() is generally a really bad idea. with() is much safer.

Sarah

On Thu, Nov 15, 2012 at 7:00 AM, maxbre <mbressan at arpa.veneto.it> wrote:
> I want to get access to the code of an S4 method in order to possibly modify
> a function to accomplish my particular needs: in my case the function in is
> cenfit() from the package NADA
>
> Now, given my reproducible example:
>
> my.ex<-structure(list(TEC = c(0.21, 0.077, 0.06, 0.033, 0.014, 0.0072), LR =
> c(0L, 0L, 1L, 0L, 1L, 0L)), .Names = c("TEC", "LR"), class = "data.frame",
> row.names = c(NA, -6L))
>
> and the following few lines of code:
>
> library(“NADA)
>
> attach(my.ex)
>
> cenfit(TEC,LR)
>
> giving this output:
>
>   n      n.cen         median       mean      sd
> 6.00     2.00              0.033           0.058       0.083
>
> I would like to add one more result to the above, namely “sum”, very simply
> computed as the product of “n” times “mean”
>
> Do you think that is possible or convenient?
>
> I’ve been reading that editing a S4 methods needs particular attention as by
> using showMethods("cenfit") and then getMethod("cenfit"); in fact, I’ve been
> trying that but without much success (i.e. understanding of the cumbersome
> output)
>
> can anyone give me some help on how is probably better to proceed in order
> to get my new result?
>
> different alternative solutions or hints or advices are also more than
> welcomed
> (I’m pretty new on R and specially on the field of function handling and
> customizing)
>
> all the best
>
> max
>
>
>



--
Sarah Goslee
http://www.functionaldiversity.org




More information about the R-help mailing list