2019年版Rパッケージ開発の手引き / pkg_dev

D12a80cab206033a820ccff8319f957b?s=47 Uryu Shinya
November 16, 2019

2019年版Rパッケージ開発の手引き / pkg_dev

D12a80cab206033a820ccff8319f957b?s=128

Uryu Shinya

November 16, 2019
Tweet

Transcript

  1. ύοέʔδ ։ൃͷखҾ͖ ೥൛ 191116Spa Tech#02 Uryu Shinya @u_ribo #spatechjp

  2. உͷՁ஋͸ ࡞ͬͨύοέʔδͰܾ·Δ https://www.slideshare.net/TokorosawaYoshio/r45lt Tokorosawa Yoshio (2015)

  3. https://www.slideshare.net/TokorosawaYoshio/r45lt Tokorosawa Yoshio (2015) ࣭͡Όͳ͍ྔͳΜͩ

  4. ύοέʔδ࡞Ζ͏ օ͞Μͷύοέʔδ։ൃͷ ҙཉ޲্͕ ຊൃදͷ໨తͰ͢ï

  5. ࢲʹ͍ͭͯ CRAN 5 + (1) GitHub 20+ \TG^ UJEZWFSTF SMJCܥͷύοέʔδ΁ͷ$POUSJCVUJPO

    Ἒ৓ݝͭ͘͹ࢢ͔Βདྷ·ͨ͠ʢผ෎ࢢ͸౓໨ʣ >1000 km 1SJWBUFؚΉ
  6. ୭͕࢖͏ͷʁ ύοέʔδ։ൃ͸͍͍͚Ͳʜ

  7. ԶͩΑ ʮԶʯ͸ଞʹ΋͍Δ

  8. ୭ɾԿͷͨΊͷύοέʔδʁ ࣗ෼ࣗ਎ͷ ࣌ؒͷઅ໿ ·ͣ͸ࣗ෼ͷ࡞ۀΛ ޮ཰͢ΔͨΊͷॲཧΛ ؔ਺Խʢ໨҆ͭҎ্ʣ ˠύοέʔδʹ੔ཧ

  9. ୭ɾԿͷͨΊͷύοέʔδʁ ଞऀ ࣗ෼ࣗ਎ͷ ࣌ؒͷઅ໿ ڞ༗

  10. ୭ɾԿͷͨΊͷύοέʔδʁ ଞऀ ࣗ෼ࣗ਎ͷ ࣌ؒͷઅ໿ ڞ༗ ίϛϡχςΟ ٕज़ଅਐ ྫ͑͹ {magrittr} %>%

    {rlang} !!,quo() ڞಉ࡞ۀऀ΍ୈࡾऀͷ ख఻͍ͱͳΔΑ͏ʹ ʢࣗ෼͕ͦ͏Ͱ͋ΔΑ͏ʹʣ
  11. \KQNFTI^͕Ͱ͖Δ·Ͱ ݩʑ͸ϝογϡίʔυΛฦ٫͢Δؔ਺͕΢Σϒ্Ͱެ։͞Ε͍ͯͨ ύοέʔδͱͯ͠੔උ ࡉ෼ϝογϡʹରԠʢݩ͸࣍ϝογϡ·Ͱʣ ۙ๣ͷϝογϡίʔυͷฦ٫ͳͲͷಠࣗͷػೳ௥Ճ TG࿈ܞ http://takenaka-akio.org/etc/meshcode/index.html ࣮૷͕Πέͯͳ͍ͱ͜Ζ͕͋ͬͨ ύοέʔδԽͷษڧΛͯ͠Έ͔ͨͬͨ ࠷ॳͷϦϦʔε͔Βঃʑʹػೳ௥Ճ

  12. \KQNFTI^͕Ͱ͖Δ·Ͱ ػೳ௥Ճͱ վળʹΑΓ ҭ͍ͯͯ͘ ͍͖ͳΓେ͖ͳ΋ͷ͸࡞Εͳ͍

  13. ΋ͷͮ͘Γͷتͼ JTTVFΛॻ͍ͯ͘Εͨͷ͸ ྡͷݚڀࣨͷֶੜʂʂ ʢޱ಄Ͱ΋΍ΓͱΓͯ͠ରԠʣ

  14. ΋ͷͮ͘Γͷتͼ ϝϞɿGHESύοέʔδͰج൫஍ਤ৘ใͷ%&.σʔλΛTUBSTͱͯ͠ಡΈࠐΈSBZTIBEFS͢Δ IUUQTOPUDIBJOFEIBUFOBCMPHDPNFOUSZ

  15. ಺༰ ύοέʔδͱ͸Կ͔ ։ൃ؀ڥͱछͷਆث ։ൃͷखॱ 1 2 3

  16. ύοέʔδͱ͸Կ͔ ύοέʔδ਺ʜ ೥݄೔࣌఺ 15,255 $3"/ 5IF$PNQSFIFOTJWF3"SDIJWF/FUXPSL  install.packages("x") library(x) package

    ::function()
  17. ύοέʔδͱ͸Կ͔ https://CRAN.R-project.org/package=jpmesh όʔδϣϯ ґଘؔ܎ ϥΠηϯε ։ൃऀɺ؅ཧऀ 3&"%.& /&84 ϚχϡΞϧ ࢖͍ํͳͲΛࣔ͢

    WJHOFUUFT ύοέʔδ໊
  18. ύοέʔδͱ͸Կ͔ جຊతͳύοέʔδʢιʔεύοέʔδʣͷߏ੒ R data man tests .R .R .Rd .Rd

    .rds Rout.save ΍ .R .Rbuildignore DESCRIPTION LICENSE NEWS README vignettes src inst data-raw ඞཁʹԠͯ͡ ϑΥϧμΛ ഑ஔ ༨ܭͳϑΝΠϧ͕͋Δͱ DIFDLͰౖΒΕΔ
  19. LOGO from https://github.com/rstudio/hex-stickers 3ύοέʔδ։ൃछͷਆث

  20. 3ύοέʔδ։ൃछͷਆث ύοέʔδ։ൃͰඞཁͳϑΝΠϧɺϑΥϧμͷ࡞੒ %&4$3*15*0/΁ͷґଘύοέʔδͷద༻ (JUόʔδϣϯ؅ཧػೳͱͷ࿈ܞ ͳͲ ʢύοέʔδ։ൃҎ֎ͷ৔໘Ͱ΋໾ཱͪ·͢ʣ r-lib/usethis

  21. 3ύοέʔδ։ൃछͷਆث ։ൃதͷύοέʔδͷಡΈࠐΈ΍ςετͷ࣮ߦɺ ύοέʔδͷϏϧυ·ͰΛ͜ͳ͢ɻ $3"/Ҏ֎ͷϦϙδτϦʢྫ͑͹(JU)VCͳͲʣ ্ͷύοέʔδͷΠϯετʔϧΛߦ͏ ˠػೳ͕๛෋ͳͷͰ͍͔ͭ͘ͷػೳʹಛԽͨ͠ ɹύοέʔδ͕ݸผʹ༻ҙ͞Ε͍ͯ·͢ FH\SFNPUFT^ \QLHMPBE^ \SDNEDIFDL^

    r-lib/devtools
  22. 3ύοέʔδ։ൃछͷਆث UFTUUFTUUIBU಺ͰͷςετϑΝΠϧੜ੒ ୯ମςετͷࣗಈ࣮ߦ ؀ڥɺ৚݅ʹԠͨ͡ॊೈͳεΩοϓػೳ ೿ੜύοέʔδ΋ෳ਺͋Γ·͢ɻ FH\XJUIS^ \NPDLS^ \UTU^ r-lib/testthat

  23. ։ൃ؀ڥ 34UVEJP ౷߹։ൃ؀ڥ Λ࢖͏ͷ͕ϕλʔ

  24. ύοέʔδ։ൃ·ͰͷྲྀΕ Ұྫ ಈػ෇͚ طଘͷύοέʔδ͕͋Δʁ ࣮૷͸ΠέͯΔʁ վળ఺͸ͳ͍͔ʁ YES NO ؔ਺ͷ࣮૷͸Մೳʁ *TTVFʹه࿥

    13ΛૹΔ 044ͱͯ͠ެ։ʁ ύοέʔδ໊Λߟ͑Δ create_package() Մจࣈɺ਺ࣈɺϐϦΦυ ઌ಄͸จࣈͰ࢝·Δ͜ͱɺ ຤ඌʹϐϦΦυ͸࢖͑ͳ͍
  25. ύοέʔδ։ൃ·ͰͷྲྀΕ Ұྫ YES NO ؔ਺Λॻ͘ υΩϡϝϯτΛ੔උ͢Δ ςετίʔυΛ༻ҙ͢Δ ίʔυΛચ࿅ͤ͞Δ ςετɺνΣοΫΛ௨͢ $3"/ొ࿥Λ໨ࢦ͢

    ࠓ೔͸࿩͠·ͤΜ test(),check() Ϗϧυͯ͠ڞ༗ɺ044Ͱެ։ (JU)VC͕ओྲྀ ଞʹ(JU-BC΍#JCVDLFUͳͲ build(),install()
  26. GPSTQBUFDI Թઘ෼ੳද Φʔϓϯσʔλ͋Δʁ Խֶత੒෼ͷ෼ੳ Ϛοϐϯά͢Δ ΞϓϦέʔγϣϯ 4IJOZͰ 3NEͰϨϙʔτ࡞੒

  27. :PV 3FBEZ library(usethis) v1.5.1 create_package(“spatech")

  28. ؔ਺Λॻ͖࢝ΊΔલʹ %&4$3*15*0/ΛຒΊΔ Πϯϑϥͷ੔උ ϥΠηϯεͷબ୒ VTFUIJTDSFBUF@QBDLBHF ޙͷσΟϨΫτϦߏ੒ use_git() use_github() use_*_license()

  29. %&4$3*15*0/ΛຒΊΑ͏ %&4$3*15*0/ͷ৘ใ͕ $3"/ʹ൓ө͞Ε·͢ Package: spatech Title: What the Package Does

    (One Line, Title Case) Version: 0.0.0.9000 Authors@R: person(given = "First", family = "Last", role = c("aut", "cre"), email = "first.last@example.com", comment = c(ORCID = "YOUR-ORCID-ID")) Description: What the package does (one paragraph). License: What license it uses Encoding: UTF-8 LazyData: true
  30. %&4$3*15*0/ΛຒΊΑ͏ Package: spatech Title: Oita Spa Tech Sample Version: 0.0.0.9000

    Authors@R: person(given = "Shinya", family = "Uryu", role = c("aut", "cre"), email = "suika1127@gmail.com", comment = c(ORCID = "0000-0002-0493-6186")) Description: Guide to first R library development. License: MIT + file LICENSE Encoding: UTF-8 LazyData: true URL: https: //github.com/uribo/spatech BugReports: https: //github.com/uribo/spatech/issues use_github() use_mit_license() .Rprofile ʹઃఆΛهड़͓ͯ͘͠ͱָ author creator contributor copyright holder ։ൃʹܞΘΔਓͷϦετ ?person ৄࡉ͸
  31. όʔδϣϯʹ͍ͭͯ ͔Ͱ਺ࣈΛ۠੾Δʢ਺ࣈ͕ͭҎ্ʹͳΔΑ͏ʹʣ  OPW <major>.<minor>.<patch> ͷܗ͕ࣜ๬·͍͠ ։ൃதͷόʔδϣϯͰ͋Δ͜ͱ͕໌֬ 0.0.0.9000 $3"/ొ࿥࣌ʹ͸όʔδϣϯΛ͋͛Δ $3"/͔ΒΠϯετʔϧ͞ΕΔ൛͸

    ͱ͸ҟͳΔ͜ͱΛࣔ͢ 0.0.0.9000 0.1.0 0.1.0.9000 Release Development Development
  32. ϥΠηϯεͷݕ౼ .*5 (1-·ͨ͸(1- $$ 3ύοέʔδͰࢦఆ͞ΕΔ୅දతͳϥΠηϯεͷछྨ *NQPSUT 4VHHFUTͱͯ͠ґଘ͢ΔύοέʔδͷϥΠηϯεΛ Ͳ͏ѻ͏͔ʁ

  33. ςετ

  34. ͳΜͷͨΊͷςετʁ όάΛݮΒ͢ ߏ଄͕ྑ͘ͳΔ ։ൃ͕ଅਐ͞ΕΔ ݎ࿚ੑΛߴΊΔ use_testthat() use_test() devtools ::test() test_that()

    expect_*() skip_*() .R test-hoge.R 1 2 3 4 ྭ·͠ͷϝοηʔδ͖ͭ
  35. ςετʹ͸ԿΛॻ͘ʁ ίʔυ͕ͲͷΑ͏ʹৼΔ෣͏ͷ͔ɺ༧૝͞ΕΔ݁ՌΛهड़͢Δ hello_msg <- function(name = NULL) { if (is.null(name))

    { warning("Name argument is null") } else { message(paste("Hi,", name)) } } .R msg.R
  36. ςετʹ͸ԿΛॻ͘ʁ ίʔυ͕ͲͷΑ͏ʹৼΔ෣͏ͷ͔ɺ༧૝͞ΕΔ݁ՌΛهड़͢Δ expect_message( hello_msg(name = "uribo")) expect_warning( hello_msg()) expect_equal( hello_msg(name

    = "uribo"), "Hi, uribo") #> Hi, uribo #> Error: hello_msg(name = "uribo") not equal to "Hi, uribo". #> target is NULL, current is character .R test-msg.R ؔ਺ͷڍಈΛ஌ΔͨΊʹ΋ UFTUͷϑΝΠϧΛݟΔͷ͸ ྑ͍
  37. ڞ༗Šଞऀͷར༻Λ૝ఆ ෳ਺؀ڥͰͷ ݕূ͕ඞཁ ࣗ෼ͷ؀ڥͰ͸ςετ͕௨ͬͨɺ Ͱ͸μϝ ҟͳΔ04 3ͷόʔδϣϯ

  38. ڞ༗Šଞऀͷར༻Λ૝ఆ SIVC XJOCVJMEFS ܧଓత ΠϯςάϨʔγϣϯ αʔϏε ύοέʔδɺ΢ΣϒαʔϏεɺEPDLFS؀ڥΛ׆༻ͨ͠ςετΛ࣮ࢪ https://builder.r-hub.io/ \SIVC^͔Β΋ ϚϧνϓϥοτϑΥʔϜͰͷCVJME

    https://win-builder.r-project.org/ EFWUPPMTDIFDL@XJO@ https://travis-ci.org .BD04Ϣʔβ͕8JOEPXT؀ڥΛ ࢼ͢ͷʹॏๅ https://circleci.com
  39. υΩϡϝϯτ

  40. SPYZHFOίϝϯτ !UBHͷܗࣜ `Ͱ࢝·Γɺର৅ͷؔ਺ͷલʹهड़͢Δ @inheritParams @rdname @export {roxygen2md} {roxygen2} ॏཁͳ΋ͷ @param

    @description SPYZHFOλά·ͱΊc"UVTZTCMPH https://blog.atusy.net/2018/08/28/roxygen2matome/ SPYZHFOίϝϯτʹNBSLEPXOه๏ ΛՄೳʹ͢Δʢ3EϑΝΠϧੜ੒࣌ʹม׵ʣ ؔ਺ͷΤΫεϙʔτ Ҿ਺ͷܧঝ
  41. #' @title Read and Parse Fundamental Geospatial Data (FGD) file

    #' #' @description The JPGIS (GML) format file provided by FGD as input, #' the fundamental items in the file is read as an 'sf' object. #' Supporting FGD Version 4.1 (2016/10/31). #' @details Support following items: #' Administrative Area ('AdmArea'), Administrative Boundary ('AdmBdry'), #’ … লུ #' @seealso \url{https: //fgd.gsi.go.jp/download/ref_kihon.html} #' @param file Path to XML file #' @import sf #' @import xml2 #' @importFrom purrr pmap reduce list_modify #' @importFrom tibble new_tibble #' @return A [sf][sf ::st_sf] #' @export #' @examples #' # Administrative Area #' read_fgd(system.file("extdata/FG-GML-000000-AdmPt-dummy.xml", package = "fgdr")) ?fgdr ::read_fgd()
  42. ύοέʔδ։ൃΛ ࢧ͑Δ 3ύοέʔδ

  43. {styler} {lintr} ίʔσΟϯάελΠϧ ΦϒδΣΫτͷ໋໊نଇ΍୅ೖԋࢉࢠͷॻࣜɺ ۭനεϖʔεͷೖΕํɺվߦ࣌ͷنଇͳͲ ίʔυͷݟ௨͠Λྑ͘͠ɺอकੑΛߴΊΔͨΊʹେࣄ ಛʹଞऀ͕։ൃʹؔΘΔՄೳੑ͕͋Δ৔߹ʹ༗ޮ 34UEVJPͷΞυΠϯ ͔Β΋࣮ߦՄೳ ίʔυ੔ܗ

    ίʔυͷॻ͖ํΛࢦఠ
  44. {travis} {tic} ܧଓతΠϯςάϨʔγϣϯ {pkgdown} (JU)VCͱ࿈ܞ͠ɺϑΝΠϧʹमਖ਼ʢίϛοτʣ͕ߦΘΕͨ ࣌ʹϏϧυɺνΣοΫɻ Τϥʔͷૣظൃݟʹͭͳ͕Δ ෆඋ͕ͳ͚Ε͹΢ΣϒϖʔδΛߋ৽ {covr}

  45. ͳ͚Ε͹࡞Δɻ ͦΕ͕ ϗΫιΤϜͷ੤͍ Enjoy SPA!