48
35

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 1 year has passed since last update.

"install.packages()"するのはこれが最後!Rのパッケージ管理には「pacman」を使おう

Last updated at Posted at 2019-05-28

#いつもスクリプトの最初にこんな感じで書きますよね、、、

somesetup.R
# パッケージ無かったらインストール、
if (!require("dplyr")) install.packages("dplyr")
if (!require("tidyr")) install.packages("tidyr")
if (!require("dbplyr")) install.packages("dbplyr")
if (!require("magrittr")) install.packages("magrittr")
if (!require("ggplot2")) install.packages("ggplot2")

#パッケージ読み込む
library(dplyr)
library(tidyr)
library(dbplyr) 
library(magrittr)
library(ggplot2)

#これを、1行で書けたら幸せじゃ無いかと

pacman.R
pacman::p_load(dplyr, tidyr, dbplyr, magrittr, ggplot2)

書けたぞすごいぞ pacman、めっちゃ便利です。
R3.4辺りからは標準でCRANに入ってます。
公式: https://github.com/trinker/pacman

他にも便利な機能が色々:一括アンロード

pacman_unload.R
# 全てのパッケージを一括アンロード
p_unload("all")
> p_unload("all")
The following packages have been unloaded:jsonlite, magrittr, rdrop2, aws.s3, googledrive, dbplyr, pacman, slackr, maps, rvest, xml2, lubridate, httr, forcats, stringr, dplyr, purrr, readr, tidyr, tibble, tidyverse, testthat, ggplot2

一撃で全部アップデートしたり...

# これだけ!
pacman::p_update()

試験的に使うパッケージはp_temp()が便利

pacman.R
# セッション内だけで有効な、一時的パッケージ読み込み
p_temp(aprof)

p_isinstalled(aprof)
p_isloaded(aprof)
> p_temp(aprof)
trying URL 
  ...(中略)...
aprof installed
Loading required package: aprof

> p_isinstalled(aprof)
[1] FALSE   # 入ってない!
> p_isloaded(aprof)
aprof 
 TRUE       # でもロードされてる!

これで %>% select()MASS にマスクされて混乱する事も無いぞ!

一覧したり...

pacman::p_lib() # インストール済みパッケージ一覧
> pacman::p_lib()
  [1] "askpass"              "assertive"            "assertive.base"      
  [4] "assertive.code"       "assertive.data"       "assertive.data.uk"   
  [7] "assertive.data.us"    "assertive.datetimes"  "assertive.files"     
 [10] "assertive.matrices"   "assertive.models"     "assertive.numbers"   
 [13] "assertive.properties" "assertive.reflection" "assertive.sets"      
 [16] "assertive.strings"    "assertive.types"      "assertthat"          
 [19] "aws.s3"               "aws.signature"        "backports"           
 [22] "base64enc"            "BH"                   "binman"        
...

関数一覧見たり...

p_funs(pacman) # ファンクション一覧
> p_funs(pacman)
 [1] "p_author"             "p_base"               "p_boot"              
 [4] "p_citation"           "p_cite"               "p_cran"              
 [7] "p_data"               "p_del"                "p_delete"            
[10] "p_depends"            "p_depends_reverse"    "p_detectOS"          
[13] "p_exists"             "p_extract"            "p_functions"         
[16] "p_funs"               "p_get"                "p_help"              
[19] "p_info"               "p_information"        "p_install"           
[22] "p_install_gh"         "p_install_version"    "p_install_version_gh"
[25] "p_inter"              "p_interactive"        "p_iscran"            
[28] "p_isinstalled"        "p_isloaded"           "p_lib"               
[31] "p_library"            "p_load"               "p_load_current_gh"   
[34] "p_load_gh"            "p_loaded"             "p_news"              
[37] "p_old"                "p_opendir"            "p_path"              
[40] "p_sa"                 "p_search_any"         "p_search_library"    
[43] "p_sl"                 "p_temp"               "p_unload"            
[46] "p_unlock"             "p_up"                 "p_update"            
[49] "p_ver"                "p_ver_cran"           "p_ver_diff"          
[52] "p_version"            "p_version_cran"       "p_version_diff"      
[55] "p_vign"               "p_vignette"    

なにやら便利そうなファンクション名が並んでますよね。

さぁきみも今日からpacmanしてみよう!

48
35
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
48
35

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?