Re: [Rd] [R] help needed: taking a function out of a package and it can not find some funtions

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Tue, 6 Nov 2007 20:26:54 +0000 (GMT)

I do wonder when you would consider something a programming question unsuitable for R-help? See the posting guide.

At least my answer needs to be on R-devel.

On Tue, 6 Nov 2007, Jason Liao wrote:

>

> I tried to modify the R function bw.SJ (from the stats package) for my
> own use. But if I just get the source code and run directly (without any
> modification), it can no longer find some key functions called within
> it.

That's not what I find. The missing objects are not functions for me.

> I understand this has something to do with searching path which I do
> not understand well. Can anyone tell me how to modify the source code of
> an R function and still make it part of an existing package?

You don't want to do that. The issue is being in the stats namespace, and if you need to ask how to add a function to that namespace, you don't know enough to be doing so.

You can get away (or at least, I did) with modifying the source of bw.SJ as follows:

     SDh <- function(x, h, n, d) .C(stats:::R_band_phi4_bin, as.integer(n),
         as.integer(length(x)), as.double(d), x, as.double(h),
         u = double(1))$u
     TDh <- function(x, h, n, d) .C(stats:::R_band_phi6_bin, as.integer(n),
         as.integer(length(x)), as.double(d), x, as.double(h),
         u = double(1))$u
     Z <- .C(stats:::R_band_den_bin, as.integer(n), as.integer(nb), d = double(1),
         x, cnt = integer(nb))

Alternatively, you don't need the function to be in the stats namespace, just to have the stats namespace as its environment. Again, that is something that is not recommended, but

environment(my.bw.SJ) <- environment(bw.SJ)

would do it.

-- 
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 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Tue 06 Nov 2007 - 20:43:02 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 07 Nov 2007 - 00:30:15 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.