• 沒有找到結果。

R 套件 ( R packages )

N/A
N/A
Protected

Academic year: 2021

Share "R 套件 ( R packages )"

Copied!
24
0
0

加載中.... (立即查看全文)

全文

(1)

http://www.hmwu.idv.tw

http://www.hmwu.idv.tw

吳漢銘 國立政治大學 統計學系

制作 R 套件

( 使用RStudio)

F04

(2)

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

範例

(3)

http://www.hmwu.idv.tw

為什麼要有套件?

組織程式碼。

以套件方式共享程式碼。

提供可信賴的存取。

提供作者與使用者的溝通橋樑.

3/24

12585

(4)

「制作R套件」官方文件

4/24

(5)

http://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

(6)

制作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

(7)

http://www.hmwu.idv.tw

安裝過程

7/24

(8)

安裝完畢,檢查Rtools路徑

必要時重開機。

8/24

開始 => 控制台 => 系統

(9)

http://www.hmwu.idv.tw

利用RStudio新增一套件專案

9/24

(10)

填入「套件名稱」

10/24

若已有現成R 程式碼,可由

此加入

(11)

http://www.hmwu.idv.tw

產生套件樣版

11/24

待會加入R程式及 資料檔

(12)

修改"DESCRIPTION"

12/24

範例

(13)

http://www.hmwu.idv.tw

撰寫R程式及說明檔

13/24

roxygen2: Generating Rd files

https://cran.r-project.org/web/packages/roxygen2/vignettes/rd.html

(14)

利用roxygen2,建立R說明檔(*.Rd)

14/24

Generating .Rd files:

- roxygen2::roxygenise(), or

- devtools::document(), if you're using devtools

install.packages("roxygen2")

(15)

http://www.hmwu.idv.tw

建立套件專用資料 (*.rda)

15/24

(16)

套件檢測(Check)及編譯(Build)

16/24

(17)

http://www.hmwu.idv.tw

安裝新建立的套件

17/24

(18)

若有問題,先刪除此套件

18/24

rm(list=ls())

detach("package:MyFirstRPackage", unload=TRUE) remove.packages("MyFirstRPackage")

再重新安裝一次

(19)

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"

(20)

套件結構: 目錄

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/

(21)

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

(22)

提交套件到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

(23)

http://www.hmwu.idv.tw

相關參考資料

23/24

http://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)

除錯

24/24

update.packages(ask = FALSE)

參考文獻

相關文件

— John Wanamaker I know that half my advertising is a waste of money, I just don’t know which half.. —

• A language in ZPP has two Monte Carlo algorithms, one with no false positives and the other with no

Experiment code often includes messy scripts for various settings in the paper – useful for reviewers Software: for general users. One or a few reasonable settings with a

This document uses the terminology defined in the UPnP Architecture document, such as: action, SST variable, and action parameter. This sub-section defines the following

The results contain the conditions of a perfect conversion, the best strategy for converting 2D into prisms or pyramids under the best or worth circumstance, and a strategy

[r]

Besides adopting effective strategies, such as flexible grouping, differentiating learning tasks and promoting self-access learning, to cater for learners’ diverse needs, due

The Seed project, REEL to REAL (R2R): Learning English and Developing 21st Century Skills through Film-making in Key Stage 2, aims to explore ways to use film-making as a means