From ripley at stats.ox.ac.uk  Tue Nov  6 21:26:54 2007
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Tue, 6 Nov 2007 20:26:54 +0000 (GMT)
Subject: [Rd] [R] help needed: taking a function out of a package and it
 can not find some funtions
In-Reply-To: <987887.36154.qm@web53705.mail.re2.yahoo.com>
References: <987887.36154.qm@web53705.mail.re2.yahoo.com>
Message-ID: <Pine.LNX.4.64.0711062012270.23714@auk.stats>

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



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