1

I am looking to find the C source code for the cor() function in the R Stats package.

I've done some digging but I have hit a wall.

Looking at the source code on GitHub (https://github.com/SurajGupta/r-source/blob/master/src/library/stats/R/cor.R) I can see that the cor() wrapper function in R eventually calls C with

.Call(C_cor, x, y, na.method, FALSE)

So I thought, okay let's find C_cor(). But I can't find it anywhere. Oddly, if you search the entire code base for the stats package, there is no C function called C_cor...

Using get("C_cor", asNamespace("stats")) gets me the path:

/usr/local/Cellar/r/3.4.3/lib/R/library/stats/libs/stats.so

I want to find the C code used to compile this, how can I do that?

  • I believe the marked duplicate does not fully address OP's question. I feel like OP's problem is not primarily that they don't know how to find C source code for R packages, but that they don't realize they are really looking for a C function called `cor()` due to the line in the NAMESPACE `useDynLib(stats, .registration = TRUE, .fixes = "C_")`; this issue is not well addressed in the duplicate. You can find the C code for `cor()` here: https://github.com/wch/r-source/blob/trunk/src/library/stats/src/cov.c – duckmayr Feb 09 '19 at 12:39
  • See https://blog.revolutionanalytics.com/2009/02/finding-the-source.html and the (old yet still valid) piece by Uwe Ligges https://cran.r-project.org/doc/Rnews/Rnews_2006-4.pdf it points to. – Dirk Eddelbuettel Feb 09 '19 at 13:48
  • @DirkEddelbuettel These resources, and the answers to the marked duplicate, are excellent resources for generally finding R or C source code for R functions. However, they don't help enough in this specific case, as they do not help OP understand they have to actually look for the C function `cor()`, not `C_cor()`, due to the NAMESPACE line mentioned above. This NAMESPACE issue isn't mentioned in the linked sources. Searching a GitHub R source mirror for `C_cor` only brings up the `.Call()` call OP mentions, no C code hits at all: https://github.com/wch/r-source/search?q=C_cor&unscoped_q=C_cor – duckmayr Feb 09 '19 at 14:20
  • Thank you to the commenters, very helpful and answered my question. –  Feb 09 '19 at 16:20

0 Answers0