Slide 1

Slide 1 text

最近のR パッケージ開発事情 最近のR パッケージ開発事情 LINE Fukuoka 株式会社 LINE Fukuoka 株式会社 前田和寛(Kazuhiro Maeda) 前田和寛(Kazuhiro Maeda) 2019/04/06 2019/04/06

Slide 2

Slide 2 text

why creating R package? why creating R package?

Slide 3

Slide 3 text

「なければ作る」 「なければ作る」 Sometimes, we look for R packages to do the processing we want to achieve However, the packages we seek may not exist (If we look for it carefully, it will exist) 「なければ作ればいい」

Slide 4

Slide 4 text

Procedure of R package dev Procedure of R package dev 1. Prepare environment for R package-dev 2. Create repository and R-project 3. Write codes, descriptions, documents 4. Build and Testing 5. Deploy 6. Create the information-site of this package

Slide 5

Slide 5 text

Prepare environment for R Prepare environment for R package-dev package-dev

Slide 6

Slide 6 text

Applications required for R-package dev Applications required for R-package dev git for version control R RStudio IDE for R web browser for access git(github) and check docs

Slide 7

Slide 7 text

R packages for dev R packages for dev devtools tool-set of R package dev roxygen2 useful package for generating/modifying descriptions usethis useful package for creating R package testthat for test rmarkdown for rendering documents pkgdown for creating web-site of this package

Slide 8

Slide 8 text

Create repository and R- Create repository and R- project project

Slide 9

Slide 9 text

Prepare R-project Prepare R-project Create R-project Create R-project this one: usethis::create_package("path_to_project_directory")

Slide 10

Slide 10 text

Add license Add license If you want to add “MIT” license, run this code: usethis::use_mit_license() Another licenses(CC0, GPL, etc …), see .

Slide 11

Slide 11 text

initialize git initialize git

Slide 12

Slide 12 text

Prepare git(github) Prepare git(github) Create git repository Create git repository

Slide 13

Slide 13 text

Copy repository’s url Copy repository’s url Please do not create any files at this point.

Slide 14

Slide 14 text

connect R-package and repository connect R-package and repository Click ‘Terminal’ tab and run these command: git remote add origin (repository_url) git add . git commit -m "first commit" git push --set-upstream origin master This is a kind of samples.

Slide 15

Slide 15 text

Write codes, descriptions, Write codes, descriptions, documents documents

Slide 16

Slide 16 text

Description method using roxygen2 Description method using roxygen2 Concepts Concepts The premise of roxygen2 is simple: describe your functions in comments next to their definitions and roxygen2 will process your source code and comments to produce Rd files in the man/ directory. How to write How to write It is a easier way to check this cheat sheet: https://www.rstudio.com/resources/cheatsheets/#package

Slide 17

Slide 17 text

example example from roxygen2 repository: #' The length of a string (in characters). #' #' @param string input character vector #' @return numeric vector giving number of characters in each element of #' character vector. Missing strings have missing length. #' @seealso \code{\link{nchar}} which this function wraps #' @export #' @examples #' str_length(letters) #' str_length(c("i", "like", "programming", NA)) str_length <- function(string) { string <- check_string(string) nc <- nchar(string, allowNA = TRUE) is.na(nc) <- is.na(string) nc }

Slide 18

Slide 18 text

example:

Slide 19

Slide 19 text

create documents create documents If you wrote this, you just need to do this: devtools::document() This function create documents. For example, help, vignette, DESCRIPTION(package meta info), NAMESPACE and so on.

Slide 20

Slide 20 text

Build and Testing Build and Testing

Slide 21

Slide 21 text

Build check Build check Click “Check” button on “Build” tab: If you get error or warning, fix it.

Slide 22

Slide 22 text

Build and test Build and test Click “Install and Restart” button on “Build” tab. After build and installed your package, execute and test the function to see if it works as intended:

Slide 23

Slide 23 text

Check help Check help If you have already run and executed , you could see the help documents you write:

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Test using testthat Test using testthat If you want to use automated test application(e.g., CircleCI), testthat package may help you. For more detail about testthat, please see the . package site

Slide 26

Slide 26 text

Deploy Deploy

Slide 27

Slide 27 text

Edit DESCRIPTION Edit DESCRIPTION You have to edit file … If you want add “package dependency”, use : usethis::use_package("name_of_package", type = "Imports")

Slide 28

Slide 28 text

Write README.md(or README.Rmd) Write README.md(or README.Rmd) Write!

Slide 29

Slide 29 text

How to deploy How to deploy It will be a quick way to publish to GitHub. You are already connected with github, so it’s OK if you push the project.

Slide 30

Slide 30 text

Release on GitHub Release on GitHub

Slide 31

Slide 31 text

Create the infomation-site of Create the infomation-site of this package this package

Slide 32

Slide 32 text

Create site using pkgdown package Create site using pkgdown package 1. install package 2. edit (or ) 3. run this code: pkgdown::build_site()

Slide 33

Slide 33 text

Yeah!

Slide 34

Slide 34 text

Deploy the site Deploy the site 1. git add -> git commit -> git push 2. access github and go to this repository 3. click “Setting” tab 4. activate “GitHub pages” choose “master branch /docs folder” at Source area 5. access and check it

Slide 35

Slide 35 text

Summary Summary

Slide 36

Slide 36 text

Let’s develop your package easily using Let’s develop your package easily using these packages! these packages! devtools roxygen2 usethis testthat rmarkdown pkgdown https://devtools.r-lib.org/ https://github.com/klutometis/roxygen https://usethis.r-lib.org/index.html https://testthat.r-lib.org/ https://bookdown.org/yihui/rmarkdown/ https://pkgdown.r-lib.org/

Slide 37

Slide 37 text

Enjoy! Enjoy!