Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protecting function names in R

Tags:

r

Is it possible in R to protect function names (or variables in general) so that they cannot be masked.

I recently spotted that this can be a problem when creating a data frame with the name "new", which masked a function used by lmer and thus stopped it working. (Recovery is easy once you know what the problem is, here "rm(new)" did it.)

like image 350
Andi F Avatar asked Aug 10 '10 21:08

Andi F


People also ask

Can you have numbers in variable names R?

A variable name in R programming language can contain numeric and alphabets along with special characters like dot (.) and underline (-). Variable names in R language can begin with an alphabet or the dot symbol. However, if the variable name begins with a dot symbol it should not be a followed by a numeric digit.

Can you define a function within a function R?

A nested function or the enclosing function is a function that is defined within another function. In simpler words, a nested function is a function in another function. There are two ways to create a nested function in the R programming language: Calling a function within another function we created.

What is valid R variable name?

Nomenclature of R Variables A valid variable name consists of a combination of alphabets, numbers, dot(.), and underscore(_) characters. Example: var. 1_ is valid. Apart from the dot and underscore operators, no other special character is allowed.

How long can variable names be in R?

The following rules apply when creating new variables or changing the name of an existing variable: Variable names must be unique in a Dataset. Variable names are up to 64 characters long and can only contain letters, digits and nonpunctuation characters (except that a period (.) is allowed.


1 Answers

There is an easy workaround for your problem, without worrying about protecting variable names (though playing with lockBinding does look fun). If a function becomes masked, as in your example, it is still possible to call the masked version, with the help of the :: operator.

In general, the syntax is packagename::variablename.

(If the function you want has not been exported from the package, then you need three colons instead, :::. This shouldn't apply in this case however.)

like image 87
Richie Cotton Avatar answered Oct 21 '22 20:10

Richie Cotton