Custom bracket filter `[` in R6 class

Hello,

I'm playing around with R6 classes in R and am trying to define custom behaviour when using single bracket subsetting. Like x[1:5]

I would like to be able to check all the arguments that go in the brackets and perform additional steps before evaluating them. However, I'm getting weird behaviour:

#Dummy class
Foo = R6::R6Class(
  'Foo',
  public = list(
    x = NULL,
    initialize = function(x) {
      self$x = x
    },
    
    myFun = function(...) {
      return(as.list(match.call()))
    },
    
    `[` = function(...) {
      return(as.list(match.call()))
    }
  )
)
`[.Foo`    <- function(obj, ...) obj$`[`(...) 

x = Foo$new(1)
#Expected list of arguments when not using the `[` function
x$myFun(1:5,5,)
#> [[1]]
#> x$myFun
#> 
#> [[2]]
#> 1:5
#> 
#> [[3]]
#> [1] 5
#> 
#> [[4]]
#Weird output when using custom `[` functionality
x[1:5,5,]
#> [[1]]
#> obj$`[`
#> 
#> [[2]]
#> ..1
#> 
#> [[3]]
#> [1] 5
#> 
#> [[4]]
#> ..3

Created on 2023-08-20 with reprex v2.0.2

My aim is to check the arguments and depending on the type or function have different behaviour, but with this ..1 notation I cannot see what the content of the arguments are. Any idea how I can get the output that comes with the myFun for the [?

Thanks!
PJ

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.