tidyverse / magrittr

Improve the readability of R code with the pipe
https://magrittr.tidyverse.org
Other
959 stars 157 forks source link

set_attr and sys.function #271

Closed inferentialist closed 5 months ago

inferentialist commented 5 months ago

First, thank you for an incredibly useful package. I'm a big fan of magrittr and appreciative of the work that you do. This is the first time I've run into unexpected behavior, so here goes.

an edit: it seems like |> gets me the result I expected; so this is probably more appropriately addressed on stackoverflow. apologies!

I was hoping to call set_attr and pass it the result of sys.function. This doesn't work / no attribute is set. I'm using magrittr version 2.0.3.

  library(magrittr)
  f <- function() {
    "hello" %>%
      magrittr::set_attr("g", sys.function())
  }

  r <- f()

  #' this fails as attr(r, "g") returns NULL
  testthat::expect_true(
    is.function(attr(r, "g"))
  )

Experimenting, I found that I could get the expected result using attr<- in lieu of set_attr. Also, assigning to an intermediate variable, phi, in the execution environment, i.e., phi <- sys.function(), before calling set_attr with phi worked. This made me wonder if the issue might be related to #195. It's also possible (likely) that I have the wrong expectations. Please don't hesitate to point me back to any relevant documentation, and thanks for any clarity you can provide.

lionel- commented 5 months ago

This is expected behaviour / a limitation of magrittr. But I see you've already found a couple of workarounds :-)