Qbik
Qbik

Reputation: 6147

Using .Call in R outside the main function

I was trying to analyse cor(stats) function but I stack on line containing first .Call function :

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

C_cor isn't defined before it is called, it isn't defined anywhere, how to execute above line outside the cor function ? Setting y to NULL, na.method to everything, and x to some dataset brings the same object 'C_cor' not found error, part of cor() body below :

> cor
    function (x, y = NULL, use = "everything", method = c("pearson", 
        "kendall", "spearman")) 
{
    na.method <- pmatch(use, c("all.obs", "complete.obs", "pairwise.complete.obs", 
        "everything", "na.or.complete"))
    if (is.na(na.method)) 
        stop("invalid 'use' argument")
    method <- match.arg(method)
    if (is.data.frame(y)) 
        y <- as.matrix(y)
    if (is.data.frame(x)) 
        x <- as.matrix(x)
    if (!is.matrix(x) && is.null(y)) 
        stop("supply both 'x' and 'y' or a matrix-like 'x'")
    if (!(is.numeric(x) || is.logical(x))) 
        stop("'x' must be numeric")
    stopifnot(is.atomic(x))
    if (!is.null(y)) {
        if (!(is.numeric(y) || is.logical(y))) 
            stop("'y' must be numeric")
        stopifnot(is.atomic(y))
    }
    Rank <- function(u) {
        if (length(u) == 0L) 
            u
        else if (is.matrix(u)) {
            if (nrow(u) > 1L) 
                apply(u, 2L, rank, na.last = "keep")
            else row(u)
        }
        else rank(u, na.last = "keep")
    }
    if (method == "pearson") 
        Call(C_cor, x, y, na.method, FALSE)
   ...

Edition example of use

you can call C_cor like below :

C_cor=get("C_cor", asNamespace("stats"))  
.Call(C_cor, x=as.matrix(iris[,1:4]), y=NULL, method=1, FALSE

)

where method is number of method on this list :

c("all.obs", "complete.obs", "pairwise.complete.obs",   
            "everything", "na.or.complete")

Upvotes: 2

Views: 1145

Answers (1)

MrFlick
MrFlick

Reputation: 206197

C_cor is a variable in the stats package that is not exported for general use. You can view it's contents with

 get("C_cor", asNamespace("stats"))

but since environment(cor) is <environment: namespace:stats>, the cor() function has access to those un-exported variables.

The .Call function is meant to run code from a compiled DLL or shared object. Perhaps read this question if you wish to track down the source code. Generally you should not try to call those functions directly yourself because if you pass in a bad/unexpected argument, you may cause a crash.

Upvotes: 2

Related Questions