any R style guide / checker?
Asked Answered
C

7

33

in Python I'm used to having my code "style-checked" by an automatic but configurable tool, called pep8, after the 8th Python enhancement proposal.

in R I don't know. Google has a style guide, but:

  • what do most R programmers actually use?
  • I still didn't find any program that performs those checks.

Dirk, Alex, in your answers you pointed me at pretty printers, but in my opinion that would overdo one thing and not do another: code would be automatically edited to follow the style, while no warnings are issued for poorly chosen identifiers.

Causeuse answered 25/2, 2011 at 12:43 Comment(4)
Related question : #4407373Bewley
The other style guide that's available is Henrik Bengtsson's. A canonical one would be nice, but there ain't one (and there's plenty of disagreement between the existing ones)Aeciospore
This could be useful: cran.r-project.org/web/packages/lint/lint.pdfKohler
As I mentioned in a comment further down the page, the goodpractice R module gives "... advice about good practices when building R packages. Advice includes functions and syntax to avoid, package structure, code complexity, code formatting, etc." I believe it uses lintr and other similar R packages internally and is under somewhat active development on github.Trioxide
S
11

I think if you want such a tool, you may have to write it yourself. The reason is that R does not have an equivalent to Python's PEP8; that is, an "official style guide" that has been handed down from on high and is universally followed by the majority of R programmers.

In addition there are a lot of stylistic inconsistencies in the R core itself; this is a consequence of the way in which R evolved as a language. For example, many functions in R core follow the form of foo.bar and were written before the S3 object system came along and used that notation for method dispatch. In hindsight, the naming of these functions should probably be changed in the interests of consistency and clarity, but it is too late to consider that now.

In summary, there is no official "style lint" tool for R because the R Core itself contains enough style lint, of which nothing can be done about, that writing one would be very difficult. For every rule--- "don't do this" ---there would have to be a long list of exceptions--- "except in this case, and this case, and this one, and ..., where it was done for historical purposes".

Speak answered 26/2, 2011 at 19:39 Comment(5)
writing it myself, I was already considering it, either by altering the R compiler or by adapting an other "lint" to work with R. but I am aware that this is a huge enterprise.Causeuse
but, about the too many styles in the R Core, I don't see the problem: a style checker would complain about what I define, not about what I use. and I would not ask it to check the R Core! :)Causeuse
starting from the wikipedia page for lint, I stumbled upon Yasca. what about a plugin for it?Causeuse
@moriotomo: Functions from R Core provide many of the building blocks for user code. The more core functions you end up using in your own code, the more intelligent your style checker will have to be.Speak
Hadley's recs are for the most part followed by lintr.Diversion
L
22

There's a formatR package with tidy.source function. I use Emacs with ESS, and follow Hadley's style recommendations. It's hard to compare R with Python, since style is kind of mandatory in Python, unlike R. =)

EDIT
a simple demonstration:

code <- "fn <- function(x, y) { paste(x, '+', y, '-', x+y) }"
tidy.source(text = code)
## not run
fn <- function(x, y) {
    paste(x, "+", y, "-", x + y)
}
Learned answered 25/2, 2011 at 13:37 Comment(4)
which looks useful for poorly formatted code you inherited somehow. but I'm looking for something different, that just tells me where I'm not following a style, leaving me the task to do something about it (or not).Causeuse
I'm not sure if there's a tool like that, but thank you for giving me an idea for a web-application. =)Learned
why a "web-application"? anyhow, if you GPL it and hold the sources on a public repository, I'd be interested in following the development!Causeuse
well... because I tend to think of myself as a web-developer (apart from the gig in statistics and R). It shouldn't be that hard... I guess =) And yes, if I decide to develop a tool like that, it's definitely going to published under GPL. It makes no sense otherwise...Learned
S
11

I think if you want such a tool, you may have to write it yourself. The reason is that R does not have an equivalent to Python's PEP8; that is, an "official style guide" that has been handed down from on high and is universally followed by the majority of R programmers.

In addition there are a lot of stylistic inconsistencies in the R core itself; this is a consequence of the way in which R evolved as a language. For example, many functions in R core follow the form of foo.bar and were written before the S3 object system came along and used that notation for method dispatch. In hindsight, the naming of these functions should probably be changed in the interests of consistency and clarity, but it is too late to consider that now.

In summary, there is no official "style lint" tool for R because the R Core itself contains enough style lint, of which nothing can be done about, that writing one would be very difficult. For every rule--- "don't do this" ---there would have to be a long list of exceptions--- "except in this case, and this case, and this one, and ..., where it was done for historical purposes".

Speak answered 26/2, 2011 at 19:39 Comment(5)
writing it myself, I was already considering it, either by altering the R compiler or by adapting an other "lint" to work with R. but I am aware that this is a huge enterprise.Causeuse
but, about the too many styles in the R Core, I don't see the problem: a style checker would complain about what I define, not about what I use. and I would not ask it to check the R Core! :)Causeuse
starting from the wikipedia page for lint, I stumbled upon Yasca. what about a plugin for it?Causeuse
@moriotomo: Functions from R Core provide many of the building blocks for user code. The more core functions you end up using in your own code, the more intelligent your style checker will have to be.Speak
Hadley's recs are for the most part followed by lintr.Diversion
C
6

As for

what do most R programmers actually use

I suspect that quite a few people follow R Core who have a R Coding standards section in the R Internals manual.

Which in a large sense falls back to these sensible Emacs defaults to be used along with ESS. Here is what I use and it is only minimally changed:

;;; C
(add-hook 'c-mode-hook
          ;;(lambda () (c-set-style "bsd")))
          ;;(lambda () (c-set-style "user"))) ; edd or maybe c++ ?
          (lambda () (c-set-style "c++"))) ; edd or maybe c++ ?
;;;; ESS
(add-hook 'ess-mode-hook
          (lambda ()
            (ess-set-style 'C++)
        ;; Because
            ;;                                 DEF GNU BSD K&R C++
            ;; ess-indent-level                  2   2   8   5   4
            ;; ess-continued-statement-offset    2   2   8   5   4
            ;; ess-brace-offset                  0   0  -8  -5  -4
            ;; ess-arg-function-offset           2   4   0   0   0
            ;; ess-expression-offset             4   2   8   5   4
            ;; ess-else-offset                   0   0   0   0   0
            ;; ess-close-brace-offset            0   0   0   0   0
            (add-hook 'local-write-file-hooks
                      (lambda ()
                        (ess-nuke-trailing-whitespace)))))
(setq ess-nuke-trailing-whitespace-p t)

As for a general, tool Xihui's formatR pretty-printer may indeed be the closest. Or just use ESS :)

Cheer answered 25/2, 2011 at 14:24 Comment(3)
wow. the R coding standard says close to nothing about style?Causeuse
For those of use without emacs-fu, what should we be looking at in the last code block? Is the K&R column indicating that indents in R should be 5 spaces?Aeciospore
No, as I understand it the last column (C++) is selected. Everything is in fours, which I like in other languages too.Cheer
K
4

The lint package gives warnings about stylistic problems, without correcting those. Running the lint() command (using the default parameter values) gives you a list of warnings for all R files in the current directory.

Katheleenkatherin answered 1/7, 2014 at 20:21 Comment(3)
The lint package has been archived on CRAN sometime between now and when this answer was made, which may mean it is no longer a good option for consideration.Trioxide
@Trioxide There is however the newer lintr (@USER_1 mentions it in their answer below). Its newer and maintained.Rangel
@Rangel The goodpractice R module gives "... advice about good practices when building R packages. Advice includes functions and syntax to avoid, package structure, code complexity, code formatting, etc." I believe it uses lintr and other similar R packages internally and is under somewhat active development on github.Trioxide
A
4
Amenity answered 9/2, 2017 at 15:48 Comment(0)
P
4

I use styler and then lintr before I check anything into version control.

styler converts your code base to match a given style - the default matches the tidyverse style described here. It modifies alignments, and some syntax (<- over =). But, it doesn't rename variables or anything like that.

lintr is non-modifying. It just identifies lines of code that are inconsistent with your style guide. I use this within vim when I'm working on a package or a project to identify things that need a bit more human input to fix (renaming variables/functions etc)

Pot answered 13/2, 2018 at 14:21 Comment(0)
E
1

RStudio has added a style checker at some point in the past. For instance, in version 1.1.463 you can enable the feature under General Options. Here's a screenshot:

enter image description here

Expecting answered 14/2, 2019 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.