http://www.hmwu.idv.tw
http://www.hmwu.idv.tw
吳漢銘 國立政治大學 統計學系
制作 R 套件
( 使用RStudio)
F04
R 套件 ( R packages )
A collection of source code allows the user to attach to R session.
在R中,使用 library(pk.name) or require(pk.name)呼叫套件.
官方套件: http://cran.r-project.org/web/packages/
2/24
範例
http://www.hmwu.idv.tw
為什麼要有套件?
組織程式碼。
以套件方式共享程式碼。
提供可信賴的存取。
提供作者與使用者的溝通橋樑.
3/24
12585
「制作R套件」官方文件
4/24http://www.hmwu.idv.tw
制作R套件步驟
安裝必要軟體及R套件
建立 package skeleton (檔案目錄結構)
編輯/撰寫R程式(*.R)
利用"roxygen2"套件,撰寫R程式說明檔(*.Rd)
建立R程式說明檔及NAMESPACE檔
編輯DESCRIPTION檔
增加資料檔、增加demos or vignettes
檢測套件(Check)
重覆上述程序數次
編譯並建立套件(Build)
發佈套件(Distribute) (if you want to)
5/24
制作R套件所需的軟體安裝 (Windows)
6/24(1) GNU software development tools including a C/C++ compiler;
Rtools: https://cran.rstudio.com/bin/windows/Rtools/
(2) LaTeX for building R manuals and vignettes.
MikTeX: http://miktex.org/download
Other platform:
https://support.rstudio.com/hc/en-us/articles/200486498-Package-Development-Prerequisites
http://www.hmwu.idv.tw
安裝過程
7/24安裝完畢,檢查Rtools路徑
必要時重開機。
8/24
開始 => 控制台 => 系統
http://www.hmwu.idv.tw
利用RStudio新增一套件專案
9/24填入「套件名稱」
10/24若已有現成R 程式碼,可由
此加入
http://www.hmwu.idv.tw
產生套件樣版
11/24待會加入R程式及 資料檔
修改"DESCRIPTION"
12/24範例
http://www.hmwu.idv.tw
撰寫R程式及說明檔
13/24roxygen2: Generating Rd files
https://cran.r-project.org/web/packages/roxygen2/vignettes/rd.html
利用roxygen2,建立R說明檔(*.Rd)
14/24Generating .Rd files:
- roxygen2::roxygenise(), or
- devtools::document(), if you're using devtools
install.packages("roxygen2")
http://www.hmwu.idv.tw
建立套件專用資料 (*.rda)
15/24套件檢測(Check)及編譯(Build)
16/24http://www.hmwu.idv.tw
安裝新建立的套件
17/24若有問題,先刪除此套件
18/24rm(list=ls())
detach("package:MyFirstRPackage", unload=TRUE) remove.packages("MyFirstRPackage")
再重新安裝一次
http://www.hmwu.idv.tw
使用套件提供之指令
19/24安裝成功後,載入套件使用。
並查詢指令。
> .libPaths()
[1] "C:/Users/userpc/Documents/R/win-library/3.4"
[2] "C:/Program Files/R/R-3.4.0/library"
套件結構: 目錄
chtml/ : windows help files
data/ : contains files with data (txt, csv, rda)
demo/ : demonstrate some of the functionality of the package
exec/ : contain additional executables the package needs, typically scripts for interpreters such as the shell, Perl, or Tcl.
html/ : help file for each function
inst/ : contents are copied to installed package
man/ : contain (only) documentation files for the objects in the package in R documentation (Rd) format
R/ : R codes
src/ : contains C, Fortran codes to compile with the package
tests/ : R code for testing package functions
CONTENTS
DESCRIPTION
INDEX
NAMESPACE
20/24
https://methodsblog.wordpress.com/2015/11/30/building-your-first-r- package/
http://www.hmwu.idv.tw
撰寫使用說明 (vignettes)
R packages allow the
inclusion of documents in arbitrary other formats.
The standard location for these is subdirectory
inst/doc of a source
package, the contents will be copied to subdirectory doc when the package is installed.
21/24
提交套件到CRAN
CRAN Repository Policy
https://cran.r-project.org/web/packages/policies.html
Releasing a package
http://r-pkgs.had.co.nz/release.html
Getting your R package on CRAN
http://kbroman.org/pkg_primer/pages/cran.html
Submitting your first package to CRAN, my experience
https://www.r-bloggers.com/submitting-your-first-package-to-cran-my-experience/
Submit package to CRAN
https://cran.r-project.org/submit.html
22/24
http://www.hmwu.idv.tw
相關參考資料
23/24http://r-pkgs.had.co.nz/
A Quickstart Guide for Building Your First R Package
https://methodsblog.wordpress.com/2015/11/30/building-your-first-r-package/
Create an R Package in RStudio
https://www.youtube.com/watch?v=9PyQlbAEujY
除錯
24/24update.packages(ask = FALSE)