why creating R package? · t o o l - s e t o f R pa cka g e dev roxygen2 u s e f u l pa cka g e f o...

Post on 29-Aug-2020

3 views 0 download

Transcript of why creating R package? · t o o l - s e t o f R pa cka g e dev roxygen2 u s e f u l pa cka g e f o...

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

2019/04/062019/04/06

why creating R package?why creating R package?

「なければ作る」「なければ作る」

Sometimes, we look for R packages to do theprocessing we want to achieve

However, the packages we seek may not exist(If we look for it carefully, it will exist)

「なければ作ればいい」

Procedure of R package devProcedure of R package dev1. 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

Prepare environment for RPrepare environment for Rpackage-devpackage-dev

Applications required for R-package devApplications required for R-package devgit

for version control

R

RStudioIDE for R

web browserfor access git(github) and check docs

R packages for devR packages for devdevtools

tool-set of R package dev

roxygen2useful package for generating/modifying descriptions

usethisuseful package for creating R package

testthatfor test

rmarkdownfor rendering documents

pkgdownfor creating web-site of this package

Create repository and R-Create repository and R-projectproject

Prepare R-projectPrepare R-projectCreate R-projectCreate R-project

this one:

usethis::create_package("path_to_project_directory")

Add licenseAdd license

If you want to add “MIT” license, run this code:

usethis::use_mit_license()

Another licenses(CC0, GPL, etc…), see ����������������.

initialize gitinitialize git

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

Copy repository’s urlCopy repository’s url

Please do not create any files at this point.

connect R-package and repositoryconnect R-package and repositoryClick ‘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.

Write codes, descriptions,Write codes, descriptions,documentsdocuments

Description method using roxygen2Description method using roxygen2ConceptsConcepts

The premise of roxygen2 is simple:describe your functions in comments nextto their definitions and roxygen2 willprocess your source code and commentsto produce Rd files in the man/ directory.

How to writeHow to write

It is a easier way to check this cheat sheet:https://www.rstudio.com/resources/cheatsheets/#package

exampleexample

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 }

example:

create documentscreate documentsIf you wrote this, you just need to do this:

devtools::document()

This function create documents. For example, help,vignette, DESCRIPTION(package meta info), NAMESPACEand so on.

Build and TestingBuild and Testing

Build checkBuild checkClick “Check” button on “Build” tab:

If you get error or warning, fix it.

Build and testBuild and testClick “Install and Restart” button on “Build” tab. Afterbuild and installed your package, execute and test thefunction to see if it works as intended:

Check helpCheck helpIf you have already run ��������������������� andexecuted �������������������, you could see the helpdocuments you write:

Test using testthatTest using testthatIf you want to use automated test application(e.g.,CircleCI), testthat package may help you.

For more detail about testthat, please see the .

packagesite

DeployDeploy

Edit DESCRIPTIONEdit DESCRIPTIONYou have to edit ����������� file…

If you want add “package dependency”, use����������������������:

usethis::use_package("name_of_package", type = "Imports")

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

How to deployHow to deployIt will be a quick way to publish to GitHub. You arealready connected with github, so it’s OK if you push theproject.

Release on GitHubRelease on GitHub

Create the infomation-site ofCreate the infomation-site ofthis packagethis package

Create site using pkgdown packageCreate site using pkgdown package1. install ������� package

2. edit ���������(or ����������)

3. run this code:

pkgdown::build_site()

Yeah!

Deploy the siteDeploy the site1. 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

SummarySummary

Let’s develop your package easily usingLet’s develop your package easily usingthese 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/

Enjoy!Enjoy!