15

I've seen some R code with variable names that begin with a period:

.variable<-1
class(.variable) 
[1] 1
[1] "numeric"

It seems like such variables are in some sense "hidden", as they don't appear in the global environment of RStudio.

What are such variables used for? Does the leading period have some special meaning?

5
  • 2
    You can use ls(all.names = TRUE) to get all the object names Commented Jan 9, 2017 at 17:23
  • thx akrun but, the main target would be also to define it as a concept, they are hidden variables? which uses they have? some information to see their uses.. thx a lot Commented Jan 9, 2017 at 17:26
  • You can check here Commented Jan 9, 2017 at 17:27
  • This question is NOT a duplicate of #7526467. This question is asking about variable, whereas that question is asking about a function - and the answer is different for both! Please unmark this "duplicate". Commented Oct 17 at 11:26
  • @Tomas Function names are variables. The linked question absolutely provides a valid answer to this question. It provides additional information which in’t relevant for non-function variables, but that doesn’t invalidate it being a duplicate. As it stands I don’t think there’s harm in keeping both questions open. But this one’s definitely redundant (and what’s more, the answer to this question is pretty bad, despite the upvotes; the answers for the other question are better). Commented Nov 6 at 9:41

1 Answer 1

22

The prefix dot notation in R specifies a hidden object that cannot be accessed directly through ls unless you use ls(all.names = TRUE). The purpose of this is for developers of R packages to have some way to hide the implementation details of their functions from users, making their package more user-friendly, as described more fully on R-bloggers (and quoted briefly here in case of link rot):

Lets say that you are developing the function use_me(). If the details you want the users to control are actually arguments of other functions used inside use_me(), then you can simplify your function by using the ... argument. This argument is very well explained at The three-dots construct in R (Burns, 2013). It is very useful and can greatly simplify your life as a developer. Plus, it reduces the length of your help pages, thus making your package more user friendly.

However, if some of the details in use_me() are not arguments to other functions, then the common strategy is to write two functions. One is a low level function with arguments for all the details which might or might not export. Then, you write a second function that is a wrapper for the low level function and pre-specifies values for all the details. See the next minimal example:

# Don't export this function
.use_me <- function(arg1, arg2, verbose = TRUE) {
    if(verbose) message(paste(Sys.time(), 'working'))
    pmax(arg1, arg2)
}

#' @export
use_me <- function(arg1, ...) {
    .use_me(arg1, 0, ...)
}

This is very similar to Python's use of the single underscore to prevent automatically loading objects from packages. In both cases the practice appears to be a naming convention - outside of their specified uses (in R's case, hiding the object in the environment) there isn't really any other outcome of using the notation (according to the comments on this post at least, and after scanning the documentation myself).

For examples of this in actual use, see the help for colSums or trace.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.