[R] two problem in writing R functions

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Tue Jul 2 13:25:42 CEST 2002


On Tue, 2 Jul 2002, vito muggeo wrote:

> Hi all,
> Although I'm able to manage functions in R, because I'm writing my first
> package I'd like to optmize them, but I'm not so expert to do it and I spent
> a lot of my time without success. So I was wonder whether someone could give
> me some advice. Let the function
> myfn<-function(glm.obj,....){ #glm.obj is a glm object
> .....
> class(out)<- c("myclass",class(obj))
> out}
>
>  I'm stuck on the following two problem:
> 1) My function needs global assignment (otherwise it doesn't work)
> Instead of using "<<-", I used something like
> assign("X",x,envir=sys.frame()) #to build
> rm(X,envir=sys.frame()) #to delete
> However if there is some X variable in the workspace, it is deleted too. (In
> practice it works as the simple "<<-" and this should be avoided, of
> course).
> I could use .X but the problem isn't yet solved

This should not be necessary.  What are you doing that appears to need
global assignment?

> 2)I'd like that summary.myclass() works like summary.glm, printing further
> information from the obj. Following summary.negbin() in MASS library from
> V&R:
> summary.myclass<-function(x,dispersion=NULL,correlation=FALSE){
> cat(".....")
> summ<-c(summary.glm(x,dispersion=dispersion, correlation=correlation),
> x[c("name1","name2")])
> summ
> }
> but it doesn't work! The obj is printed with all its component!

c() removes the attributes.  summary.negbin added a class and you do not.
Note also that there is print.summary.negbin.

> $call
> $terms
> $family
> ....
> $name1
> $name2
>
> Where am I wronging?
> Thank you very much,
> vito
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list