[R] Problems with external library conflict in R package
I have an R library (using Rcpp) that used to compile and install fine. It uses some boost libraries. My Makevars file looks like this: PKG_LIBS=`$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"` -lboost_iostreams -lm CXX_STD = CXX14 The problem I face now is that for some other R-independent software project I installed boost 1.71 from source in /usr/local. Since then, the R package does not install anymore; I get: Error: package or namespace load failed (...) dyn.load(file, DLLpath = DLLpath, ...): unable to load shared object (...) XXX.so undefined symbol: _ZN5boost9iostreams6detail10bzip2_base3endEbSt9nothrow_t Both the distribution's includes and library files and the ones I installed in /usr/local are there: $ locate boost/iostreams/filter/bzip2.hpp /usr/include/boost/iostreams/filter/bzip2.hpp /usr/local/include/boost/iostreams/filter/bzip2.hpp $ ldconfig -p | grep boost_ios libboost_iostreams.so.1.71.0 (libc6,x86-64) => /usr/local/lib/libboost_iostreams.so.1.71.0 libboost_iostreams.so.1.65.1 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libboost_iostreams.so.1.65.1 When I als specify -L/usr/local/lib in Makevars, the code installs fine. There is probably a simple answer, but I don't understand why I get this mismatch between library versions. Any help is appreciated! Thanks Pascal __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Global counter in library
Thanks for the hint. I think I will use a big.matrix [with 1 element only ;-) ] and a mutex to ensure access is thread-safe. Pascal On 12.03.19 17:35, Duncan Murdoch wrote: On 11/03/2019 2:57 p.m., Pascal A. Niklaus wrote: I am trying to implement a global counter in a library. This counter resides in an environment local to that package, and is incremented by calling a function within this package. Problems arise when I use parallelization. I protected the increment operation with a lock so that this operation effectively should be atomic. However, the environment local to the package seems not to be shared among the threads that use the functions in that package (not in the sense that a change would be visible in the other threads). How is such a global counter best implemented? Use the Rdsm package. See ?Rdsm::Rdsm for help. Duncan Murdoch Here is a basic sketch of what I tried: library(parallel) library(flock) ## begin of code in package tmp <- tempfile() .localstuff <- new.env() .localstuff$N <- 0 .localstuff$tmp <- tempfile() incr <- function() { h <- lock(.localstuff$tmp) .localstuff$N <- .localstuff$N + 1 unlock(h) } ## end code in library invisible(mclapply(1:10, function(i) incr())) cat("N =", .localstuff$N) __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[R] Global counter in library
I am trying to implement a global counter in a library. This counter resides in an environment local to that package, and is incremented by calling a function within this package. Problems arise when I use parallelization. I protected the increment operation with a lock so that this operation effectively should be atomic. However, the environment local to the package seems not to be shared among the threads that use the functions in that package (not in the sense that a change would be visible in the other threads). How is such a global counter best implemented? Here is a basic sketch of what I tried: library(parallel) library(flock) ## begin of code in package tmp <- tempfile() .localstuff <- new.env() .localstuff$N <- 0 .localstuff$tmp <- tempfile() incr <- function() { h <- lock(.localstuff$tmp) .localstuff$N <- .localstuff$N + 1 unlock(h) } ## end code in library invisible(mclapply(1:10, function(i) incr())) cat("N =", .localstuff$N) __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] data.table: reference column in "i"-part by column name in string object
On 21.02.19 20:43, Rui Barradas wrote: Hello, I don't understand the question. Like this? xx <- "B" yy <- "D" a[xx, on = v] a[c(xx, yy), on = v] Note that .(xx, yy) doesn't work. It outputs something else. Could you give an example of more complicated expressions you have doubts with? Let's assume I'd like to create a subset like this, in data.frame syntax: d[ (d[[v]] == a | d$b == "c") & d$c>2, ] Here I have a column referenced by its name (stored in v), and compared to a value stored in a, and the whole thing is part of a more complex logical expression. I could write it in this conventional way, but I wondered whether there is a better way that would leverage some of data.tables syntax. Pascal __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[R] data.table: reference column in "i"-part by column name in string object
I am converting data.frame-based code to data.table, for performance reasons. Is there a way to refer to columns using expressions in the "i"-part? Here is an example: a <- data.table(x=rep(LETTERS[1:10],each=2), y=1:20) v <- "x" For the j-part, I can access the column whose name is stored in v as a[,..v] However, for the i-part I did not find a good way to achieve the same. For a simple case, the following works xx <- "B" a[xx, on=v] However, I can't see how this is easily expanded to more complex logical expressions. Pascal __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[R] Spacing around \cdot (%.%) unequal in pdf
I am struggling labeling an axis in a plot. Here is a minimal example illustrating the point: pdf("mve.pdf",width=5,height=5) ypos <- c(1:3) * 1e6 ylabs <- sapply(ypos/1e6, function(i) as.expression(bquote(.(i)%.%10^6))) plot(rep(1,3), ypos, yaxt="n", ylab="") axis(2, at=ypos, labels=ylabs, las=1) dev.off() First, I find the space left and right of the \cdot operator rather larger. Is there a way to decrease it? Second, and more important, in the generated PDF, the space left of the dot is much bigger, it looks as if it was typeset as e.g. "3 *10^6". Thanks for any hint Pascal __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Sorting of character vectors
Thanks for all suggestions. With my build (from the CRAN repo) I don't get ICU support, and setting LC_COLLATE to "C" did not help. > capabilities("ICU") ICU FALSE > sessionInfo() R version 3.3.2 (2016-10-31) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 14.04.5 LTS locale: [1] LC_CTYPE=en_DK.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_DK.UTF-8LC_COLLATE=C [5] LC_MONETARY=en_DK.UTF-8LC_MESSAGES=en_DK.UTF-8 [7] LC_PAPER=en_DK.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_DK.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] compiler_3.3.2 tools_3.3.2 However, using stringi::stri_sort did the trick ! __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[R] Sorting of character vectors
I just got caught by the way in character vectors are sorted. It seems that on my machine "sort" (and related functions like "order") only consider characters related to punctuation (at least here the "+" and "-") when there is no difference in the remaining characters: > x1 <- c("-A","+A") > x2 <- c("+A","-A") > sort(x1)# sorting is according to "-" and "+" [1] "-A" "+A" > sort(x2) [1] "-A" "+A" > x3 <- c("-Aa","-Ab") > x4 <- c("-Aa","+Ab") > x5 <- c("+Aa","-Ab") > sort(x3) [1] "-Aa" "-Ab" # here the "+" and "-" are ignored > sort(x4) [1] "-Aa" "+Ab" > sort(x5) [1] "+Aa" "-Ab" I understand from the help that this depends on how characters are collated, and that this scheme follows the multi-level comparison in unicode (http://www.unicode.org/reports/tr10/). However, what I need is a strict left-to-right comparison of the sort provided by strcmp or wcscmp in glibc. The particular ordering of special characters is not so important, but there should be no "multi-level" aspect to the sorting. Is there a way to achieve this in R? Thanks for your help Pascal __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] import of data set and warning from R CMD check
Dear Thierry, Thanks, that indeed solved the warning. Still, I think the message from R CMD check is confusing since the hint seems not to work with the current R version. Pascal On 2016-06-21 11:31, Thierry Onkelinx wrote: Dear Pascal, You could try to use data(CO2, package = "datasets") instead of data(CO2) Best regards, ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance Kliniekstraat 25 1070 Anderlecht Belgium To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey 2016-06-17 17:45 GMT+02:00 Pascal A. Niklaus <pascal.nikl...@ieu.uzh.ch <mailto:pascal.nikl...@ieu.uzh.ch>>: Hi all, When checking an R package, I get: |Consider adding importFrom("datasets","CO2") (this data set is used in some example code) However, when I add the suggested 'importFrom' statement to NAMESPACE (using roxygen2), I get | |Error :object ‘CO2’ is not exported by 'namespace:datasets'| || |I understand that datasets are not exported, and the comment printed by 'R CMD check' seems not to have any consequences, but it nevertheless seems inconsistent to me. But maybe I miss something here... Pascal | -- Dr. Pascal A. Niklaus Department of Evolutionary Biology and Environmental Studies University of Zurich Winterthurerstrasse 190 CH-8057 Zurich / Switzerland __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[R] import of data set and warning from R CMD check
Hi all, When checking an R package, I get: |Consider adding importFrom("datasets","CO2") (this data set is used in some example code) However, when I add the suggested 'importFrom' statement to NAMESPACE (using roxygen2), I get | |Error :object ‘CO2’ is not exported by 'namespace:datasets'| || |I understand that datasets are not exported, and the comment printed by 'R CMD check' seems not to have any consequences, but it nevertheless seems inconsistent to me. But maybe I miss something here... Pascal | -- Dr. Pascal A. Niklaus Department of Evolutionary Biology and Environmental Studies University of Zurich Winterthurerstrasse 190 CH-8057 Zurich / Switzerland [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[R] error in lmerTest after updating to R 3.3.0
Dear all, After updating to R 3.3.0 (inadvertently, via apt-get), I get an error when using lmerTest. Here is an example: library(lmerTest) library(MASS) data(oats) m <- lmer(Y ~ N*V + (1|B/V), data=oats) summary(m) summary from lme4 is returned some computational error has occurred in lmerTest I have removed all old libraries and tried to have as clean an installation as possible, but this did not fix the problem. Is this an incompatibility or a problem with my installation? Thanks for your help Pascal __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[R] Keep factor order in lmer
I would like to keep a specific order of fixed effects in a model passed to lmer. In particular, I would like to prevent that interactions are automatically moved after all main effects. In aov and lme, this is possible with terms(..., keep.order=TRUE). Unfortunately, I have not found a way to achieve this behaviour in lmer. Here is an example that illustrates the problem: d <- data.frame( R=factor(rep(1:4,each=8)), A=factor(rep(1:2,each=4)), B=factor(rep(1:2,each=2)), C=factor(1:2)) d$y <- rnorm(nrow(d)) ## example using aov, with intended output summary(aov(terms(y~A*B+C,keep.order = TRUE),data=d)) Df Sum Sq Mean Sq F value Pr(>F) A1 0.831 0.8308 0.832 0.370 B1 0.356 0.3557 0.356 0.556 A:B 1 0.103 0.1035 0.104 0.750 C1 0.992 0.9919 0.993 0.328 Residuals 27 26.970 0.9989 ## works also in lme anova(lme(terms(y~A*B+C,keep.order=TRUE),random=~1|R,data=d)) ## but how do I achieve this in lmer? anova(lmer(y~A*B+C+(1|R),data=d)) Of course, I could recode the interactions into new single factors, but that seems rather cumbersome. Thanks for your help Pascal __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[R] Rcpp module / class access -- answer
Hi all, I am trying to answer my own question (although it probably does not end up in the thread because I am not a subscribed and thus have to send a new mail). The way I finally got the code working was: In NAMESPACE, comment out the following lines # importFrom(Rcpp, loadModule) # importFrom(Rcpp, evalCpp) In zzz.R: # loadModule(mod_yada,TRUE); .pkgNamespace - environment() .onLoad - function(libname, pkgname) { assign(mod_yada, Module(mod_yada,PACKAGE=testPackage,mustStart=TRUE), envir=.pkgNamespace); } The object generation then works as expected in the S4 class code. Not sure whether this is the best way to achieve this, though. Pascal __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[R] Rcpp module / class access
Dear all, I am struggling accessing a class created in an Rcpp module. The structure of the package is essentially the one created using: Rcpp.package.skeleton(name=testPackage,module=TRUE) Now, after loadiong the package with library(testPackage), I can create instances of the World class as follows: library(testPackage) mod_yada-Rcpp::Module(yada,PACKAGE=testPackage,mustStart=TRUE) w - new(mod_yada$World); So far so good. However, I fail defining a S4 wrapper class within the same package. As part of that, I would like to define a generator function that returns an instance of mod_yada$World, as in the code above: However, yada$World is not accessible. I then tried inserting the following line in the respective R file (although I thought this was already taken care by the loadModule(yada, TRUE) that was places in zzz.R by the skeleton generator: mod_yada-Rcpp::Module(yada,PACKAGE=testPackage,mustStart=TRUE) But again, this fails with: Failed to initialize module pointer: Error in FUN(_rcpp_module_boot_mod_yada[[1L]], ...): no such symbol _rcpp_module_boot_mod_yada in package testPackage Putting this file after zzz.R in the Collate: list in the DESCRIPTION file also does not help. How can I define this in the Rcpp package itself? Thanks for any hint Pascal Niklaus __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[R] Coordinate or top left corner + offset
Dear all, I am struggling to add annotations to panels of a series of plots arranged on a page. Basically, I'd like to add letters enumerating the panels (a,b,c,...), at a fixed distance from the top left corner of the plot's box. I succeeded partly with mtext (see below), but the at option is in user coordinates, which makes is difficult to specify a given offset from the corner (e.g. 1cm from top and left). I tried grid's npc but these coordinates refer to the entire plot instead of the current inner plotting region. Phrased differently, I'd like to place text (and ideally also be able to plot, e.g. a white disc to cover background items) at position (top-1cm,left+1cm) Here is a minimum working example illustrating what I try to achieve: pdf(example.pdf,width=15,height=15) m - rbind( c(0.1,0.9,0.1,0.6), c(0.1,0.9,0.6,0.9) ); split.screen(m) screen(1); par(mar=c(0,0,0,0)); plot(rnorm(10),rnorm(10),xlim=c(-5,5),xaxt=n,yaxt=n); mtext(quote(bold(a)),side=3,line=-2.5,at=-5,cex=2.5) screen(2); par(mar=c(0,0,0,0)); plot(rnorm(10),rnorm(10),xlim=c(-3,3),xaxt=n,yaxt=n); mtext(quote(bold(a)),side=3,line=-2.5,at=-3,cex=2.5) close.screen(all.screens=TRUE) dev.off() Thanks for your help Pascal Niklaus __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[R] lme model specification problem (Error in MEEM...)
Dear all, In lme, models in which a factor is fully contained in another lead to an error. This is not the case when using lm/aov. I understand that these factors are aliased, but believe that such models make sense when the factors are fitted sequentially. For example, I sometimes fit a factor first as linear term (continuous variable with discrete levels, e.g. 1,2,4,6), and then as factor, with the latter then accounting for the deviation from linearity. If x contains the values 1,2,4,6, the model formula then would be y ~ x + as.factor(x) A complete example is here: library(nlme) d - data.frame(x=rep(c(1,2,4,6),each=2),subj=factor(rep(1:16,each=2))) d$y - rnorm(32) + rnorm(length(levels(d$subj)))[d$subj] anova(lme(y~x,random=~1|subj,data=d)) ## works anova(lme(y~as.factor(x),random=~1|subj,data=d))## works anova(lme(y~x+as.factor(x),random=~1|subj,data=d)) ## fails: # Error in MEEM(object, conLin, control$niterEM) : # Singularity in backsolve at level 0, block 1 summary(aov(y~x+as.factor(x)+Error(subj),data=d)) ## works: # Error: subj # Df Sum Sq Mean Sq F value Pr(F) # x 1 8.434 8.434 4.780 0.0493 * # as.factor(x) 2 10.459 5.230 2.964 0.0900 . # Residuals12 21.176 1.765 # ... rest of output removed ... I understand I can to some extent work around this limitation by modifying the contrast encoding, but I then still don't get an overall test for as.factor(x) (in the example above, a test for deviation from linearity). d$xfac - factor(d$x) contrasts(d$xfac)-c(1,2,4,6) summary(lme(y~xfac,random=~1|subj,data=d)) Is there a way to work around this limitation of lme? Or do I mis-specify the model? Thanks for your advice. Pascal __ R-help@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.
[R] nlrob problem
Dear all, I am not sure if this mail is for R-help or should be sent to R-devel instead, and therefore post to both. While using nlrob from package 'robustbase', I ran into the following problem: For psi-functions that can become zero (e.g. psi.bisquare), weights in the internal call to nls can become zero. Example: d - data.frame(x=1:5,y=c(2,3,5,10,9)) d.nlrob - nlrob(y ~ k*x,start=list(k=1),data=d,psi=psi.bisquare) I think the problem is as follows: After the call to nls, a weighted residual vector is calculated by dividing by sqrt(w). This generates NaN's for weights of zero, which leads to problems in the subsequent call to nls during the next iteration: for (iiter in 1:maxit) { ... w - psi(resid/Scale, ...) ... data$w - sqrt(w) ... out - nls( ., data=data ... ) ... resid - -residuals(out)/sqrt(w) # NaN for w=0 ... } I wonder whether this problem shouldn't be dealt with by setting 'w' to 0 for the NaN cases. I can get a fit by calling nlrob with na.action=na.exclude, but I'd have intuitively assumed that na.action applies to the NAs in the data set passed to nlrob, not to the one internally generated by nlrob and passed to nls. The second 'issue' is that the weights are passed as 'w', whereas the documentation of 'nls' says weights should be given as 'weights' : data: an optional data frame in which to evaluate the variables in ‘formula’ and ‘weights’. Can also be a list or an environment, but not a matrix. I think it would be good to mention in the documentation of 'nls' that weights can be passed as 'w' as well. Pascal Niklaus __ R-help@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.
[R] rpanel
Dear all, I am struggling to align textentry fields in a Tcl/Tk widget. In the example below, I'd like to have the boxes aligned. library(rpanel) panel - rp.control(title=title,size=c(100,100)) rp.textentry(panel,var=a,labels=Variable A, initval=1,pos=list(row=0,column=0)) rp.textentry(panel,var=b,labels=Var. B, initval=1,pos= list(row=1,column=0)) Thanks for your help Pascal -- Pascal A. Niklaus Institute of Evolutionary Biology and Environmental Studies University of Zurich Winterthurerstrasse 190 CH-8057 Zurich / Switzerland __ R-help@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.
[R] modify ... (optional args)
Hi all, Is there a way to modify the optional arguments (...) passed to a function, so that these can be passed in modified form to a subsequent function call? I checked Programming with Data but could not find a solution there. What I'd like is something along these lines: test - function(x,y,...) { if(!hasArg(xlab)) { ___add xlab to ...___ } if(hasArg(xlab)) { ___remove xlab from ...___ } # alternative plot(x,y,...) } Of course, I could use separate calls, like if(hasArg(xlab)) plot(x,y,...) else plot(x,y,xlab=label,...) but this gets complicated if there is more than one such case to consider. Thanks Pascal -- Pascal A. Niklaus Institute of Evolutionary Biology and Environmental Studies University of Zurich Winterthurerstrasse 190 CH-8057 Zurich / Switzerland __ R-help@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.
[R] undefined slot classes in package code
Dear all, I am facing a problem (warning message) when building a package that I am unable to fix: * checking whether package ‘myRcppTest’ can be installed ... WARNING Found the following significant warnings: Warning: undefined slot classes in definition of S4test: Rcpptest(class Rcpp_test) The package (originally generated with Rcpp.package.skeleton) contains a class named test, in module test (see the *.cpp file below). This class works fine (I can generate and use objects created with new(test$test)). To extend the functionality of this class, I want to wrap a S4 class (named S4test in the minimal example below) around the Rcpp class. The idea is to store an instance of the Rcpp-class in a slot (see R file below), and provide S4 methods that make use of this internal object. The problem seems to be that during the check/install process test$test is not known when this file is processed. However, I could not figure out so far how to make test$test known within the context of this file... Thanks for your help Pascal --- cpp file #include Rcpp.h using namespace Rcpp ; RCPP_MODULE(test) { class_test(test) .constructor() .field(value, test::val) ; } test::test() {} test::~test() {} -- R file - setClass(S4test, representation( Rcpptest=Rcpp_test) ); setMethod(f=initialize, signature=S4test, definition=function(.Object) { .Object@Rcpptest - new(test$test); .Object; } ); newS4test - function() { new(S4test); } - zzz.R - .NAMESPACE - environment() test - new( Module ) .onLoad - function(libname, pkgname) { unlockBinding( test , .NAMESPACE ) assign( test, Module( test ), .NAMESPACE ) lockBinding( test, .NAMESPACE ) } .onAttach - function(libname, pkgname) {} .onUnload - function(libname, pkgname) {} NAMESPACE useDynLib(myRcppTest) exportPattern(^[[:alpha:]]+) import( Rcpp ) -- Pascal A. Niklaus Institute of Evolutionary Biology and Environmental Studies University of Zurich CH-8057 Zurich / Switzerland __ R-help@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.
[R] Problem with setMethod in R package
Dear all, My apologies for re-posting this question, but I have not found any solution to my problem so far. I also think that my post may have been overseen due to the posting time and high traffic on this list. I experience a problem in implementing a S4 class in a package and would appreciate your help in this matter. The class is implemented using Rcpp, which works well. I then extend the class with R functions, adding them as methods with a call to setMethod(...). When I do this outside the package (after loading the package with library(...)), everything works smoothly. However, when I try to incorporate the call to setMethod(...) in the package code itself, the only way I get it to work is to include the call in .onLoad(...) or .onAttach(...). This works, but when loading the library I get the messages creating new generic function for plot in .GlobalEnv, as well as no definition for class Rcpp_rothC... Again, the code works, but the messages let me presume that I add the methods in the wrong way or miss-specify name spaces, class names, or something else. Thank you for your advice. Pascal (The error message and the relevant parts of the package files are pasted below, together with my related questions as comments.) showMethods(plot) Function plot: not a generic function # I am surprised about this -- # why isn't plot a generic function? library(RrothC2) Loading required package: Rcpp Loading required package: pascal Creating a new generic function for plot in .GlobalEnv in method for ‘plot’ with signature ‘x=Rcpp_rothC,y=missing’: no definition for class Rcpp_rothC # class seems not to be known # at this stage. # but where should I put setMethod()? r1 - new(rothC$rothC); class(r1)# class is known by now [1] Rcpp_rothC attr(,package) [1] .GlobalEnv plot(r1) # plot is dispatched correctly showMethods(plot) Function: plot (package graphics) # now plot from graphics shows up x=ANY, y=ANY # why was it missing before? x=Rcpp_rothC, y=missing # FILE rcpp_rothC_module.h # class rothC { ... } # FILE rcpp_rothC_module.cpp # using namespace Rcpp ; RCPP_MODULE(rothC) { class_rothC(rothC) .constructor() ... } # FILE rcpp_rothC.R # Rcpp_rothC.plot - function(x,y,...) { ... } ## FILE zzz.R ## .NAMESPACE - environment() rothC - new( Module ) .onLoad - function(libname, pkgname) { unlockBinding( rothC , .NAMESPACE ) assign( rothC, Module( rothC ), .NAMESPACE ) setMethod(f=plot,signature(x=Rcpp_rothC,y=missing),definition=Rcpp_rothC.plot,where=.GlobalEnv); lockBinding( rothC, .NAMESPACE ) } .onAttach - function(libname, pkgname) {} .onUnload - function(libname, pkgname) { gc(); } __ R-help@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.
[R] setMethod call in package code using Rcpp
Dear all, I experience a problem in implementing a S4 class in a package and would appreciate your help in this matter. The class is implemented using Rcpp, which works well. I then extend the class with R functions, adding them as methods with a call to setMethod(...). When I do this outside the package (after loading the package with library(...)), everything works smoothly. However, when I try to incorporate the call to setMethod(...) in the package code itself, the only way I get it to work is to include the call in .onLoad(...) or .onAttach(...). This works, but when loading the library I get the messages creating new generic function for plot in .GlobalEnv, as well as no definition for class Rcpp_rothC... Again, the code works, but the messages let me presume that I add the methods in the wrong way or miss-specify name spaces, class names, or something else. Thank you for your advice. Pascal (The error message and the relevant parts of the package files are pasted below, together with my related questions as comments.) showMethods(plot) Function plot: not a generic function # I am surprised about this -- # why isn't plot a generic function? library(RrothC2) Loading required package: Rcpp Loading required package: pascal Creating a new generic function for plot in .GlobalEnv in method for ‘plot’ with signature ‘x=Rcpp_rothC,y=missing’: no definition for class Rcpp_rothC # class seems not to be known # at this stage. # but where should I put setMethod()? r1 - new(rothC$rothC); class(r1)# class is known by now [1] Rcpp_rothC attr(,package) [1] .GlobalEnv plot(r1) # plot is dispatched correctly showMethods(plot) Function: plot (package graphics) # now plot from graphics shows up x=ANY, y=ANY # why was it missing before? x=Rcpp_rothC, y=missing # FILE rcpp_rothC_module.h # class rothC { ... } # FILE rcpp_rothC_module.cpp # using namespace Rcpp ; RCPP_MODULE(rothC) { class_rothC(rothC) .constructor() ... } # FILE rcpp_rothC.R # Rcpp_rothC.plot - function(x,y,...) { ... } ## FILE zzz.R ## .NAMESPACE - environment() rothC - new( Module ) .onLoad - function(libname, pkgname) { unlockBinding( rothC , .NAMESPACE ) assign( rothC, Module( rothC ), .NAMESPACE ) setMethod(f=plot,signature(x=Rcpp_rothC,y=missing),definition=Rcpp_rothC.plot,where=.GlobalEnv); lockBinding( rothC, .NAMESPACE ) } .onAttach - function(libname, pkgname) {} .onUnload - function(libname, pkgname) { gc(); } __ R-help@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.
[R] NA and NaN question
Hi all, I ran into a problem in some of my code that could be traced back to 'mean' sometimes returning NA and sometimes NaN, depending on the value of na.rm: mean(c()) [1] NA mean(c(NA),na.rm=T) [1] NaN However, I don't understand the reasoning behind this and would appreciate and explanation. I understand that the mean of an empty vector is not definied, but I don't understand why it matters whether the vector was empty from the beginning or only after removing the NAs. Pascal Niklaus __ R-help@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.
Re: [R] NA and NaN question
Thanks, now I understand what's happening. Maybe a line explaining this could be added to the help text for mean? Pascal Niklaus On Wed 07-Jan-2009 13:29:25 Prof Brian Ripley wrote: Pascal A. Niklaus wrote: Hi all, I ran into a problem in some of my code that could be traced back to 'mean' sometimes returning NA and sometimes NaN, depending on the value of na.rm: mean(c()) [1] NA mean(c(NA),na.rm=T) [1] NaN However, I don't understand the reasoning behind this and would appreciate and explanation. I understand that the mean of an empty vector is not definied, Not so, it is well-defined as 0/0 = NaN. but I don't understand why it matters whether the vector was empty from the beginning You didn't try that case: mean(numeric(0)) is also NaN. The issue is that typeof(c()) [1] NULL is not numeric (not evan a vector), and so mean() of it is undefined. or only after removing the NAs. Speculation (and wrong). Pascal Niklaus __ R-help@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. __ R-help@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.
[R] xyplot problem
Hi all, I am trying to produce some panels with dots in an X/Y plane where the diameter of the dots indicates a Z value (like e.g. earthquake maps where dot sizes indicate magnitudes and X/Y the location). This works fine with xyplot, e.g.: xyplot(1:3~1:3,cex=1:3,pch=16) However, when I do this with a panel variable, e.g.: x-rep(1:3,5) y - rep(1:3,5) sz - rep(1:5,each=3) grp - factor(rep(1:5,each=3)) xyplot(y~x | grp , cex=sz) then sz in the cex argument is not applied per group as I would expect. Same for other arguments like col. Is this really the intended behaviour? How can I achieve what I want, i.e., that the cex argument is different for the different levels of grp? Thank you for your help Pascal Niklaus __ R-help@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.