0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

R パイプ演算子(%>%)をデフォルトで使えるようにする(Ubuntu & Windows10)

Last updated at Posted at 2019-12-06

Rのパイプ演算子はとても便利

パッケージmagrittrによって提供されるパイプ演算子は、複数の処理を連続で行う際にとても便利である。
tidyverseを使うときだけでなく、デフォルトパッケージのみで作業を行う際にもぜひ使いたい機能である。

しかし、tidyverseは少し重い

tidyverseを読み込めばその他の便利パッケージとともにパイプ演算子が使えるようになるが、tidyverseは(ほんの少し)読み込みが遅い。加えて、読み込むパッケージが多いためその分メモリーを消費する。

デフォルトでパイプ演算子を使えるようにしよう

ただ、毎回

> library(magrittr)

とするのは少し面倒くさい。
ということで、magritrのみをデフォルトで読み込むようにしたのでメモ。

magrittrのインストール

magrittrが入っていない場合は以下のコマンドでインストールする。(tidyverseを入れていてもmagrittrは入っていなかった気がするので一応確認する)

> install.packages("magrittr")

Ubuntuの場合

作業環境:Ubuntu 18.04
      R 3.6.1

Rprofile.siteの確認

以下のようにして、Rprofile.siteの場所を確認する。

~$ locate Rprofile.site
/etc/R/Rprofile.site
/usr/lib/R/etc/Rprofile.site

2つあるが、「/etc/R/Rprofile.site」を編集すれば「/usr/lib/R/etc/Rprofile.site」に反映されるので前者を選ぶ。

Rprofile.siteの編集

以下のようにして、「/etc/R/Rprofile.site」をエディタで開く。編集には管理者権限が必要。

~$ sudo gedit /etc/R/Rprofile.site

すると中身は次のようになっている

##						Emacs please make this -*- R -*-
## empty Rprofile.site for R on Debian
##
## Copyright (C) 2008 - 2018  Dirk Eddelbuettel and GPL'ed
##
## see help(Startup) for documentation on ~/.Rprofile and Rprofile.site

# ## Example of .Rprofile
# options(width=65, digits=5)
# options(show.signif.stars=FALSE)
# setHook(packageEvent("grDevices", "onLoad"),
#         function(...) grDevices::ps.options(horizontal=FALSE))
# set.seed(1234)
# .First <- function() cat("\n   Welcome to R!\n\n")
# .Last <- function()  cat("\n   Goodbye!\n\n")

# ## Example of Rprofile.site
# local({
#  # add MASS to the default packages, set a CRAN mirror
#  old <- getOption("defaultPackages"); r <- getOption("repos")
#  r["CRAN"] <- "http://my.local.cran"
#  options(defaultPackages = c(old, "MASS"), repos = r)
# })

## We set the cloud mirror, which is 'network-close' to everybody, as default
local({
    r <- getOption("repos")
    r["CRAN"] <- "https://cloud.r-project.org"
    options(repos = r)
})

書式例がきちんと書いてあるので、それを参考にして最後のlocal以下の部分を次のように書き換える。(ついでにレポジトリを統数研のミラーに変更する)

local({
   old <- getOption("defaultPackages"); r <- getOption("repos")
    r["CRAN"] <- "https://cran.ism.ac.jp/"
    options(defaultPackages = c(old, "magrittr"), repos = r)
})

これで完了。
確認すると、きちんとデフォルトでパイプが使えるようになっていることがわかる。

~$ R

R version 3.6.1 (2019-07-05) -- "Action of the Toes"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R は、自由なソフトウェアであり、「完全に無保証」です。 
一定の条件に従えば、自由にこれを再配布することができます。 
配布条件の詳細に関しては、'license()' あるいは 'licence()' と入力してください。 

R は多くの貢献者による共同プロジェクトです。 
詳しくは 'contributors()' と入力してください。 
また、R や R のパッケージを出版物で引用する際の形式については 
'citation()' と入力してください。 

'demo()' と入力すればデモをみることができます。 
'help()' とすればオンラインヘルプが出ます。 
'help.start()' で HTML ブラウザによるヘルプがみられます。 
'q()' と入力すれば R を終了します。 

> iris %>% summary
  Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
 Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
 1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
 Median :5.800   Median :3.000   Median :4.350   Median :1.300  
 Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
 3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
 Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
       Species  
 setosa    :50  
 versicolor:50  
 virginica :50  

Windows10の場合

作業環境:Windows10
      R 3.6.1

Rprofile.siteの確認

Windowsの場合、64bit版のRprofile.siteは「C:\Program Files\R\R-3.6.1\etc\Rprofile.site」にある。

Rprofile.siteの編集

エディタで開くと次のようになっている(管理者権限で開かないと編集できないので注意)

# Things you might want to change

# options(papersize="a4")
# options(editor="notepad")
# options(pager="internal")

# set the default help type
# options(help_type="text")
  options(help_type="html")

# set a site library
# .Library.site <- file.path(chartr("\\", "/", R.home()), "site-library")

# set a CRAN mirror
# local({r <- getOption("repos")
#       r["CRAN"] <- "http://my.local.cran"
#       options(repos=r)})

# Give a fortune cookie, but only to interactive sessions
# (This would need the fortunes package to be installed.)
#  if (interactive()) 
#    fortunes::fortune()


…Ubunutuと違って書式例が書いてない。しかし仕組みは一緒のはずなので、Ubuntuで変更した部分をこれに追加する。

local({
   old <- getOption("defaultPackages"); r <- getOption("repos")
    r["CRAN"] <- "https://cran.ism.ac.jp/"
    options(defaultPackages = c(old, "magrittr"), repos = r)
})

これで完了。
確認すると、こちらもパイプが使えるようになっていることがわかる。


R version 3.6.1 (2019-07-05) -- "Action of the Toes"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R は、自由なソフトウェアであり、「完全に無保証」です。 
一定の条件に従えば、自由にこれを再配布することができます。 
配布条件の詳細に関しては、'license()' あるいは 'licence()' と入力してください。 

R は多くの貢献者による共同プロジェクトです。 
詳しくは 'contributors()' と入力してください。 
また、R や R のパッケージを出版物で引用する際の形式については 
'citation()' と入力してください。 

'demo()' と入力すればデモをみることができます。 
'help()' とすればオンラインヘルプが出ます。 
'help.start()' で HTML ブラウザによるヘルプがみられます。 
'q()' と入力すれば R を終了します。 

> iris %>% summary
  Sepal.Length    Sepal.Width     Petal.Length    Petal.Width          Species  
 Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100   setosa    :50  
 1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300   versicolor:50  
 Median :5.800   Median :3.000   Median :4.350   Median :1.300   virginica :50  
 Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199                  
 3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800                  
 Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500                  
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?