[Rd] Recursive indexing can cause R-1.8.0 (and R-1.7.1) to segment fault (PR#4486)

anaximander at gmx.de anaximander at gmx.de
Thu Oct 9 23:34:44 MEST 2003


Recursive indexing can cause R-1.8.0 (and R-1.7.1) to segment fault

First of all, many thanks to the R team!!
R is really a software for everyday work.
Yes, I've found a fault, but I hope it's not just faultfinding ;-)

In the NEWS file of R-1.8.0, first printed in R NEWS 1.7.1,
there was given a promise: [ 1 ]
  o       Recursive indexing of lists is allowed, so x[[c(4,2)]] is
          shorthand for x[[4]][[2]] etc.  (Wishlist PR#1588)

That's not fulfilled in all cases, although there was already a
code correction in R-1.8.0: [ 2 ]
   o       Recursive indexing of lists had too little error-checking.
           (related to PR#3324)

Recursive indexing can cause R-1.8.0 / R-1.7.1 / R-1.7.0
(operating system: linux, compiler: gcc 3.3.1 or 2.95.3)
to produce a segment fault.

Suppose we have the following code:
(be aware: it crashes R!)
###################################################################
# R code from Volkmar Klatt, email: volkmar.klatt AT stud.uni-bayreuth.de
# just make a nested list: 
erzeugeTestListe <- function( laenge )
{ 
Liste <- list()
for (i in 1: laenge )
   {
   hilf <- round(runif(1)*20) +1 
   X <- runif( hilf )
   Y <- runif( hilf )^2
   summeX <- sum(X)
   summeY <- sum(Y)
   Liste <- c(Liste, list(list( summe=list(X=summeX, Y=summeY),
punkte=list(X=X, Y=Y))) )
   }
# make clean:
rm( hilf, summeX, summeY,  X,Y, i )
return( Liste )
} # end of erzeugeTestListe()
#
#
Liste <- erzeugeTestListe(25)            #0
#
# Now, e.g., we want to extract all X-und Y-elements of Nr. 7-12 and 18,20
# and to write them into a vector A:
A <- Liste[[ c(7:12,18,20) ]]$summe$X    #1 ...good bye!!
#
#
#    __
#   //##)
#  //  o  -  Wow, 
#   /  \       fantastic, that's really a short R-session ...
#
###################################################################
##
## We can achieve the same result with lapply:
# x <- c( 7:12, 18,20 )
# lapply(  Liste[[ c(x,1)  ]] , FUN=print ) #2 ...good bye!
#
#
# Remark:
# Very seldom, R-1.8.0 does not crash, but gives the following
# output:
# Error: recursive indexing failed at level 3
# You then should just make an other list Liste (line #0)
# and try line (#1) or (#2) again. 
###################################################################

Of coarse, R is not intended to be an ingenious data base program,
which should be able to handle, let's say, complicated requests 
like they are possible in SQL-language.

On the other side, lists are becoming more and more important in R,
so there *should* be a convenient way to deal with nested lists, shan't it?
Sure, all of us can avoid such lists in our own data, but we often
have to use data files from other people with *have* a nested structure.
(Thats the way I discovered this error.)

Perhaps we can find a fair compromise?

Have a nice day,
Volkmar Klatt
anaximander AT gmx.de
or
volkmar.klatt AT stud.uni-bayreuth.de

Bibliography:
[ 1 ] http://www.cran.r-project.org/src/base/NEWS , line 1116 f.
[ 2 ] ibidem, line 774 f.
###########################################################################




-- 
NEU FÜR ALLE - GMX MediaCenter - für Fotos, Musik, Dateien...
Fotoalbum, File Sharing, MMS, Multimedia-Gruß, GMX FotoService

Jetzt kostenlos anmelden unter http://www.gmx.net

+++ GMX - die erste Adresse für Mail, Message, More! +++



More information about the R-devel mailing list