From plummerm at iarc.fr  Tue Nov 17 11:13:11 2015
From: plummerm at iarc.fr (Martyn Plummer)
Date: Tue, 17 Nov 2015 10:13:11 +0000
Subject: [Rd] Small request of a feature improvement in the next version
 of R
In-Reply-To: <0c3401d120d4$db715bd0$92541370$@gmail.com>
References: <0c3401d120d4$db715bd0$92541370$@gmail.com>
Message-ID: <1447755191.19134.45.camel@iarc.fr>

On Mon, 2015-11-16 at 20:11 -0500, Paul Grosu wrote:
> Hi Everyone,
> 
> Sorry to bother the list with this small request, but I've run into this
> issue and was wondering if it could be fixed in the next version of R.
> Sorry if it was raised in a previous thread:
> 
> So when I try the following I get an error:
> 
> > m <- list()
> > m[["A3V6HVSALQ835D"]][['profiles']] <- 3
> > m[["A3V6HVSALQ835D"]][['stars']] <- c(1, 23)
> Error in m[["A3V6HVSALQ835D"]][["stars"]] <- c(1, 23) : 
>   more elements supplied than there are to replace
>   
> As does the following:
> 
> > m <- list()
> > m[["A3V6HVSALQ835D"]][['profiles']] <- c()
> > m[["A3V6HVSALQ835D"]][['stars']] <- c()
> > m[["A3V6HVSALQ835D"]][['profiles']] <- 3
> > m[["A3V6HVSALQ835D"]][['stars']] <- c(1, 23)
> Error in m[["A3V6HVSALQ835D"]][["stars"]] <- c(1, 23) : 
>   more elements supplied than there are to replace
> 
> But when I reverse the order, I don't:
> 
> > m <- list()
> > m[["A3V6HVSALQ835D"]][['stars']] <- c(1, 23)
> > m[["A3V6HVSALQ835D"]][['profiles']] <- 3
> 
> As doesn't the following, with the order reversed for the assignment:
> 
> > m <- list()
> > m[["A3V6HVSALQ835D"]][['profiles']] <- c()
> > m[["A3V6HVSALQ835D"]][['stars']] <- c()
> > m[["A3V6HVSALQ835D"]][['stars']] <- c(1, 23)
> > m[["A3V6HVSALQ835D"]][['profiles']] <- 3
> 
> And when I instantiate it in this way, it does not with the original order:
> 
> > m <- list()
> > m[["A3V6HVSALQ835D"]][['profiles']] <- c()
> > m[["A3V6HVSALQ835D"]][['stars']] <- list()
> > m[["A3V6HVSALQ835D"]][['profiles']] <- 3
> > m[["A3V6HVSALQ835D"]][['stars']] <- c(1, 23)
> 
> The request is so that order-specific assignments would not throw an error,
> and I am using version 3.2.2 of R.

Your example combines two nested calls to the replacement function
"[[<-". It is a lot easier to understand what is going wrong if you
break this down into two separate function calls.

First, the element of m that you want to modify is NULL:

> m <- list()
> m[["A3V"]]
NULL

So the expression 

> m[["A3V"]][['profile']] <- 3

is equivalent to:

> tmp <- NULL
> tmp[['profile']] <- 3
> m[["A3V"]] <- tmp

Inspecting the result:

> m
$A3V
profile 
      3 

> class(m$A3V)
[1] "numeric"

So m$A3V is a numeric vector and not, as you expected, a list. This
behaviour of "[[<-" when applied to NULL objects is documented on the
help page: See help("[[<-")

The solution is to create m[["A3V"]] as a list before modifying its
elements:

> m <- list()
> m[["A3V"]] <- list()
...

Martyn

> Thank you,
> Paul
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-----------------------------------------------------------------------
This message and its attachments are strictly confidenti...{{dropped:8}}



[Rd] Small request of a feature improvement in the next version of R
Martyn Plummer