Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arguments and classes for writing (generic) functions in R

I want to make a small R package out of a few very simple functions. The literature I use is "Creating R packages: A tutorial", and "Writing R extensions". Although I tried, but I don't really understand the concept of generic functions and methods and how to handle the arguments within the different functions.

Here a small example of how my code looks like:

#Make generic function
f <- function(x,...) UseMethod("newmethod")

#Default method
f.default <- function(a,b=5,c=3,...){
    out <- a+b+c
    class(out) <- "fclass"
}

# Print method
print.f <- function(x,...){
    cat("Result:")
    print(x)
}

# Summary method
summary.f <- function(object,...){
    res <- object
    class(res) <- "fsummary"
    print(res)
}

# Plot method
plot.f <-function(x,p=0.3,...){}

I have a function called f with a default f.default. Actually my function needs several arguments (non of them is defined as x), so how do I have to make my generic function? The print method should simply print the output of f.default (in that simple case similar to the summary output). plot.f method uses the the output of f.default and one additional argument (obligatory). How can I write these functions correctly? The usual methods use arguments like "object" and "x"...but as I said I don't need any variable x in my functions... I am a little confused...maybe someone can help.

If there is someone out there how is willing to help me with this problem, I could also send the "real" R code (not only this fictive example).

like image 316
Johannes Avatar asked Dec 08 '11 11:12

Johannes


People also ask

How do I create a generic function in R?

Generic functions are created and assigned by setGeneric or setGroupGeneric and, indirectly, by setMethod .

What is generic function in R Give Two example?

A generic function is one which may be applied to different types of inputs producing results depending on the type of input. no status days ulc thick sex 1 789 3 10 1 676 2 2 13 3 30 2 65 2 3 97 2 35 2 134 2 4 16 3 99 2 290 1 5 21 1 185 1 1208 2 ... sex=1 lower upper time n.

What are generic functions and generic classes?

Generic functions are functions declared with one or more generic type parameters. They may be methods in a class or struct , or standalone functions. A single generic declaration implicitly declares a family of functions that differ only in the substitution of a different actual type for the generic type parameter.

What is the syntax for generic function?

The syntax for a generic method includes a list of type parameters, inside angle brackets, which appears before the method's return type. For static generic methods, the type parameter section must appear before the method's return type.


2 Answers

You've got into a right mess here...

First, your constructor function probably shouldn't be a generic/method. Something like:

makefclass = function(a,b,c){
        l = list(a=a,b=b,c=c)
        class(l)="fclass"
        return(l)
      }

Then you can write print.fclass:

print.fclass=function(x,...){
     cat("I'm an fclass!")
     cat("my abc is ",x$a,x$b,x$c,"\n")
}

Then you do:

> f=makefclass(1,2,3)
> f
I'm an fclass!my abc is  1 2 2 

Hope that helps...

like image 86
Spacedman Avatar answered Sep 18 '22 20:09

Spacedman


I've fixed your code and put in some comments on what I fixed...

UPDATE As @Spacedman pointed out, the "constructor" function should probably not be generic. But I'll keep it here so you see how a generic function is done.

#Make generic function
# This is the "constructor" function...
# ... UseMethod should have the name of the function!
f <- function(x,...) UseMethod("f")

#Default method
# ... The class name should be the same as the constructor
f.default <- function(a,b=5,c=3,...){
    out <- a+b+c
    class(out) <- "f"
    out # must return the object out, not the class name!
}

# Print method
# The "f" part of "print.f" must be the same as the class!
print.f <- function(x,...){
    cat("Result for f: ")
    print(unclass(x)) # Must unclass to avoid infinite recursion
    # NextMethod(x) # Alternative, but prints the class attribute...
}

# Summary method
# Should return a summary object (and not print it!)
# Need a unique class for it ("fsummary")
summary.f <- function(object,...){
    res <- object
    class(res) <- "fsummary"
    res
}

# Now need to print the summary too:
print.fsummary <- function(x, ...) {
    cat("f summary!\n")
    # Nice summary print goes here...
}

# Plot method
plot.f <-function(x,p=0.3,...){ cat("PLOTTING!\n") }

# Try it out:

x <- f(3)
x # print x

y <- summary(x) # 
y # print summary

plot(x)
like image 45
Tommy Avatar answered Sep 17 '22 20:09

Tommy