INPUTしたらOUTPUT!

忘れっぽいんでメモっとく

Rでコーディングスタイルを適用させる方法

Hadleyの新刊ペラペラ見てたらコーディングスタイルに関する記載があったのでメモしとく。
formatR, lintrパッケージなどがあるようでそれぞれ試してみる。

R Packages

R Packages

以前のR勉強会@東京で発表した&された資料も参考までに。


formatRパッケージ

結構前からあるパッケージのようだけど存在知らなかった。使用する前に An Introduction to formatR を読めとのこと。

tidy_source()

Rスクリプトファイルを読込んでコンソールに整形されたコードを出すみたい。クリップボードにコピーされたソースも整形してくれる。

formatR確認用Rスクリプト


上記スクリプトクリップボードにコピーした状態でtidy_source()を実行すると整形されたコードがコンソールに表示される。

> library(formatR)
> tidy_source()
## comments are retained; a comment block will be reflowed if it contains long
## comments;
#' roxygen comments will not be wrapped in any case
1 + 1

if (TRUE) {
    x = 1  # inline comments
} else {
    x = 2
    print("Oh no... ask the right bracket to go away!")
}
1 * 3  # one space before this comment will become two!
2 + 2 + 2  # only 'single quotes' are allowed in comments

lm(y ~ x1 + x2, data = data.frame(y = rnorm(100), x1 = rnorm(100), x2 = rnorm(100)))  ### a linear model
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 
    1 + 1 + 1  ## comments after a long line


tidy_dir()

指定したディレクトリにあるRスクリプトファイルをまとめて整形してくれる。recursive=TRUEにするとネストされたディレクトリ内のスクリプトも整形されるみたい。

> tidy_dir()
tidying ./sample.R


tidy_app()

Rスクリプトを整形するWebアプリ(Shiny製)が起動する。インデントのスペース数と1行の文字数を指定してTidy My Codeボタンを押すと整形してくれる。すごい便利。

tidy_app()

f:id:tak95:20150407092325p:plain


lintrパッケージ

勝手にスクリプトを修正されると困る場合はlintrパッケージが使える。formatRが自動的に整形するのに対してlintrはwarningを出すだけ。 @soultoru氏のLT聞いてvimで試したときは上手く行かなかったけどRStudioでも使えるみたい。
キャメルケースやスネークケースを変数や関数などの名前に使用していると警告が出るけどパッケージ名でも出てしまう。twitteRパッケージなどもあるのでパッケージ名は見逃して欲しいところ。。。

以下実行結果。

> library(lintr)
> lint("sample.R")
sample.R:4:2: style: Put spaces around all infix operators.
1+1
~^~
sample.R:7:4: style: Use <-, not =, for assignment.
  x=1  # inline comments
   ^
sample.R:7:4: style: Put spaces around all infix operators.
  x=1  # inline comments
  ~^~
sample.R:9:4: style: Use <-, not =, for assignment.
  x=2;print('Oh no... ask the right bracket to go away!')}
   ^
sample.R:9:4: style: Put spaces around all infix operators.
  x=2;print('Oh no... ask the right bracket to go away!')}
  ~^~
sample.R:9:13: style: Only use double-quotes.
  x=2;print('Oh no... ask the right bracket to go away!')}
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sample.R:9:58: style: Closing curly-braces should always be on their own line, unless it's followed by an else.
  x=2;print('Oh no... ask the right bracket to go away!')}
                                                         ^
sample.R:10:2: style: Put spaces around all infix operators.
1*3 # one space before this comment will become two!
~^~
sample.R:11:2: style: Put spaces around all infix operators.
2+2+2    # only 'single quotes' are allowed in comments
~^~
sample.R:11:4: style: Put spaces around all infix operators.
2+2+2    # only 'single quotes' are allowed in comments
  ~^~
sample.R:13:1: style: lines should not be more than 80 characters.
lm(y~x1+x2, data=data.frame(y=rnorm(100),x1=rnorm(100),x2=rnorm(100)))  ### a linear model
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sample.R:13:8: style: Put spaces around all infix operators.
lm(y~x1+x2, data=data.frame(y=rnorm(100),x1=rnorm(100),x2=rnorm(100)))  ### a linear model
      ~^~
sample.R:14:2: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
~^~
sample.R:14:4: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
  ~^~
sample.R:14:6: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
    ~^~
sample.R:14:8: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
      ~^~
sample.R:14:10: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
        ~^~
sample.R:14:12: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
          ~^~
sample.R:14:14: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
            ~^~
sample.R:14:16: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
              ~^~
sample.R:14:18: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
                ~^~
sample.R:14:20: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
                  ~^~
sample.R:14:22: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
                    ~^~
sample.R:14:24: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
                      ~^~
sample.R:14:26: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
                        ~^~
sample.R:14:28: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
                          ~^~
sample.R:14:30: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
                            ~^~
sample.R:14:32: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
                              ~^~
sample.R:14:34: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
                                ~^~
sample.R:14:36: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
                                  ~^~
sample.R:14:38: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
                                    ~^~
sample.R:14:40: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
                                      ~^~
sample.R:14:42: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
                                        ~^~
sample.R:14:44: style: Put spaces around all infix operators.
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
                                          ~^~

Rコードを納品する方は納品前に是非チェックをお願いしたい。