You can extend R/S-Plus usability by writing functions. This is similar to macros in the SAS Language. These functions can be anything from a new statistical algorithm to file operation to data manipulation. Below I give an example of a custom R macro. This function takes output from an rpart tree and converts it to SAS code suitable to use in a data a step. This is useful when coding nodes into a model. This dirty little secret: I developed the code by looking at the default print method for the rpart package and adapting it to generate SAS code. This code can also be modified to generate SQL code as well. When attempting to write new code I suggest first looking at published package that do something similar then try to adapt them to your own use. The S language (which both R and SPlus use) is similar to C. There are many good editor for S. this code was written using TinnR. printSAS.rpart <- function(x, minlength=0, spaces=2, cp, tree.depth <- getFromNamespace( tree.depth , rpart ) if(!inherits(x, rpart )) stop( Not legitimate rpart object ) ylevel <- attr(x, ylevels ) #32 is the maximal depth tfun <- (x$functions)$print
z <- labels(x, digits=digits, minlength=minlength, …) term <- rep( , length(depth)) for(i in 1:length(depth)) #print(1:i) if(term[i] == Terminal ) } # end for final[i] <- paste ( If , temp1[i] , z[i], then NodeVal = , yval[i] , ; ) |
||