Jason Liao
2007-Nov-06 19:08 UTC
[R] help needed: taking a function out of a package and it can not find some funtions
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. 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? Thanks. Jason Liao, http://www.geocities.com/jg_liao Associate Professor of Biostatistics Drexel University School of Public Health 245 N. 15th Street, Mail Stop 660 Philadelphia, PA 19102-1192 phone 215-762-3934
Katharine Mullen
2007-Nov-06 20:22 UTC
[R] help needed: taking a function out of a package and it can not find some funtions
bw.SJ calls C code from the file /usr/local/bin/R-2.6.0/src/library/stats/src/bandwidths.c You have to make this code available. You can do the following: 1. copy bandwidths.c and the definition of bw.SJ (the latter renamed) to a directory 2. compile bandwidths.c into a shared library with the command R CMD SHLIB bandwidths.c (see the manual writing R extensions for details) 3. in your definition of bw.SJ, in the 3 places .C is called, remove "R_" from the first argument and quote it; e.g., C(R_band_phi4_bin, ..." becomes "C("band_phi4_bin"," 4. in R, load the shared library you built with the dyn.load function; now your definition of the (renamed) function bw.SJ can be modified as you like. 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. 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? > > Thanks. > > > Jason Liao, http://www.geocities.com/jg_liao > Associate Professor of Biostatistics > Drexel University School of Public Health > 245 N. 15th Street, Mail Stop 660 > Philadelphia, PA 19102-1192 > phone 215-762-3934 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Prof Brian Ripley
2007-Nov-06 20:26 UTC
[Rd] [R] help needed: taking a function out of a package and it can not find some funtions
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
Jason Liao
2007-Nov-06 20:50 UTC
[R] help needed: taking a function out of a package and it can not find some funtions
Simple solution provide by Prof. Ripley is to modify the relevant code as follows (he posted in the R-development group): 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)) It works great with minimum extra effort. Thanks.
Possibly Parallel Threads
- help needed: taking a function out of a package and it can not find some funtions
- fastest way to compute the squared Euclidean distance between two vectors in R
- how much performance penalty does this incur, scalar as a vector of one element?
- extremely slow recursion in R?
- Linking R with Fortran 90: make: m2c: Command not found