Skip to content
Prev 51276 / 63435 Next

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

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:
[1] "Hi"
[1] "there :)"

In fact vectors become lists:
[[1]]
function (something = "Hi") 
{
    print(something)
}

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

Even matrices become lists:
[,1]
[1,] ?   
[2,] ?
[[1]]
function (something = "Hi") 
{
    print(something)
}
[[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:plummerm at iarc.fr] 
Sent: Tuesday, November 17, 2015 5:13 AM
To: pgrosu at gmail.com
Cc: r-devel at r-project.org
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:
R.
order:
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:
NULL

So the expression
is equivalent to:
Inspecting the result:
$A3V
profile 
      3
[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:
...

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