From |uc@r @end|ng |rom |edor@project@org  Sun Nov  3 22:22:26 2019
From: |uc@r @end|ng |rom |edor@project@org (=?UTF-8?Q?I=C3=B1aki_Ucar?=)
Date: Sun, 3 Nov 2019 22:22:26 +0100
Subject: [Rd] Puzzled about a new method for "[".
In-Reply-To: <74a6cf10-3266-fef8-9ea0-0ba6d36db807@auckland.ac.nz>
References: <74a6cf10-3266-fef8-9ea0-0ba6d36db807@auckland.ac.nz>
Message-ID: <CALEXWq10AdjthU+qWtJx1V1CRhfLaTDXgaLF9gQOAiN7icM5pQ@mail.gmail.com>

On Sun, 3 Nov 2019 at 22:12, Rolf Turner <r.turner at auckland.ac.nz> wrote:
>
>
> I recently tried to write a new method for "[", to be applied to data
> frames, so that the object returned would retain (all) attributes of the
> columns, including attributes that my code had created.
>
> I thrashed around for quite a while, and then got some help from Rui
> Barradas who showed me how to do it, in the following manner:
>
> `[.myclass` <- function(x, i, j, drop = if (missing(i)) TRUE else
> length(cols) == 1)[{
>     SaveAt <- lapply(x, attributes)
>     x <- NextMethod()
>     lX <- lapply(names(x),function(nm, x, Sat){
>       attributes(x[[nm]]) <- Sat[[nm]]
>       x[[nm]]}, x = x, Sat = SaveAt)
>     names(lX) <- names(x)
>     x <- as.data.frame(lX)
>     x
> }
>
> If I set class(X) <- c("myclass",class(X)) and apply "[" to X (e.g.
> something like X[1:42,]) the attributes are retained as desired.
>
> OK.  All good.  Now we finally come to my question!  I want to put this
> new method into a package that I am building.  When I build the package
> and run R CMD check I get a complaint:
>
> ... no visible binding for global variable ?cols?
>
> And indeed, there is no such variable.  At first I thought that maybe
> the code should be
>
> `[.myclass` <- function(x, i, j, drop = if (missing(i)) TRUE else
>                                        length(j) == 1)[{
>
> But I looked at "[.data.frame" and it has "cols" too; not "j".
>
> So why doesn't "[.data.frame" throw a warning when R gets built?
>
> Can someone please explain to me what's going on here?

The thing is...

test <- function(x = y * 2) {
  y <- 1
  x
}

test()
# 2

Lazy evaluation magic.

I?aki



[Rd] Puzzled about a new method for "[".
=?UTF-8?Q?I=C3=B1aki_Ucar?=