Overwriting plot, summary for your customized dataframe

This tutorial provide you how to simply, overwriting existing generic R function like plot,summary that work for your customized data.  For example, lm function, 

model <- lm(y ~x, data=mydata) 

plot(model)  # plot function use special code to handle the output of R object from lm.

R Programming Trick

Here is how you do it in R. 

createMyspecialData <-function(…)
{
# let’s create dummy data
mydata<-data.frame(x=1:5,y=5:1)

# declare this data frame as unique class
class(mydata) <-‘mydata’

# inheriate existing data.frame behavior ,
# new class called ‘mydatatype’ do like common.data frame do.
class(mydata) = c(‘mydatatype’, class(ret))
inherits(mydata,’data.frame’) # overwrite data.frame behaviour

}

# OVERWRITING plot for your customized data ‘mydatatype’
plot.mydatatype<-function(x){
require(ggplot2)

browser()

# my plot function use ggplot2
P <- ggplot(data=x,…)

# then, add customize for your data.
P<-P+ggtile(‘my special data’) + …
}

 

Preparation for R package development

  1. Install devtools and roxygen2 package.
  2. Create an RStudio project with “package” build mode. (Ctl-Shift-B)
  3. Set to roxygenize on all three possibilities
  4. Set to build NAMESPACE, collate, and man fields
  5. Create a package directory
  6. Create an “R” directory
  7. Put some Roxygenized R code into “./R”
  8. Load/Install Devtools (install.packate(‘devtools’) )  
  9. require(devtools);  load_all()
  10. Fill out the description File

 

R OOD (prototyping code) Tip

# HOW TO WRITE R Structure Code

# demo of how to use R prototype coding

setGeneric(“givePrediction”, function(object) {
standardGeneric(‘givePrediction’)
})  # declare general method

setClass(‘bank’, representation(id=’numeric’, cashPoints=’list’))  # my test class

# default constructor for my test class
setMethod(‘initialize’,
‘bank’,
function(.Object,
id=1
){
# initialize object
.Object@id<-id
.Object@cashPoints$name <- ‘chris’

message(‘do this’)
return(.Object)
})

# global function prototype, doesn’t need to worry about other
# declaration
setGeneric(“getY”, function(object) standardGeneric(“getY”))

# writing generic method of bank
setMethod(‘getY’,
‘bank’,
function(object
){
# initialize object
message( object@id )
#return(object)
})
O <- new(‘bank’)
getY(O)

O1 <- new(‘bank’, 2)
getY(O1)

# DEBUG THIS
#untrace(‘getY’)
trace(‘getY’,browser,exit=browser, signature=c(‘bank’))

R Debugging Tip

Image

#
# R Debug Example
#
# options(warn=1) # print warning when error
# options(error=recover,warn=2) # treat warnings as errors

myFun <- function(myarg=1){
message(‘debugging my buggy function ‘)
stop(“Woops! An error”)
}

myFun2 <- function(){
myFun()
}

demo <-
function(){

myFun()
# Error in myFun() : Woops! An error
traceback()

myFun2()
traceback()

# this option, whenever error occurs, call traceback automatically
options(error=traceback)
myFun2()

debugonce(myFun2) # debug once,
myFun2()

options(error=recover) # option to recover
myFun2()

# switch back to normal mode
options(error=NULL)

# settting up break point
setBreakpoint(srcfile=’debug.r’, line=5)
setBreakpoint(srcfile=’debug.r’, line=9)

myFun2()

# unset break point
setBreakpoint(srcfile=’debug.r’, line=5, clear=TRUE)

} # function( demo )

demo.advance <- function()
{
# do more wisely,
as.list(body(myFun))

# conditional tracing function (trace when function argument is 1. )
trace(“myFun”, quote(if( myarg != 1) { browser() }), at=3, print=F)
myFun(1) # no trace
myFun(2) # with trace

untrace(myFun) # get rid of tracing

# debuging warning message
options(warn=1) # print warning when error
options(warn=2) # treat warnings as errors

}