転ばぬ先のテスト・アサートの導入〜再現性の保証対策として〜 / rcode-test-assert

D12a80cab206033a820ccff8319f957b?s=47 Uryu Shinya
August 22, 2018

転ばぬ先のテスト・アサートの導入〜再現性の保証対策として〜 / rcode-test-assert

⚡️ Lightning Talk ⚡️
Reproducible Source Code is here: https://github.com/uribo/talk_180822_rcode-test-assert

D12a80cab206033a820ccff8319f957b?s=128

Uryu Shinya

August 22, 2018
Tweet

Transcript

  1. ςετɾΞαʔτͷಋೖ ʙ࠶ݱੑͷอূରࡦͱͯ͠ʙ ӝੜਅ໵ !VSJCP ͹͵ ઌͷ స

  2. ͜Μͳ͜ͱ͕සൟʹ͋Γ·͢Ͷ స  Ҏલͷग़ྗͱҟͳΔ஋͕ग़ͨ ̎ จࣈྻܕͩͱࢥͬͨΒ ɹɹɹɹҼࢠܕʹͳ͍ͬͯͨ ̏ ͳͥɾͲ͜ͰΤϥʔʹ ɹɹͳͬͨͷ͔͕Θ͔Βͳ͍

  3. ৭ʑͳ৚݅ɾ؀ڥͷҧ͍͕͋Δ  04 3ຊମɾύοέʔδͷόʔδϣϯ มԽʢҟৗʣʹؾ͕ͭ͘͜ͱ͕େࣄ  ࢥ͍ࠐΈͷ··ղੳΛଓ͚ͯ͠·ͬͨΒʁ  Ͳ͜ͰมԽ͕͔͋ͬͨ୳͢ͷ͸ె࿑ 

    Կ͕มΘͬͯ͠·ͬͨͷ͔Λ೺Ѳ͍ͨ͠ స σʔλ ͸ มΘΔ ςετɺΞαʔτΛಋೖ͢Δ͜ͱͰɺ σʔλͷมԽʹؾ෇͖΍͘͢ͳΔ
  4. ୯ମςετ VOJUUFTU ͱݺ͹ΕΔ΋ͷ  ຊདྷ͸ɺϓϩάϥϜ͕ظ଴͞Εͨ௨Γʹɹɹ ಈ࡞͢Δ͔Λ֬ೝ͢ΔͨΊͷ࡞ۀ  هड़͞ΕͨςετύλʔϯΛࢼ͢ɹɹɹɹɹ ʢهड़ͷͳ͍ςετ݁Ռ͸อূ͠ͳ͍ʣ Ξαʔτ

    BTTFSUʜஅݴ͢Δ   ஋ͷঢ়ଶΛ༧ଌ͢Δ஋Λهड़  ༧૝֎ͷ஋͕༩͑ΒΕͨࡍʹΤϥʔΛग़ྗ స ςετ Ξαʔτ ͱ
  5. MJCSBSZ UFTUUIBU

  6. ✔ expect_equal( dim(iris), c(120, 4)) Error: dim(iris) not equal to

    c(120, 4). 2/2 mismatches (average diff: 15.5) [1] 150 - 120 == 30 [2] 5 - 4 == 1 స FYQFDU@  ΦϒδΣΫτ ͱ ฦΓ஋ͷ ঢ়ଶΛهड़ expect_equal( object = dim(iris), expected = c(150, 5))
  7. ؔ਺ ର৅ ྫ expect_equal() ฏ౳ੑ expect_equal(letters[1:3], c(“a", "b", “c”)) expect_setequal(letters[1:3],c("a",

    "c", "b")) expect_gt() େখؔ܎ expect_gt(pi, 3.1) expect_gte(pi, 3.14) expect_lt(pi, 3.2) expect_true() ਅِ஋ expect_true(iris$Species[1] == "setosa") expect_length() ௕͞ expect_length(unique(iris$Species), 3)
  8. ࣮ફ

  9. EBUB JSJT ͷঢ়ଶΛه࿥ͨ͠ ɹɹɹɹɹɹɹɹɹͭͷςετ߲໨Λ༻ҙ tests/testthat/test-01iris.R  σʔλͷαΠζ ߦɾྻͷ਺  

    ྻͷ໊લ  4QFDJFTྻʹؚ·ΕΔਫ४
  10. tests/testthat/test-01iris.R ςετର৅ͷΦϒδΣΫτΛ ɹɹɹɹɹNZ@JSJTDTWʹม͑ͯ࠶౓࣮ߦ  NZ@JSJT͸JSJTͱಉ͡σʔλͰ͋Δ૝ఆ  JSJTʹର͍͔ͯͭ͘͠ͷՃ޻Λߦͬͨ΋ͷ Error: Test failed:

    'Iris data statement' * dim(my_iris) not equal to c(150, 5). 1/2 mismatches [1] 120 - 150 == -30 * unique(my_iris$Species) has length 4, not length 3. * levels(my_iris$Species) not set-equal to c("setosa", "versicolor", "virginica"). Lengths differ: 4 is not 3 స ʮਖ਼͘͠ʯࣦഊ͢Δ
  11. MJCSBSZ BTTFSUS

  12. library(dplyr) iris %>% tibble::as_tibble() %>% select(Sepal.Length, Species) %>% verify( has_all_names(

    c("Sepal.Length", "Species"))) %>% filter(Sepal.Length >= 5.0) %>% assert( within_bounds(5.0, Inf), Sepal.Length) ύΠϓॲཧ   ϑϨϯυϦʔ ͳ Ξαʔτ ؔ਺Λఏڙ verify has_all_names assert within_bounds ྻʹؚ·ΕΔ໊લͰ͋Δ ൣғʹؚ·ΕΔ஋Ͱ͋Δ
  13. ࣮ફ

  14. my_iris <- iris my_iris %>% select(Sepal.Length, Species) %>% filter(Sepal.Length >=

    5.0) %>% group_by(Species) %>% summarise( sl_mean = mean(Sepal.Length)) # A tibble: 3 x 2 Species sl_mean <fct> <dbl> 1 setosa 5.23 2 versicolor 5.96 3 virginica 6.62 src/02-assertr_demo.R Ξαʔτ͕ͳ͍ͱʜ
  15. my_iris <- read.csv( here::here("data", "my_iris.csv")) my_iris %>% select(Sepal.Length, Species) %>%

    filter(Sepal.Length >= 5.0) %>% group_by(Species) %>% summarise( sl_mean = mean(Sepal.Length)) # A tibble: 4 x 2 Species sl_mean <fct> <dbl> 1 setosa 5.28 2 versicolor 5.98 3 virginica 6.65 4 Virsicolor 6.2 స ͜Ε͸๬Μͩ݁Ռ Ξαʔτ͕ͳ͍ͱʜ src/02-assertr_demo.R
  16. MJCSBSZ WEJGGS ͓ · ͚

  17. ࡞ਤͨ͠ϑΝΠϧ΋ςετ͍ͨ͠  ϑΥϯτɺ৭ɺϨΠΞ΢τFUD  มߋΛՃ͑ͭͭɺաڈͷਤͱ΋ൺֱ͍ͨ͠ స ͦͷ ਤ͸ ຊ౰ʹ ಉ͡ʁ

    ϙΠϯτͷେ͖͞ͱʜ  ୳͞ͳ͍Ͱ͍ͩ͘͞
  18. ˞ύοέʔδ ؀ڥͰͷ ར༻Λ૝ఆ ࡞ਤΛߦ͏ॲཧ·ͨ͸ؔ਺Λ༻ҙ UFTUTUFTUUIBUͷதͷϑΝΠϧͰ ɹΛ࣮ߦ ·ͨ͸vdiffr::collect_cases(), vdiffr:::diffrAddin() Ͱ΋Մ vdiffr::validate_cases()Λ࣮ߦ

    ˠUFTUTpHTʹ47(Ͱग़ྗ͞ΕΔ  ̎ ̏ 
  19. ࣮ફ

  20. src/03-plot.R ࡞ਤॲཧͷίʔυΛमਖ਼ લޙͷঢ়ଶ Λൺֱ ܩઢ

  21.  ςετΛॻ͘͜ͱͰੜ࢈ੑ͸ɹɹɹ Ұ࣍తʹԼ͕Δ  શͯΛ໢ཏ͢Δͷ͸ࠔ೉  σʔλ਺΍ܽଛɺྻͷσʔλܕͳͲɹ ͕େࣄʁ  ਖ਼͍͠ςετ͚ͩͰͳࣦ͘ഊ͢Δɹ

    έʔεͷهड़΋ॏཁ స ԿΛ Ͳ͜·Ͱ ςετ ͢Δ͔ ɾ d࠷ޙʹd
  22.  ίϝϯτʹΑΔ஋ͷهड़͸͍ͯ͠Δ  ໨HSFQʹΑΔ࡞ۀ͸ආ͚͍ͨͷͰ(PPE ࢀՃऀͷ੠͔Β  ςετʹཔΓ͗ͣ͢ɺ໨ࢹʹΑΔ֬ೝ΋େࣄɻ ͓ΘΓ