Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internal options in R

Tags:

r

Sometimes I find R functions that have undocumented options like use.names in c:

x <- c(a = 1, b = 2, c = 3, d = 4)
y <- c(e = 5, f = 6, g = 7, e = 8, h = 9)

print(c(x, y))
# a b c d e f g e h 
# 1 2 3 4 5 6 7 8 9 

print(c(x, y, i = 10))
# a  b  c  d  e  f  g  e  h  i 
# 1  2  3  4  5  6  7  8  9 10 

print(c(x, y, use.names = F))
# [1] 1 2 3 4 5 6 7 8 9

Are there any way of checking this .Internal or .Primitive function options? Is there any danger in their use, like random changes in the API?

like image 643
durum Avatar asked Dec 22 '15 15:12

durum


People also ask

What is an internal function in R?

What is an internal function? It's a function that lives in your package, but that isn't surfaced to the user. You could also call it unexported function or helper function; as opposed to exported functions and user-facing functions.

What is SEXP R?

A SEXP is a variant type, with subtypes for all R's data structures. The most important types are: REALSXP : numeric vector. INTSXP : integer vector. LGLSXP : logical vector.

How do I view a function within an R package?

We can find the functions inside a package by using lsf. str function but we need to load the package prior to knowing the functions inside.

How do you call a function from a package in R?

Every time you start a new R session you'll need to load the packages/libraries that contain functions you want to use, using either library() or require() . If you load a package with the same function name as in another package, you can use packageName::functionName() to call the function directly.


Video Answer


1 Answers

Today (2021, R4.1) the use.names option is officially documented.

The only way to check the implementation of .Internal or .Primitive R functions is to inspect the R source - for this particular option, here's the exact line.

As a general rule, undocumented API or API-parts are not a part of the contract between the developers and the users, and so are subject to change at any time. This option might have been an experimental feature at some point, and documenting it made it an official part of R. Alternatively it might have been an unintentional oversight that was fixed sometime later. After tracking the R development process for a bit, it seems to me both options are equally likely.


Edit: I recently learnt of Jim Hester's "lookup" R package, which uses github API and some heuristics to inspect the R sources from an R session. Example:

> devtools::install_github("jimhester/lookup")
Downloading GitHub repo jimhester/lookup@HEAD
These packages have more recent versions available.
It is recommended to update all of them.
Which would you like to update?

1: All                                     
2: CRAN packages only                      
3: None                                    
4: gh      (1.3.0 -> f75b0e498...) [GitHub]
5: memoise (2.0.1 -> 3b0bf3e5f...) [GitHub]

Enter one or more numbers, or an empty line to skip updates: 3   # <- just to save time
...

> library("lookup")
...

> is.list   # for example

This would open up an editor window with the relevant C code. Note that only the latest R sources are inspected, they typically differ slightly from the sources for the R you're actually running.

like image 98
Ofek Shilon Avatar answered Oct 03 '22 19:10

Ofek Shilon