Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R source code .Call function [duplicate]

Tags:

r

I was looking at the source_code of cov in R, and run into the a paragraph of code that I don't quite understand.

The mathematical definition of covariance goes here.

if (method == "pearson") 
    .Call(C_cov, x, y, na.method, method == "kendall")
else if ...

The help manual says something about the .Call function:

CallExternal {base} R Documentation
Modern Interfaces to C/C++ code
Description
Functions to pass R objects to compiled C/C++ code that has been loaded into R.

I am wondering where can I find the source code of how to calculate the covariance either C++ or C or whatever.

Thanks.

like image 785
B.Mr.W. Avatar asked Oct 28 '13 00:10

B.Mr.W.


1 Answers

.Call is used to pass variables to C routines. C_cov is a variable (in the stats namespace we'll soon see) that tells .Call where to find the routine that it should use to calculate covariance.

If you type C_cov at the command line, you'll get

Error: object 'C_cov' not found

That's because it's hidden from you. You'll have to do a little detective work.

getAnywhere('C_cov')
# 4 differing objects matching ‘C_cov’ were found
# in the following places
#   namespace:stats
# Use [] to view one of them

This tells us that there's a variable named C_cov in the stats name space (your output may look slightly different from this). Let's try to get it.

stats::C_cov
# Error: 'C_cov' is not an exported object from 'namespace:stats'

Apparently C_cov is not for public consumption. That's all right, we can get it anyway:

stats:::C_cov # use three colons to get unexported variables.
# $name
# [1] "cov"
# # blah, blah, blah ...
# $dll
# DLL name: stats
# Filename: C:/Program Files/R/R-3.0.1/library/stats/libs/x64/stats.dll
# Dynamic lookup: FALSE
# # blah, blah, ...

That's the info we want. It tells us the name of the routine and library it's in. Now we just need to go to C source and follow the trail: .../src/library/stats/src/cov.c

like image 182
Matthew Plourde Avatar answered Sep 28 '22 13:09

Matthew Plourde