Using method from one class within method for other class

Tjebo

I am trying to use a method for one class (data.frame) within a method for another class (list), in this case a list of several data frames.

This does not work, and I strongly assume due to some scoping problems. In my case foo.data.frame doesn't find funs anymore. If tried a few things to re-define funs (see comments in code), to no avail.

So, my question would be, how could I make funs accessible to foo.data.frame within foo.list. Or, are there good reasons why this is bad programming style. (I am still considering myself a programming beginner). Cheers.

update

I could define funs in the global environment - and I am happy to do that. But is there a way to access the environment created by useMethod, kind of from within the method in the method?

set.seed(42)
mydf <- cbind(a = rep(letters[1:3], 4), setNames(as.data.frame(replicate(c(rnorm(11), NA), n = 3)), letters[24:26]))
mydf_grouped <- split(mydf, mydf$a)

foo <- function(x) {
  funs <- list(
    mean = function(x) mean(x, na.rm = TRUE)
  )
  UseMethod("foo", x)
}

foo.data.frame <- function(x, ...) {
  suppressWarnings(lapply(funs, mapply, x))
}
## works
foo(mydf)
#> $mean
#>          a          x          y          z 
#>         NA  0.6161670 -0.4570349  0.2275039

## works with a simple function
foo.list <- function(x, ...){ 
  lapply(x, nrow)
}
foo(mydf_grouped)
#> $a
#> [1] 4
#> 
#> $b
#> [1] 4
#> 
#> $c
#> [1] 4

## fails with the method for data.frames
foo.list <- function(x, ...){ ## adding funs argument does not help
  # funs <- funs ## does not work 
  # funs <- list(
  #   mean = function(x) mean(x, na.rm = TRUE)
  # )         ## Defining funs within foo.list does not work either
  lapply(x, foo.data.frame ) ## adding funs here does also not help
}
foo(mydf_grouped)
#> Error in lapply(funs, mapply, x): object 'funs' not found
Konrad Rudolph

The problem is that you are calling foo.data.frame directly, and that function defines no funs. You need to call it via the generic foo:

foo.list <- function(x) {
  lapply(x, foo)
}

This works, since foo in lapply will call the generic which correctly dispatches to the appropriate method (foo.data.frame) in the case of calling it with a list of data.frames, as in foo(mydf_grouped).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Using a method from one class into another class

Using a method from one class in another class

Calling method from one class in other

Android getting parameter from one method to another method in other class

Access to class method from other class method

coffeescript class - calling calling one class method from an other

Using a method from one class into a set

Using method from one class in another?

Apply class method to pandas dataframe column within other class method

Calling one method from another within same class in Python

Using controller class's method in other class

undefined method error when pass function from one class to the other

How to access a method in one class from other classes

Unity - One method from a class not working at other classes but the others do

Calling a method on an Object from within a Class vs from within a method

Calling a method from one class in another class

Method returning from one class to another class

Using class method to access class variable within a class

python getattr from class method, why is it not finding the other class method?

JS - Calling method in class from other method in same class

How do I access the variable of one method from another method within the same class C#?

Class method points to other method of other class

Java - How do I get a variable generated from within a method in one class to another class?

Using string formatting within class method docstrings

How to pass values from one class with on click method from other class

Calling a constructor from method within the same class

Calling super method from within super class

Determine the defining class from within a method

Node.js ES6 Class unable to call class method from within class method when using Express.js