[R] Make package out of own function

Uwe Ligges ligges at statistik.tu-dortmund.de
Sun Apr 8 17:57:39 CEST 2012


Export the default method (and otehr methods) from your NAMESPACE? See 
Writing R Extensions about the S3method directive.

Note that print.f is the print method for a class f, while f is a 
generic function, both fs are unrelated.

Uwe Ligges




On 02.04.2012 12:02, Johannes Radinger wrote:
> Hello,
>
> I already posted that on stackoverflow[1], but although it's crossposting,
> I think this question can probably easier to be answered by other R-users on this list, which maintain packages etc.
>
> I would like to make a package out of a function. The function
> is working in a script, but when I install and load it as library()
> I get an error. The example-function is:
>
> #Make generic function
> f<- function(x,...) UseMethod("f")
>
> #Default method
> f.default<- function(a,b=5,c=3,...){
> 	out<- a+b+c
> 	class(out)<- "f"
> 	out
> }
>
> # Print method
> print.f<- function(x,...){
> 	cat("Result for f: ")
> 	print(unclass(x))
> }
>
> In the NAMESPACE for export I set "f".
> When I try to run the function from the package I get:
> Error in UseMethod("f") :
>    no applicable method for 'f' applied to an object of class "c('double', 'numeric')"
>
> here some additional info:
>> sessionInfo()
> R version 2.14.2 (2012-02-29)
> Platform: i386-apple-darwin9.8.0/i386 (32-bit)
>
> locale:
> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
> [1] fishmove_0.0-1 plyr_1.7.1     ggplot2_0.9.0
>
> loaded via a namespace (and not attached):
>   [1] colorspace_1.1-1   dichromat_1.2-4    digest_0.5.1       grid_2.14.2        MASS_7.3-17
>   [6] memoise_0.1        munsell_0.3        proto_0.3-9.2      RColorBrewer_1.0-5 reshape2_1.2.1
> [11] scales_0.2.0       stringr_0.6
>>
>
>
> Can anyone explain me how to solve that resp. how to make my own function into a package?
>
> Best regards,
>
> Johannes
>
> [1]: http://stackoverflow.com/questions/8430178/arguments-and-classes-for-writing-generic-functions-in-r
>



More information about the R-help mailing list