[Rd] language: bug or feature: vector-subscript of list (PR#1558)

mark.bravington@csiro.au mark.bravington@csiro.au
Wed, 15 May 2002 08:54:53 +0200 (MET DST)


Full_Name: Mark Bravington
Version: R1.3.1 & R1.5.0
OS: Windows 2000
Submission from: (NULL) (140.79.2.3)


R doesn't like the use of subscripts with length > 1, to get into recursive
lists:

> listio_ list( a=list( b=9, c='hello'), d=1:5)
> listio[[ c( 1, 2)]]
Error: attempt to select more than one element
> 

S is more relaxed:

test>  listio_ list( a=list( b=9, c='hello'), d=1:5)
test> listio[[ c(1,2)]]
[1] "hello"
test> 

The same goes for assignment into recursive lists.

I find this feature essential, so I eventually wrote the workarounds below. But
I'm sure these are slow and ungainly.

Could this feature not be added as a primitive?

cheers
Mark
assign( '[[', function( var, ...) {
  pg_ .Primitive( '[[')
  vv_ as.name( 'var')
  for( i in c(...))
    vv_ call( 'pg', vv, i)
  eval( vv)
})

assign( '[[<-', function( var, ..., value) {
  i_ c( ...)
  if( length( i)<2)
return( .Primitive( '[[<-')( var, i, value))

  pa_ .Primitive( '[[<-')
  pg_ .Primitive( '[[')
  vario_ as.name( 'var')
  for( ii in i[ -length(i)])
    vario_ call( 'pg', vario, ii)

#  print( vario)

  callio_ substitute( value, envir=parent.frame())
  for( ii in rev( i)) {
    callio_ call( 'pa', vario, ii, callio)
    if( length( vario)>1)
      vario_ vario[[ 2]]
  }
#  print( callio)
return( eval( callio))
})




-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._