[Rd] Problem with dyn.load'ed code
Hi, I am having trouble with some code that I am dyn.loading. I am writing an interface to ARPACK. I compile my interface (dssimp.cc), and link it against the ARPACK library (libarpack_SUN4.a): g++ -shared -static -fPIC dssimp.cc -o dssimp.so -larpack_SUN4 -lg2c -lm I can dyn.load the code and it appears OK. However, when I call my function, the call to the function in the ARPACK library returns an error. I can print the arguments to my function when I call it from R, so I can see that I am dyn.load'd, and the arguments are passed correctly. Moreover, I can load and call the shared object (dssimp.so) using dlopen and dlsym from a stand alone C++ program. So I can see that the shared object is OK. In summary, I have a shared object that works correctly outside of R, but when dyn.load'd into R, does not work. Short of calling errors (which is not the case here) I wonder how that might happen, and how I might fix it? Matt __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] Problem with initialize of S4 classes
Dear all Below is the code for "scriptPreFilter.R" which gives the following result: > source("scriptPreFilter.R") > prefltr <- new("PreFilter", mad=c(0.5,0.01)) [1] "--initialize:PreFilter--" [1] "--initialize:Filter--" > str(prefltr) Formal class 'PreFilter' [package ".GlobalEnv"] with 2 slots ..@ mad :List of 2 .. ..$ cutoff : num 0.5 .. ..$ epsilon: num 0.01 ..@ numfilters: num 1 It seems that everything is ok, the results are as expected. However, my problem is that copying the identical code to my package results in an error: > prefltr <- new("PreFilter", mad=c(0.5,0.01)) [1] "--setValidity:Filter--" Error in validObject(.Object) : invalid class "PreFilter" object: invalid object for slot "mad" in class "PreFilter": got class "numeric", should be or extend class "list" The following code avoids the error and gives the result: > prefltr <- new("PreFilter", mad=list(0.5,0.01)) [1] "--setValidity:Filter--" [1] "--setValidity:PreFilter--" > str(prefltr) Formal class 'PreFilter' [package "xps"] with 11 slots ..@ mad:List of 2 .. ..$ : num 0.5 .. ..$ : num 0.01 ..@ numfilters : num 0 This is only partly correct, e.g. numfilters is 0. Only the following code gives the correct result: > prefltr <- new("PreFilter") > madFilter(prefltr) <- c(0.5,0.01) > str(prefltr) Formal class 'PreFilter' [package "xps"] with 11 slots ..@ mad:List of 2 .. ..$ cutoff : num 0.5 .. ..$ epsilon: num 0.01 ..@ numfilters : num 1 As you see, the loading "scriptPreFilter.R" calls method initialize but not setValidity. In contrast, loading my package as library calls setValidity but not initialize. My question is: - Why can the identical code behave differently when put in a package? - How can I ensure, that initialize gets also called in my package? > sessionInfo() R version 2.6.1 (2007-11-26) i386-apple-darwin8.10.1 locale: C attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] xps_0.4.0 loaded via a namespace (and not attached): [1] rcompgen_0.1-17 Best regards and Merry Christmas Christian _._._._._._._._._._._._._._._._ C.h.i.s.t.i.a.n S.t.r.a.t.o.w.a V.i.e.n.n.a A.u.s.t.r.i.a e.m.a.i.l:cstrato at aon.at _._._._._._._._._._._._._._._._ --- BEGIN: scriptPreFilter.R - setClass("Filter", representation(numfilters = "numeric"), prototype(numfilters = 0) ) setClass("PreFilter", representation(mad = "list"), contains=c("Filter"), prototype(mad = list()) ) setGeneric("madFilter", function(object) standardGeneric("madFilter")) setGeneric("madFilter<-", function(object, value) standardGeneric("madFilter<-")) "initialize.Filter" <- function(.Object, ...) { print("--initialize:Filter--") .Object <- callNextMethod(.Object, ...) .Object } setMethod("initialize", "Filter", initialize.Filter) setValidity("Filter", function(object) { print("--setValidity:Filter--") msg <- NULL if (is.null(msg)) TRUE else msg } ) "initialize.PreFilter" <- function(.Object, mad = list(), ...) { print("--initialize:PreFilter--") [EMAIL PROTECTED] <- 0 if (length(mad)) madFilter(.Object) <- unlist(mad) .Object <- callNextMethod(.Object, ...) .Object } setMethod("initialize", "PreFilter", initialize.PreFilter) setValidity("PreFilter", function(object) { print("--setValidity:PreFilter--") msg <- NULL if (is.null(msg)) TRUE else msg } ) setMethod("madFilter", signature(object="PreFilter"), function(object) [EMAIL PROTECTED] ) setReplaceMethod("madFilter", signature(object="PreFilter", value="numeric"), function(object, value) { if (length(value) == 1) { value[2] <- 0.01 } else if (length(value) != 2) { stop(paste(sQuote("mad"), "must have ")) }#if if (length([EMAIL PROTECTED]) == 0) { [EMAIL PROTECTED] <- [EMAIL PROTECTED] + 1 }#if [EMAIL PROTECTED] <- list(cutoff = as.double(value[1]), epsilon = as.double(value[2])) return(object) } ) --- END: scriptPreFilter.R - __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Problem with dyn.load'ed code
You are missing some extern "C" declarations? R needs C linkage style. Basically a lot of: extern "C" { } around your c++ code. Matt Calder wrote: > Hi, > I am having trouble with some code that I am dyn.loading. I am > writing an interface to ARPACK. I compile my interface (dssimp.cc), and > link it against the ARPACK library (libarpack_SUN4.a): > > g++ -shared -static -fPIC dssimp.cc -o dssimp.so -larpack_SUN4 -lg2c -lm > > I can dyn.load the code and it appears OK. However, when I call my > function, the call to the function in the ARPACK library returns an > error. > I can print the arguments to my function when I call it from R, so I > can see that I am dyn.load'd, and the arguments are passed correctly. > Moreover, I can load and call the shared object (dssimp.so) using > dlopen and dlsym from a stand alone C++ program. So I can see that the > shared object is OK. > In summary, I have a shared object that works correctly outside of R, > but when dyn.load'd into R, does not work. Short of calling errors > (which is not the case here) I wonder how that might happen, and how I > might fix it? > > Matt > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] re quest for addition to R-int
Bjørn-Helge Mevik wrote: > > Ben Bolker wrote: > >> http://cran.r-project.org/doc/manuals/R-ints.html#R-coding-standards >> >> gives detailed advice on how to set the indentation level for >> C code to 4, but it took me a bit of poking around in the archives >> to find the >> >> (setq ess-indent-level 4) >> >> incantation for getting the R indentation right as well. > > I'm confused. Doesn't the suggested code > > (add-hook 'ess-mode-hook >(lambda () > (ess-set-style 'C++) > ... > > implicitly set ess-indent-level to 4? At least for me, using the > suggested code gives indentation steps of 4 in R code. > > -- > Bjørn-Helge Mevik > > > I guess, but since I was using emacs 21 I followed the instructions to use customization instead -- and I guess the customization steps don't include this hook ? Ben -- View this message in context: http://www.nabble.com/request-for-addition-to-R-int-tp14457818p14482966.html Sent from the R devel mailing list archive at Nabble.com. __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Problem with initialize of S4 classes
Hi Christian -- Does your package have a name space, but not export 'initialize'? Calling 'new' will then go directly to the default constructors, generating the error. A different solution would be to define a constructor in your package PreFilter <- function(...) { new("PreFilter", ...) } so that your user would not have to know about the details of calling new, you could provide helpful arguments to your constructor, and 'pre-processing' of arguments can be done (in the constructor) before calling 'new' (I find this helpful, to avoid have to be too careful when constructing initialize methods that deal with both the class and classes derived from the class). Martin cstrato <[EMAIL PROTECTED]> writes: > Dear all > > Below is the code for "scriptPreFilter.R" which gives the following result: > > source("scriptPreFilter.R") > > prefltr <- new("PreFilter", mad=c(0.5,0.01)) > [1] "--initialize:PreFilter--" > [1] "--initialize:Filter--" > > str(prefltr) > Formal class 'PreFilter' [package ".GlobalEnv"] with 2 slots > ..@ mad :List of 2 > .. ..$ cutoff : num 0.5 > .. ..$ epsilon: num 0.01 > ..@ numfilters: num 1 > > It seems that everything is ok, the results are as expected. > > However, my problem is that copying the identical code to my package > results in an error: > > prefltr <- new("PreFilter", mad=c(0.5,0.01)) > [1] "--setValidity:Filter--" > Error in validObject(.Object) : > invalid class "PreFilter" object: invalid object for slot "mad" in > class "PreFilter": got class "numeric", should be or extend class "list" > > The following code avoids the error and gives the result: > > prefltr <- new("PreFilter", mad=list(0.5,0.01)) > [1] "--setValidity:Filter--" > [1] "--setValidity:PreFilter--" > > str(prefltr) > Formal class 'PreFilter' [package "xps"] with 11 slots > ..@ mad:List of 2 > .. ..$ : num 0.5 > .. ..$ : num 0.01 > ..@ numfilters : num 0 > > This is only partly correct, e.g. numfilters is 0. > > Only the following code gives the correct result: > > prefltr <- new("PreFilter") > > madFilter(prefltr) <- c(0.5,0.01) > > str(prefltr) > Formal class 'PreFilter' [package "xps"] with 11 slots > ..@ mad:List of 2 > .. ..$ cutoff : num 0.5 > .. ..$ epsilon: num 0.01 > ..@ numfilters : num 1 > > As you see, the loading "scriptPreFilter.R" calls method initialize but > not setValidity. > In contrast, loading my package as library calls setValidity but not > initialize. > > My question is: > - Why can the identical code behave differently when put in a package? > - How can I ensure, that initialize gets also called in my package? > > > sessionInfo() > R version 2.6.1 (2007-11-26) > i386-apple-darwin8.10.1 > > locale: > C > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > > other attached packages: > [1] xps_0.4.0 > > loaded via a namespace (and not attached): > [1] rcompgen_0.1-17 > > Best regards and Merry Christmas > Christian > _._._._._._._._._._._._._._._._ > C.h.i.s.t.i.a.n S.t.r.a.t.o.w.a > V.i.e.n.n.a A.u.s.t.r.i.a > e.m.a.i.l:cstrato at aon.at > _._._._._._._._._._._._._._._._ > > > --- BEGIN: scriptPreFilter.R - > setClass("Filter", >representation(numfilters = "numeric"), >prototype(numfilters = 0) > ) > > setClass("PreFilter", >representation(mad = "list"), >contains=c("Filter"), >prototype(mad = list()) > ) > > setGeneric("madFilter", function(object) standardGeneric("madFilter")) > setGeneric("madFilter<-", function(object, value) > standardGeneric("madFilter<-")) > > "initialize.Filter" <- function(.Object, ...) > { >print("--initialize:Filter--") >.Object <- callNextMethod(.Object, ...) >.Object > } > setMethod("initialize", "Filter", initialize.Filter) > > setValidity("Filter", >function(object) { > print("--setValidity:Filter--") > msg <- NULL > if (is.null(msg)) TRUE else msg >} > ) > > "initialize.PreFilter" <- function(.Object, mad = list(), ...) > { >print("--initialize:PreFilter--") >[EMAIL PROTECTED] <- 0 >if (length(mad)) madFilter(.Object) <- unlist(mad) >.Object <- callNextMethod(.Object, ...) >.Object > } > setMethod("initialize", "PreFilter", initialize.PreFilter) > > setValidity("PreFilter", >function(object) { > print("--setValidity:PreFilter--") > msg <- NULL > if (is.null(msg)) TRUE else msg >} > ) > > setMethod("madFilter", signature(object="PreFilter"), >function(object) [EMAIL PROTECTED] > ) > > setReplaceMethod("madFilter", signature(object="PreFilter", > value="numeric"), >function(object, value) { > if (length(value) == 1) { > value[2] <- 0.01 > } else if (length(value) != 2) { > stop(paste(sQuote("mad"), "must have ")) > }#if > > if (length([EMAIL PROTECTED]) == 0) { > [EMAIL PROTECTED]