As the doctor gone rogue

May 20, 2012

Obtaining R Object name

Filed under: Uncategorized — Tags: , — hypotheses @ 9:07 am

While programming in R, occasionally I wish I can just get the name of an object that I am working with and print out the name, or use the name as a name for the current plot that I am creating.

This is more convenient when creating a function that your user might not have an idea yet what the title should be. By default, the title name in most plot functions in R is created using “deparse(substitute(x))” function

mydata <- cbind(c(1:10),c(1:10))
plot(mydata,main=deparse(substitute(mydata)))
deparse(substitute(mydata))

Name the plot after the name of the data frame used to create this plot.

May 3, 2012

Getting help finding sourcecode in R

Filed under: Uncategorized — Tags: , — hypotheses @ 3:41 pm

(From:http://cran.r-project.org/doc/Rnews/Rnews_2006-4.pdf  page 43)

 It’s always a good idea to look under the hood and see how things work! Sometimes, that’s the only way to make sure that other people’s codes or programs work the way you really expect it to be, especially the free ones.

 

  1. The simplest case is typeing the function’s name without “()” following the function’s name
     

> matrix
function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
{
    if (is.object(data) || !is.atomic(data))
        data <- as.vector(data)
    .Internal(matrix(data, nrow, ncol, byrow, dimnames, missing(nrow),
        missing(ncol)))
}
<environment: namespace:base>
 

However, the comments are not included,  and it might still be better to really look at the sourcecode.
You can find the original sources after unpakcing the source package, in the directory “PackageName/R/”.
For R’s base pakcages, the R code is in$R_HOME/src/library/PakageName/R/.
 

  1. For codes hidden in a namespace,  type getAnywhere(“FunctionName”) to find out
     

> plot.factor
Error: object ‘plot.factor’ not found
> getAnywhere(plot.factor)
A single object matching ‘plot.factor’ was found
It was found in the following places
  registered S3 method for plot from namespace graphics
  namespace:graphics
with value

function (x, y, legend.text = NULL, …)
{


}

<environment: namespace:graphics>
 

It is possible

to ask for available methods with methods(print).

The function of interest is the S3 method print.lm()
 

> methods(print)
  [1] print.acf*                              
  [2] print.anova                             
  [3] print.lm
 

A method hidden in a namespace

can be accessed (and therefore printed) directly

using the ::: operator as in stats:::print.lm.

For S4 related sources, it is advisable to look at the package’s source files directly!

  1. For “COMPILED” code sources, these are always more problematic. Why you type the name of these functions, what you will see are something like these: .C(), .Call(), .Fortran(), .External(), or .Internal(), or .Primitive().
     

– The first step is to look up the entry point in file ‘$R_HOME/src/main/names.c’, if the calling R function is either .Primitive() or .Internal().
You will find something like this
{“rnorm”,        do_random2,        8,        11,        3,        {PP_FUNCALL, PREC_FN,        0}},
This tells you to find “do_random2” in the source files.
– Then, grep ‘do_random2″ *.* in the source directory should point you to the correct file to look at. In this case, the source is in “random.c”

 

Now it’s time to learn “c” in order to understand more about these sources. At least, you will have to know the program structure well enough to be able to read other people’s codes.
  

Create a free website or blog at WordPress.com.