Removing an element from a lazy_dots list

137 views
Skip to first unread message

hong...@revolutionanalytics.com

unread,
Jun 24, 2015, 9:50:16 PM6/24/15
to manip...@googlegroups.com
Is there an approved way of removing an element from an object of class lazy_dots?

require(lazyeval)

f <- function(a, ...)
lazy_dots(...)

lst <- f(1, x=2, y=3, z=4)

lst$z <- NULL
#Error in UseMethod("as.lazy") : 
#  no applicable method for 'as.lazy' applied to an object of class "NULL"

lst[["z"]] <- NULL
# this works

lst <- lst[-which(names(lst) == "z")]
# this also works


Setting the list element to NULL using the $ operator gives an error; but using [[ indexing works. I don't know if either behaviour will change in the future though. Using a negative index with which() also works, but that's rather clumsy.

I'm using lazyeval 0.1.10.


Hadley Wickham

unread,
Jun 25, 2015, 2:41:26 AM6/25/15
to hong...@revolutionanalytics.com, manipulatr
I think lst[["z"]] <- NULL is probably best.
Hadley
> --
> You received this message because you are subscribed to the Google Groups
> "manipulatr" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to manipulatr+...@googlegroups.com.
> To post to this group, send email to manip...@googlegroups.com.
> Visit this group at http://groups.google.com/group/manipulatr.
> For more options, visit https://groups.google.com/d/optout.



--
http://had.co.nz/

hong...@revolutionanalytics.com

unread,
Jun 26, 2015, 1:59:53 AM6/26/15
to manip...@googlegroups.com
Thanks Hadley. Can I assume that this won't change in the future?

Note that the error with lst$z can be fixed by defining a method for as.lazy.NULL:

as.lazy.NULL <- function(x, ...) NULL

This would give the same behaviour as using [[ indexing.

Hadley Wickham

unread,
Jul 1, 2015, 11:39:44 PM7/1/15
to hong...@revolutionanalytics.com, manipulatr
That doesn't seem like consistent behaviour to me.
Hadley

hong...@revolutionanalytics.com

unread,
Jul 4, 2015, 8:59:45 AM7/4/15
to manip...@googlegroups.com, hong...@revolutionanalytics.com
I'm not sure what you mean. It would be consistent with the behaviour of [[<-, which deletes the element. It would also be consistent with the common use of assigning a value of NULL to delete from a list. (I think the latter isn't great semantics honestly, but it's what we've got.)
Reply all
Reply to author
Forward
0 new messages