S3_and_usemethod_notes

2013-09-04 by

Classes/S3

In these notes, we explain by example how the Class and UseMethod are used, and how to overwrite function. These notes are taken from http://www.math.ncu.edu.tw/~chenwc/R_note/index.php?item=class

Class and UseMethod

This is a silly example, but it gives a hint to make a class by class() and use generic functions by using UseMethod(). In R, you can define your generic functions for print().

If a1 is a variable, then typing a1 (on the console) is the same as print(a1). We will start definining some variable, a1, a2, and illustrate the usage class and the UseMethod

a1 <- 3.1415926
class(a1) <- "myC1"
a2 <- 6.1415926
class(a2) <- "myC2"

## defining a generic function called my_fcn
my_fcn <- function(x){
  UseMethod("my_usefcn", x)
}

## defining the implmentation of my_fcn for class myC1 and myC2
my_usefcn.myC1 <- function(x){
  x + 1
}
my_usefcn.myC2 <- function(x){
  x + 2
}

At First when we call

my_fcn(a1)

## [1] 4.141593
## attr(,"class")
## [1] "myC1"

my_fcn(a2)

## [1] 8.141593
## attr(,"class")
## [1] "myC2"

my_fcn() return the object's attributes since there is no default print() function for classes myC1 and myC2.

Similarly, when we type a1 and a2 in the console

a1

## [1] 3.141593
## attr(,"class")
## [1] "myC1"

a2

## [1] 6.141593
## attr(,"class")
## [1] "myC2"

we get the attributes since there is no default print() function for classes myC1 and myC2

But now let's define some print() function for these classes

#Defining a print function for class myC1 --- Note the function names
print.myC1 <- function(x, digits = 3){
  print(unclass(x), digits = digits)
}
#Defining a print function for class myC2
print.myC2 <- function(x, digits = 6){
  print(unclass(x), digits = digits)
}

and now when we call

my_fcn(a1)

## [1] 4.14

my_fcn(a2)

## [1] 8.14159

print(a1)

## [1] 3.14

print(a2)

## [1] 6.14159

a1

## [1] 3.14

a2

## [1] 6.14159

we get the nice R behavior and our newly defined objects of class myC1 and myC2 print properly.

Notes: * The attributes are discarded in print() by using unclass() function * the syntax for the function name of print functions. There is period followed by the name of the class for which the generic function is defined for. * A more formal way of stating the previous note, is: To create an S3 method write a function with the name generic.class, where generic is a generic function name and class is the corresponding class for the method. Examples of generic functions are summary(), print() and plot().

Summary and Print

In, we define some gerenic functions for summary(), and print(), so they can summary the results by the input's attribute and print it by the summary's attribute, NOT the input's attribute. Note that class is an attribute!

summary.myC3 <- function(x){
  x <- x + 10
  class(x) <-"summary_C3" 
  x
}
summary.myC4 <- function(x){
  x <- x + 20
  class(x) <-"summary_C4" 
  x
}
print.summary_C3 <- function(x, digits = 3){
  cat("Result: ", format(x, digits = digits), "\n")
}
print.summary_C4 <- function(x, digits = 6){
  cat("Result: ", format(x, digits = digits), "\n")
}

In the above definition, we have created a summary function for classes myC3 and myC4. In those function we have set the classes of the object to summary_C3 and summary_C4 respectively. We then defined print function for classes summary_C3 and summary_C4 which will be used to print object of original classes myC3 and myC4. Although convoluted, this illustrates how we can "chain" classes together to use function already defined for other classes. In the following we provide examples to show how these functions are used and the value they return.

a1 <- 3.1415926
class(a1) <- "myC3"
a2 <- 6.1415926
class(a2) <- "myC4"

Now we when we call the apply the summary function on these newly defined objects, we get

summary(a1)

## Result:  13.1

summary(a2)

## Result:  26.1416

Overwrite Operators

This is also the other silly example, but I want to demonstrate overwrite functions in R and user defined function for binary operators. In R, you can redefine an operator or use %any% to make a new one. we define a unior operator of two sets in the following.

a1 <- c("a", "b")
a2 <- 4:7
class(a1) <- "myC5"

### Overwrite "+" for a new class.
"+.myC5" <- function(a, b) c(a, b) 

Now we apply the overloaded operator to our new defined object we get

a1 + a2

## [1] "a" "b" "4" "5" "6" "7"

Similarly, we can overload the union operator as following

### Define a new one.
"%union%" <- function(a, b) c(a, b)

and then use it as such

a1 %union% a2

## [1] "a" "b" "4" "5" "6" "7"

Dll_notes

2013-08-26 by

Notes about GCC and make

  • Compiling, Linking and Building C/C++ Applications tutorial

  • Documentation for gcc command options can be found here

  • Sometime, when compiling in linux, you might encounter the error: ld cannot find an existing library which might be caused by the fact that the library you are linking against, end with .so.1 instead of just .so. If this happens

    • see stackoverflow link for how to fix it,
    • see blog link for Linkers and names -- see previous stackoverflow link, the comments for first answers which at the time of writting this document had 22 upvotes
    • see libtool which resolves the libraries automatically.

Notes for creating C code to be called from R

  • Notes from Simon Fraser University, departement of Statistics can be found here

  • Notes from Hadley Wickham on github, which can be found here

Notes for creating DLL using an existing C library to be used in R

  1. To use Makevars with R CMD SHLIB either

  2. gcc equivalent commands to R CMD SHLIB can be found in the answer to the following question found on stackoverflow

  3. To add enviroment variable in a unix-like system, u can either add them to the .bash_profile or to the .profile files in your home directory see unix.stackexchange

  4. It's not a good idea to mix compilers (i.e. use gcc with a library built in Microsoft VisualC++ (MSVC)).stackoverflow Here is some links that might help

  5. But if you insist, then good luck. below are some links

  6. Source Code for SHLIB command (for sun arch which is developed by Sun and whose operating system includes SunOS, Solaris or OpenSolaris, NeXTSTEP, RTEMS, FreeBSD, OpenBSD, NetBSD, and GNU/Linux) can be found here

  7. Packaging a C library in 15 minutes R-bloggers

  8. For the difference between: Makefile, Makevars and configure, see Prof. Brian D. Ripley explanation on R-dev mailing list

  9. Make and Makefiles tutorial can be found @ http://mrbook.org/tutorials/make/

Notes for Daemons and Agents in Mac OSx

  1. see apple developer link for basic introductory information

  2. use launchd to manage daemons apple support link

  3. To configure daemon, one creates a .plist wich is essentially an xml file. See blog link for the .plist file used to manage RStudio Server on a Mac, and it's usually placed in /Library/LaunchDaemons.

  4. launchd.plist command to manage daemons on mac apple developer link

  5. launchctl Tutorial blog link

  6. Lingon is a GUI utility to view & edit agents & daemons for OS X - see. blog link

Notes for Linux:

  1. Ubuntu keyboard shortcuts can be found @ https://help.ubuntu.com/community/KeyboardShortcuts

  2. to find out the ip of the machine, use ifconfig command

  3. how to create symbolic link using ls command. seeaskubuntu.com

  4. Despite initial success with R CMD SHLIB after setting up enviroment variable, as discussed above, when i used the command dyn.load in R, i would get an error that the library file could not be found. This is the same library file that i had linked against, using the enviroment variable. To diagnose this problem, i went back to the .so file which was created usin R CMD SHLIB, and type: ldd NameOfLibrary.so to see its dependencies. See the links below for info.

  5. Another possible solution (although untested), is to LD_LIBRARY_PATH environment variable, as described in http://www.bioconductor.org/help/faq/ where they discuss the situation where an R package can install properly but fail to load.

  6. To set enviroment variable from within R and to be used only for R, use the following commands: Sys.getenv and Sys.setenv

  7. Library file (.so) are usually installed in /usr/local/lib/ as discussed in askubuntu and ubuntuforums


Twitter feed section



R-Corner