It looks like you are looking for methods . Many common functions ( print , summary , plot ) are overloaded in R using different methods depending on the class object to which they are applied.
You mentioned plot , but it's easier for me to start by looking at print . One common data structure that is used in R is a 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 different from a regular 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 create your own methods . This can be useful if you want to print something with special formatting. Here is a simple example for printing a vector with some unnecessary junk file.
#
As you can imagine, you can do the calculation methods yourself. Although this may seem convenient, it also ultimately leads to less "transparent" code.
A5C1D2H2I1M1N2O1R2T1
source share