7
votes

I want to find the location of the script .R files which are used for computation in R.

I know that by typing the object function, I will get the code which is running and then I can copy and edit and save it as a new script file and use that.

The reason for asking to find the foo.R file is

  1. Curiosity
  2. Know what is the algorithm used in the numerical computations
  3. More immedietly, the function from stats package I am using, is running results for two of the arguments and not the others and have to figure out how to make it work. Error shown by R implies that there might be some modification required in the script file.

I am looking for a more general answer, if its possible.

Edit: As per the comments so far, here is the code to compute spectrum of a time series using autoregressive methods. The data input is a univariate series.

x = ts(data)
spec.ar(x, method = "yule-walker")    1
spec.ar(x, method = "burg")        2

command 1 is running ok. command 2 gives the following error.

Error in ar.burg.default(x, aic = aic, order.max = order.max, na.action = na.action,  : 
  Burg's algorithm only implemented for univariate series

I did try specify all the arguments correctly like na.action=na.fail, order.max = NULL etc but the message is the same. Kindly suggest possible solutions.

P.S. (This question is posted after searching the library folder where R is installed and zip files which come with packages, manuals, and opening .rdb, .rdx files)

4
The way to find the .R files does depend on the specifics of the package you're using (how did you install it, for example).David Robinson
I have installed them using the install manager in R and am using packages related to time series analysis. I did not find the script .R files in quite a few that I checked even with the zip or tar files which come with the CRAN package online.Anusha
As for (3), it's very unlikely that the solution to an error from a function in the stats package (which is part of the base R distribution, not an add-on) requires modifying the source code. But that's probably better addressed in a separate question, with the specific information provided.joran
Added the code with error. I was thinking that there might be a general, known way of finding the package script files aka source code. If I am missing any file in tar.gz file which I must open,please let me know.Anusha
That error message indicates that there is nothing to "fix". It simply only accepts univariate data at this time.joran

4 Answers

10
votes

See FAQ 7.40 How do I access the source code for a function?

In most cases, typing the name of the function will print its source code. However, code is sometimes hidden in a namespace, or compiled. For a complete overview on how to access source code, see Uwe Ligges (2006), “Help Desk: Accessing the sources”, R News, 6/4, 43–45 (http://cran.r-project.org/doc/Rnews/Rnews_2006-4.pdf).

6
votes

When R installs a package, it evaluates all the ".R" source files and re-saves them into a binary format for faster loading. Therefore you typically cannot easily find the source file.

As has been suggested elsewhere, you can simply type the function name and see the source code, or download the source package and find the source there.

library(plyr)
ddply # prints the source for ddply

# See the content of the R directory for plyr,
# but it's only binary files:
dir(file.path(find.package("plyr"), "R"))
# [1] "plyr"     "plyr.rdb" "plyr.rdx"

# Get the source for the package:
download.packages("plyr", "~", type="source")

# ...then unpack and inspect the R directory...
5
votes

I think you are asking to see what I call the source code for a function in a package. If so, the way I do it is as follows, which has worked successfully for me on the three times I have tried. I keep these instructions handy in a few places and just copied and pasted them here:

To see the source code for a function in Program R download the package containing the function. Specifically, download the file that ends in "tar.gz". This is a compressed file. Expand the compressed file using, for example, "WinZip". Now you need to open the uncompressed file that ends in ".tar". Download the free software "7-Zip". Click on the file "7zFM.exe" and navigate to the directory containing the ".tar" file. You can extract the contents of that ".tar" file into a new folder. The contents consist of R files showing the source code for the functions in the R package.

EDIT:

Today (July 8, 2012) I was able to open the 'tar.gz' file using the latest version of 'WinZIP' and could copy the contents (the source code) from there without having to use '7-Zip'.

EDIT:

Today (January 19, 2013) I viewed the source code for functions in base R by downloading the file 'R-2.15.2.tar.gz'

To download that file go to the http://cran.at.r-project.org/ webpage and click on that file in this line:

"The latest release (2012-10-26, Trick or Treat): R-2.15.2.tar.gz, read what's new in the latest version."

Unzip the file. WinZip will work, or it did for me. Then search your computer for readtable.r or another base R function.

agstudy noted here https://stackguides.com/questions/14417214/source-file-for-r-function that source code for read.csv is located in the file readtable.r, so do not expect every base R function to have its own file.

4
votes

.libPaths() should tell you all of your current library locations. It's possible to have more than one installation of a package if there are two libraries but only the one that is in the first library will be used. Unless you offer the code and the exact error message, it's not likely that anyone will be able to offer better advice.