As far as I can tell, the R community has no generally-accepted style guide. Google and Hadley Wickham both have style guides, but across and even within CRAN packages, different naming and spacing conventions abound. You’re likely to find variables named in camelCase, snake_case, or, interestingly, dot.case. This last convention is unusual, because unlike many languages, R does not enforce specific syntactic meaning for dots. Dots can denote methods for S3 classes, but they don’t have to. This means that R only cares about dots sometimes, with confusing results.
S3 generic functions, like print
, use the function UseMethod
to
dispatch the appropriate method for the data type. The methods are
named using the construction function.class
(i.e., print.lm
,
print.data.frame
). Sometimes you can even call the methods
directly, without the generic. For example:
> print('hi')
[1] "hi"
> print.data.frame('hi')
NULL
<0 rows> (or 0-length row.names)
The character object "hi"
isn’t a data.frame
, but R will try to
call it like a normal function anyway. S3 is very unstructured in this
way. The problem is that R can’t always distinguish between
S3 generics/methods and dot case functions:
> example <- function(x) print(mean(x))
> example.two <- function(x) print(sum(x))
> methods(example)
[1] example.two
see '?methods' for accessing help and source code
Warning message:
In .S3methods(generic.function, class, parent.frame()) :
function 'example' appears not to be S3 generic; found functions that look like S3 methods
Here I’ve defined two unrelated functions, but R isn’t sure what to do
with them. example
isn’t an S3 generic, because I don’t call
UseMethod
to dispatch example
methods for different classes, but
because of S3 naming structure, example.two
looks like an S3 method
on the class two
. S3 is so informal that there is no checking to
distinguish between the possibilites, and the methods()
function
lists example.two
as a method (although at least it warns you that
it’s not sure).
This behavior is both unexpected and problematic. If you’re relying on
a specific behavior from methods()
, someone else’s package (or your
own functions!) could give you unreliable results. There is no reason to
use dot case except as convention, and it’s not a set convention, even
in base R. There are even base R functions that use a different case
for the function name and arguments (colSums (x, na.rm = FALSE, dims
= 1)
)! I’d love to see an R overhaul, where a new R version is
released with a consistent style and an accompanying style guide. But until
that time, R users should at least stop using dot case.