How to see the source code of R .Internal or .Primitive function?

The R source code of pnorm is:

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

So, technically speaking, typing “pnorm” does show you the source code. However, more usefully: The guts of pnorm are coded in C, so the advice in the previous question view source code in R is only peripherally useful (most of it concentrates on functions hidden in namespaces etc.).

Uwe Ligges’s article in R news, Accessing the Sources (p. 43), is a good general reference. From that document:

When looking at R source code, sometimes calls
to one of the following functions show up: .C(),
.Call(), .Fortran(), .External(), or .Internal()
and .Primitive(). These functions are calling entry points in compiled code such as shared objects,
static libraries or dynamic link libraries. Therefore,
it is necessary to look into the sources of the compiled code, if complete understanding of the code is
required.

The first step is to look up the
entry point in file ‘$R HOME/src/main/names.c’, if
the calling R function is either .Primitive() or
.Internal(). This is done in the following example for the code implementing the ‘simple’ R function
sum().

(Emphasis added because the precise function you asked about (sum) is covered in Ligges’s article.)

Depending on how seriously you want to dig into the code, it may be worth downloading and
unpacking the source code as Ligges suggests (for example, then you can use command-line tools
such as grep to search through the source code). For more casual inspection, you can view
the sources online via the R Subversion server or Winston Chang’s github mirror (links here are specifically to src/nmath/pnorm.c). (Guessing the right place to look, src/nmath/pnorm.c, takes some familiarity with the structure of the R source code.)

mean and sum are both implemented in summary.c.

Leave a Comment