tags:

views:

131

answers:

3

How do I view source code in R? For example, for function portfolio.optim

> require(tseries)
> portfolio.optim
function (x, ...) 
UseMethod("portfolio.optim")
<environment: namespace:tseries>

> methods(portfolio.optim)
[1] portfolio.optim.default* portfolio.optim.ts*     

Non-visible functions are asterisked
> portfolio.optim.ts
Error: object 'portfolio.optim.ts' not found
> portfolio.optim.default
Error: object 'portfolio.optim.default' not found

When I install R package locally, does it download source code as well? Where does it store on the computer? Does anyone know?

+3  A: 
  1. In response to Non-visible functions are asterisked, this means that the actual functions that are dispatched on ts or default objects, respectively, are in the tseries namespace but not exported. So just type tseries:::portfolio.optim.default and you see the function code once you specify the full patch including the namespace.

  2. Whether R downloads source or a binary depends on your operating system. In either event, source for the tseries package is available. Reading source code written by experienced coders is a good way to learn.

Dirk Eddelbuettel
I am unable to display source code for function `lu` in package `Matrix`. Can you please take a look?
A: 

In R introduction you could find description of the getAnywhere function (or see help page ?getAnywhere).
It's helpful when you don't know in which namespace is a function.

Marek
A: 

What you can do for most of the functions is enter edit(functionname) into the command window in R. Where you fill in functionname with the name.

As a result you can get the source code of the function. However, I tried it for the function portfolio.optim, so there it does not work. Possibly only for standard functions.