What does the dot in R mean - personal preference, naming convention or more?

I (possibly) DO NOT refer to "all other variables", which means var1~. here. I pointed to plyr and looked in mlply and wondered why the parameters are defined with a leading point like this:

 function (.data, .fun = NULL, ..., .expand = TRUE, .progress = "none", .parallel = FALSE) { if (is.matrix(.data) & !is.list(.data)) .data <- .matrix_to_df(.data) f <- splat(.fun) alply(.data = .data, .margins = 1, .fun = f, ..., .expand = .expand, .progress = .progress, .parallel = .parallel) } <environment: namespace:plyr> 

What is the use of this? Is it just a personal preference, a naming convention or more? Often R is so functional that I miss a trick that has been done a long time ago.

+52
coding-style r naming-conventions plyr
Sep 23 2018-11-11T00:
source share
2 answers

A period in a function name can mean any of the following:

  • nothing at all
  • separator between method and class in S3 methods
  • to hide the function name



Possible Values

1. Nothing at all

A point in data.frame does not separate data from frame , except visually.

2. Separation of methods and classes in S3 methods

plot is one example of the general S3 method. Thus, plot.lm and plot.glm are the basic definitions of functions that are used when calling plot(lm(...)) or plot(glm(...))

3. To hide internal functions

When writing packages, it is sometimes useful to use leading points in function names, since these functions are somewhat hidden from the general view. Functions that should be purely internal to the package sometimes use this.

In this context, โ€œsomewhat hiddenโ€ simply means that a variable (or function) is usually not displayed when you specify an object using ls() . To make ls show these variables, use ls(all.names=TRUE) . Using a dot as the first letter of a variable, you change the scope of the variable itself. For example:

 x <- 3 .x <- 4 ls() [1] "x" ls(all.names=TRUE) [1] ".x" "x" x [1] 3 .x [1] 4 

4. Other possible causes

In the Hadley plyr package, it uses convention to use leading points in function names. It is like a mechanism to try to ensure that when resolving variable names, values โ€‹โ€‹are resolved to user variables, and not to internal function variables.




Complications

This mess of different uses can lead to very confusing situations, because these different applications can be implicated in the same function name.

For example, to convert a data.frame to a list, you use as.list(..)

 as.list(iris) 

In this case, as.list is a common S3 method, and you pass data.frame . So the S3 function is called as.list.data.frame :

 > as.list.data.frame function (x, ...) { x <- unclass(x) attr(x, "row.names") <- NULL x } <environment: namespace:base> 

And for something really impressive, download the data.table package and look at the as.data.table.data.frame function:

 > library(data.table) > methods(as.data.table) [1] as.data.table.data.frame* as.data.table.data.table* as.data.table.matrix* Non-visible functions are asterisked > data.table:::as.data.table.data.frame function (x, keep.rownames = FALSE) { if (keep.rownames) return(data.table(rn = rownames(x), x, keep.rownames = FALSE)) attr(x, "row.names") = .set_row_names(nrow(x)) class(x) = c("data.table", "data.frame") x } <environment: namespace:data.table> 
+81
Sep 23 2018-11-11T00:
source share
โ€” -

At the beginning of the name, it works as a convention with the UNIX file name to hide objects by default.

 ls() character(0) .a <- 1 ls() character(0) ls(all.names = TRUE) [1] ".a" 

It can be just a token, which does not have much significance, it does nothing more than any other allowed token.

 my.var <- 1 my_var <- 1 myVar <- 1 

It is used to send the S3 method. So, if I define a simple class "myClass" and create objects with this class attribute, then common functions like print () will automatically be sent to my specific printing method.

 myvar <- 1 print(myvar) class(myvar) <- c("myClass", class(myvar)) print.myClass <- function(x, ...) { print(paste("a special message for myClass objects, this one has length", length(x))) return(invisible(NULL)) } print(myvar) 

There is ambiguity in the S3 syntax, since you cannot define a function name, whether it be the S3 method or just a dot in the name. But this is a very simple mechanism, which is very powerful.

Each of these three aspects is much larger, and you should not take my examples as good practice, but they are the main differences.

+21
Sep 23 '11 at 9:15
source share



All Articles