關於R Package的二三事

Taiwan R User Group

2013-04-29

Wush Wu

R 經歷

R 著作

大綱

簡介

為什麼要使用R Package

目前有超過4000個R Packages,大大的擴充了R 的功能

使用R Package的注意事項

品質參差不齊,不保證正確

為什麼要建立R Package

Reproducibility

使用R Package

讓我們簡單的回顧,如何使用別人寫好的Package吧!

搜尋

想要尋找發e-mail相關的R套件嘛?

搜尋R Package -- R 套件集散地

  • CRAN: 超過4000個套件
  • Bioconductor: 約600個套件,主要和生物相關
  • Omegahat: 主要是R 和其他語言的套件

安裝

install.packages("rredis")

出錯了?請看看log

閱讀說明

library(rredis)
# library(help=rredis)
example(redisConnect)
## 
## rdsCnn> ## Not run: 
## rdsCnn> ##D redisConnect()
## rdsCnn> ##D redisSet('x',runif(5))
## rdsCnn> ##D redisGet('x')
## rdsCnn> ##D redisClose()
## rdsCnn> ## End(Not run)
## rdsCnn> 
## rdsCnn> 
## rdsCnn>

使用

library(rredis)

建立R Package

R Package 結構簡介

使用Rstudio建立R Package

使用Rstudio安裝R Package

將功能放置於R目錄之內

rdemo_hello_world <- function() {
    print("hello world")
}

Build --> Configure Build Tools...

Build --> Build All

library(RDemo)
rdemo_hello_world()  # 找不到吧
Rdemo:::rdemo_hello_world()  # 隱藏在namespace:Rdemo

使用roxygen2 建立文件: export function

#'@export
rdemo_hello_world <- function() {
    print("hello world")
}

使用roxygen2 建立文件: man

#'@title This is title
#'
#'@description This is description
#'
#'@details This is detials
#'
#'@param msg greeting message
#'@examples
#'rdemo_hello_world('examples')
#'
#'@seealso \link{package.skeleton}
#'@export
rdemo_hello_world <- function(msg) {
    print("hello world")
    print(msg)
}

使用R CMD check編寫測試

測試script

library(Rdemo)
rdemo_hello_world("greeting!")
放置於 tests 底下

Build --> Check Packages

使用R CMD build發佈R Package

Build --> Build Source Package

發佈前最好先對RDemo_1.0.tar.gz做檢查:

R CMD check RDemo_1.0.tar.gz
R CMD check --as-cran RDemo_1.0.tar.gz 

Windows Binary

http://win-builder.r-project.org/

Q&A

參考資料、引用資料

圖片來源