Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there function overloading in R?

Tags:

r

It is possible to overload function in R?like the plot function,that means two functions have the same name but different parameter list,how to achieve?

Thanks!!!

like image 714
Lawes Avatar asked Dec 28 '13 07:12

Lawes


People also ask

Can you overload function in r?

yes, this can be done by optional arguments but I'm looking for something similar to Erlang function definitions :-) Good suggestion, @joran.

Is it possible to overload a function?

Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading.

Is there function overloading in TypeScript?

TypeScript provides the concept of function overloading. You can have multiple functions with the same name but different parameter types and return type. However, the number of parameters should be the same.

Is function overloading good practice?

You need to be careful while overloading a method in Java, especially after the introduction of autoboxing in Java 5. Poorly overloaded method not only adds confusion among developers who use that but also they are error-prone and leaves your program at compiler's mercy to select proper method.


1 Answers

What it sounds like you're looking for are methods. Many common functions (print, summary, plot) are overloaded in R with different methods being applied depending on the class of the object they are being applied to.

You mentioned plot, but I find it easier to start by looking at print. One common data structure that is used in R is an object of the class of data.frame. If you look at methods("print"), you will find a specific print method for an object with this class. This makes it print differently from a normal list although a data.frame, is a special type of list in R.

Example:

mydf <- data.frame(lengths = 1:3, values = 1:3, blah = 1:3)
mydf ### SAME AS print(mydf)
#   lengths values blah
# 1       1      1    1
# 2       2      2    2
# 3       3      3    3

print.default(mydf) ## Override automatically calling `print.data.frame`
# $lengths
# [1] 1 2 3
# 
# $values
# [1] 1 2 3
# 
# $blah
# [1] 1 2 3
# 
# attr(,"class")
# [1] "data.frame"

print(unclass(mydf))  ## Similar to the above
# $lengths
# [1] 1 2 3
# 
# $values
# [1] 1 2 3
# 
# $blah
# [1] 1 2 3
# 
# attr(,"row.names")
# [1] 1 2 3

You can also, of course, create your own methods. This might be useful when you want to print something with specialized formatting. Here's a simple example to print a vector with some unnecessary junk.

## Define the print method
print.SOexample1 <- function(x, ...) {
  cat("Your values:\n============", 
      format(x, width = 6), sep = "\n>>> : ")
  invisible(x)
}

## Assign the method to your object
## "print" as you normally would
A <- 1:5
class(A) <- "SOexample1"
print.SOexample1(A)
# Your values:
# ============
# >>> :      1
# >>> :      2
# >>> :      3
# >>> :      4
# >>> :      5

## Remove the "class" value to get back to where you started
print(unclass(A))
# [1] 1 2 3 4 5

As you can imagine, it is possible to have your methods do calculations in themselves. While that might seem convenient, that also ultimately leads to less "transparent" code.

like image 80
A5C1D2H2I1M1N2O1R2T1 Avatar answered Nov 23 '22 13:11

A5C1D2H2I1M1N2O1R2T1