Laurent Gautier
2009-Nov-19 07:40 UTC
[Rd] Issue when calling deparse(substitute(x)) from C with "anonymous" R vectors ?
Dear list, When calling R from C, what appears like a spurious error can be triggered during the execution of chisq.test(x, y). This is happening when the following conditions are met: - x and y are "anonymous" C-level R vectors (they do not have a symbol), but they are protected from garbage collection - x and y are "not too small" (it was experienced as soon as they are longer than 17 elements). The error is Error in names(dimnames(x)) <- DNAME : 'names' attribute [4] must be the same length as the vector [2] and can be traced to the use of deparse(substitute(x)) and deparse(substitute(y)) in the R code. What seems to be happening is that the deparse(substitute(x)) call gives a character vector of length > 1 as soon as x is "not so small". To demonstrate this, I am using rpy2 (as the problem was found when using it http://sourceforge.net/mailarchive/forum.php?thread_name=4B043CA1.9050500%40salilab.org&forum_name=rpy-list ), but it will likely be present in other bridges to R as well. #using R-2.10 and rpy2-2.1.dev >>> import rpy2.robjects as robjects >>> f = robjects.r('''function(x) return(deparse(substitute(x)))''') >>> tuple(f(robjects.FloatVector(range(17)))) ('c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)',) # length 1 >>> tuple(f(robjects.FloatVector(range(18)))) ('c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17', ')') # length 2 !! Does it seem to others like an issue present in other bridges as well ? L.
Peter Dalgaard
2009-Nov-19 08:37 UTC
[Rd] Issue when calling deparse(substitute(x)) from C with "anonymous" R vectors ?
Laurent Gautier wrote:> Dear list, > > > When calling R from C, what appears like a spurious error can be > triggered during the execution of chisq.test(x, y). > > This is happening when the following conditions are met: > > - x and y are "anonymous" C-level R vectors (they do not have a symbol), > but they are protected from garbage collection > > - x and y are "not too small" (it was experienced as soon as they are > longer than 17 elements). > > > The error is > > Error in names(dimnames(x)) <- DNAME : > 'names' attribute [4] must be the same length as the vector [2] > > and can be traced to the use of deparse(substitute(x)) and > deparse(substitute(y)) in the R code. > > What seems to be happening is that the deparse(substitute(x)) call > gives a character vector of length > 1 as soon as x is "not so small". > > To demonstrate this, I am using rpy2 (as the problem was found when > using it > http://sourceforge.net/mailarchive/forum.php?thread_name=4B043CA1.9050500%40salilab.org&forum_name=rpy-list > > ), but it will likely be present in other bridges to R as well. > > > #using R-2.10 and rpy2-2.1.dev >>>> import rpy2.robjects as robjects > >>>> f = robjects.r('''function(x) return(deparse(substitute(x)))''') > >>>> tuple(f(robjects.FloatVector(range(17)))) > ('c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)',) > # length 1 >>>> tuple(f(robjects.FloatVector(range(18)))) > ('c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17', ')') > # length 2 !! > > > Does it seem to others like an issue present in other bridges as well ? >It can be generated from R itself:> x <- sample(0:1,2000,replace=T) > y <- sample(0:1,2000,replace=T) > eval(substitute(chisq.test(x,y),list(x=x,y=y)))Error in names(dimnames(x)) <- DNAME : 'names' attribute [252] must be the same length as the vector [2] Looks like nlines=1 is needed in the deparse(). -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Prof Brian Ripley
2009-Nov-19 08:40 UTC
[Rd] Issue when calling deparse(substitute(x)) from C with "anonymous" R vectors ?
This is easy to reproduce in R: chisq.test(c(1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011), c(1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011)) The simple answer is: don't do that. It is unclear what is a reasonable label to give in such a case: maybe simply 'x' and 'y'? On Thu, 19 Nov 2009, Laurent Gautier wrote:> Dear list, > > > When calling R from C, what appears like a spurious error can be triggered > during the execution of chisq.test(x, y). > > This is happening when the following conditions are met: > > - x and y are "anonymous" C-level R vectors (they do not have a symbol), > but they are protected from garbage collection > > - x and y are "not too small" (it was experienced as soon as they are longer > than 17 elements). > > > The error is > > Error in names(dimnames(x)) <- DNAME : > 'names' attribute [4] must be the same length as the vector [2] > > and can be traced to the use of deparse(substitute(x)) and > deparse(substitute(y)) in the R code. > > What seems to be happening is that the deparse(substitute(x)) call > gives a character vector of length > 1 as soon as x is "not so small". > > To demonstrate this, I am using rpy2 (as the problem was found when using it > http://sourceforge.net/mailarchive/forum.php?thread_name=4B043CA1.9050500%40salilab.org&forum_name=rpy-list > ), but it will likely be present in other bridges to R as well. > > > #using R-2.10 and rpy2-2.1.dev >>>> import rpy2.robjects as robjects > >>>> f = robjects.r('''function(x) return(deparse(substitute(x)))''') > >>>> tuple(f(robjects.FloatVector(range(17)))) > ('c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)',) > # length 1 >>>> tuple(f(robjects.FloatVector(range(18)))) > ('c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17', ')') > # length 2 !! > > > Does it seem to others like an issue present in other bridges as well ? > > > > L. > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- 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
Duncan Murdoch
2009-Nov-19 11:32 UTC
[Rd] Issue when calling deparse(substitute(x)) from C with "anonymous" R vectors ?
Can you put together a minimal, self contained example, not requiring an external system? This has the symptoms of a bug in the external code (e.g. a missing PROTECT), but it is possible it's a bug in R. Duncan Murdoch On 19/11/2009 2:40 AM, Laurent Gautier wrote:> Dear list, > > > When calling R from C, what appears like a spurious error can be > triggered during the execution of chisq.test(x, y). > > This is happening when the following conditions are met: > > - x and y are "anonymous" C-level R vectors (they do not have a symbol), > but they are protected from garbage collection > > - x and y are "not too small" (it was experienced as soon as they are > longer than 17 elements). > > > The error is > > Error in names(dimnames(x)) <- DNAME : > 'names' attribute [4] must be the same length as the vector [2] > > and can be traced to the use of deparse(substitute(x)) and > deparse(substitute(y)) in the R code. > > What seems to be happening is that the deparse(substitute(x)) call > gives a character vector of length > 1 as soon as x is "not so small". > > To demonstrate this, I am using rpy2 (as the problem was found when using it > http://sourceforge.net/mailarchive/forum.php?thread_name=4B043CA1.9050500%40salilab.org&forum_name=rpy-list > ), but it will likely be present in other bridges to R as well. > > > #using R-2.10 and rpy2-2.1.dev > >>> import rpy2.robjects as robjects > > >>> f = robjects.r('''function(x) return(deparse(substitute(x)))''') > > >>> tuple(f(robjects.FloatVector(range(17)))) > ('c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)',) > # length 1 > >>> tuple(f(robjects.FloatVector(range(18)))) > ('c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17', ')') > # length 2 !! > > > Does it seem to others like an issue present in other bridges as well ? > > > > L. > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel