Small request of a feature improvement in the next version of R

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Small request of a feature improvement in the next version of R

Paul Grosu

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.

Thank you,
Paul

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Small request of a feature improvement in the next version of R

Martyn Plummer-3
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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Small request of a feature improvement in the next version of R

Paul Grosu
Hi Martyn,

I understand now what is happening and thank you for the nice explanation.
I am wondering now, based on the following definition:

"When $<- is applied to a NULL x, it first coerces x to list(). This is what
also happens with [[<- if the replacement value value is of length greater
than one: if value has length 1 or 0, x is first coerced to a zero-length
vector of the type of value."

The following works as expected:

m <- list()
m$"A3V6HVSALQ835D"$'profiles' <- 3
m$"A3V6HVSALQ835D"$'stars' <- c(1, 23)

But if the object encapsulating them (m) is already a list, why would it not
be sensible for any [[<- assignments underneath it be automatically
converted to a list?  It would be nice to have it automatically have m
become a list if one performs m[["A3V6HVSALQ835D"]][['profiles']] <- 3 on
the first assignment - without having to instantiate m to a list (i.e. m <-
list()).  Since R is heavily influenced by SEXP this would be as natural as
a cons().

For instance, I can create a list of functions but not with any other type:

> a <- function(something="Hi") { print( something ) }
> b <- a
> list( a, b )[[1]]()
[1] "Hi"
> list( a, b )[[2]]("there :)")
[1] "there :)"

In fact vectors become lists:

> c(a,b)
[[1]]
function (something = "Hi")
{
    print(something)
}

[[2]]
function (something = "Hi")
{
    print(something)
}

Even matrices become lists:

> as.matrix(c(a,b))
     [,1]
[1,] ?  
[2,] ?  
> as.matrix(c(a,b))[1]
[[1]]
function (something = "Hi")
{
    print(something)
}

> as.matrix(c(a,b))[2]
[[1]]
function (something = "Hi")
{
    print(something)
}

Since all become lists and the assignments are list-like, then it would be
nice to have the children of a list to automatically become a list, even
when they are accessed like a list and the parent (m) is not instantiated as
a list, which should automatically become a list.

I know I wrote a lot, but let me know if I should expand on anything for
clarification purposes.

Thank you,
Paul

-----Original Message-----
From: Martyn Plummer [mailto:[hidden email]]
Sent: Tuesday, November 17, 2015 5:13 AM
To: [hidden email]
Cc: [hidden email]
Subject: Re: [Rd] Small request of a feature improvement in the next version
of R

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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel