2012-12-26 27 views
62

這些都不顯示pnorm功能的源代碼,如何查看R .Internal或.Primitive函數的源代碼?

stats:::pnorm 
getAnywhere(pnorm) 

我怎麼能看到的pnorm的源代碼?

sum 
(..., na.rm = FALSE) .Primitive("sum") 
.Primitive("sum") 
function (..., na.rm = FALSE) .Primitive("sum") 
methods(sum) 
no methods were found 

和,我怎樣才能看到sum函數的源代碼?

回答

74

pnorm將R源代碼是:

function (q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE) 
.Call(C_pnorm, q, mean, sd, lower.tail, log.p) 

所以,從技術上來講,鍵入 「pnorm」 確實告訴你的源代碼。然而,更有用的是:pnorm的內容用C語言編碼,所以前面的問題view source code in R中的建議只是在外圍是有用的(大部分集中在隱藏在名稱空間中的函數等)。

Uwe Ligges的article in R news(第43頁)是一個很好的一般參考。從該文檔:

當尋找在r源代碼,有時調用 到以下功能中的一個顯示:.C(), .CALL(),.Fortran(),.External(),或.Internal() 和.Primitive()。這些函數調用編譯代碼中的入口點,如共享對象,靜態庫或動態鏈接庫。因此,如果需要對代碼的完整理解是 ,則有必要查看編譯代碼的來源。 ... 如果 調用的R函數是.Primitive()或 .Internal(),則文件'$ R HOME/src/main/names.c'中的第一步是查找 入口點。 。 這在以下示例中針對實現「簡單」R函數 sum()的代碼完成。

(着重號,因爲你問到的精確功能(sum)被覆蓋在Ligges的文章。)

根據您想如何認真鑽研的代碼,它可能是值得下載和 按照Ligges的建議解包源代碼(例如,然後您可以使用命令行工具 ,如grep來搜索源代碼)。要進行更加隨意的檢查,您可以通過R Subversion serverWinston Chang's github mirror在線查看 的信息來源(這裏的鏈接專門針對src/nmath/pnorm.c)。 (猜測看,src/nmath/pnorm.c正確的地方,需要一些熟悉的R源代碼的結構。)

meansumsummary.c二者實現。

+1

它從'pnorm'不同的類別。爲R代碼嘗試使用'mean.default',爲C代碼使用https://github.com/wch/r-source/blob/trunk/src/main/summary.c。並閱讀上面鏈接的Uwe Ligges的文章! –

6
> methods(mean) 
[1] mean.data.frame mean.Date  mean.default mean.difftime mean.IDate*  
[6] mean.POSIXct mean.POSIXlt mean.yearmon* mean.yearqtr* 

    Non-visible functions are asterisked 
> mean.default 
function (x, trim = 0, na.rm = FALSE, ...) 
{ 
    if (!is.numeric(x) && !is.complex(x) && !is.logical(x)) { 
     warning("argument is not numeric or logical: returning NA") 
     return(NA_real_) 
    } 
    if (na.rm) 
     x <- x[!is.na(x)] 
    if (!is.numeric(trim) || length(trim) != 1L) 
     stop("'trim' must be numeric of length one") 
    n <- length(x) 
    if (trim > 0 && n) { 
     if (is.complex(x)) 
      stop("trimmed means are not defined for complex data") 
     if (any(is.na(x))) 
      return(NA_real_) 
     if (trim >= 0.5) 
      return(stats::median(x, na.rm = FALSE)) 
     lo <- floor(n * trim) + 1 
     hi <- n + 1 - lo 
     x <- sort.int(x, partial = unique(c(lo, hi)))[lo:hi] 
    } 
    .Internal(mean(x)) 
} 
<bytecode: 0x155ef58> 
<environment: namespace:base> 
+0

這似乎並不回答OP的原始問題(關於'pnorm'),但他們的評論低於'mean' - 並且注意到這也落在了C代碼的底部(參見下面的評論)。 –

+1

確實。而「正確的答案」就是你之前給過的那個......閱讀Uwe Ligges在RNews上的文章。 –

23

我知道這個職位是2歲以上,但我認爲這可能是有用的瀏覽這個問題的一些用戶。

我基本上只是複製我的回答this other similar question,以便它可以或許證明給誰想要探索的C源文件中的某些[R用戶非常有用。

  1. 首先,pryr你可以做使用show_c_source功能,這將在GitHub上相關的代碼在C源文件中搜索。適用於.Internal和.Primitive功能。

    body(match.call) 
    
    # .Internal(match.call(definition, call, expand.dots)) 
    
    pryr::show_c_source(.Internal(match.call(definition, call, expand.dots))) 
    

    這需要你this page,顯示出unique.c包含函數do_matchcall

  2. 我已經把這個tab delimited file,建立在names.c文件,並使用發現,在檔案來確定源代碼的位置。有一些函數具有特定於平臺的文件,還有一些函數具有多個具有相關源代碼的文件。但對於其餘的映射來說,至少對於當前版本(3.1.2)來說,這種映射是非常完善的。