Upgrade to Pro — share decks privately, control downloads, hide ads and more …

renvによるパッケージ管理/renv_version_control

くろたんく
September 29, 2019

 renvによるパッケージ管理/renv_version_control

分析再現性担保のためのrenvをつかったパッケージ管理のまとめ資料

くろたんく

September 29, 2019
Tweet

More Decks by くろたんく

Other Decks in Research

Transcript

  1. ໨࣍ Agenda  ࣗݾ঺հ  ύοέʔδΠϯετʔϧʹΑΔ؀ڥͷഁյ  ҆શʹύοέʔδΠϯετʔϧ͢Δʹ͸  SFOWͱ͸

     ֓ཁ  ࢓૊Έ  SFOWͷ࢖͍ํ  SFOWͷΠϯετʔϧ  SFOWͷ֤छίϚϯυ
  2. ύοέʔδΠϯετʔϧʹΑΔ3؀ڥͷഁյ Upgrading packages can be a risky affair ύοέʔδͷόʔδϣϯΛVQHSBEFͨ͠Γɺ ผ؀ڥʹద౰ʹΠϯετʔϧͨ͠Γͯ͠ɺ

    ࢥ͏Α͏ʹίʔυ͕ಈ͔ͳ͍ͱ͍͏ܦݧ͸͋Γ·ͤΜ͔ʁ ݪҼͱͯ͠ߟ͑ΒΕΔͷ͸ w JOTUBMMQBDLBHFT ࣮ߦλΠϛϯάʹΑΔόʔδϣϯͷมԽͷՄೳੑ w ޓ׵ੑͷͳ͍มߋʹΑΔڍಈ͕มΘͬͨΓಈ͔ͳ͘ͳͬͨΓ͢ΔՄೳੑ
  3. ύοέʔδΠϯετʔϧʹΑΔ3؀ڥͷഁյ Upgrading packages can be a risky affair ύοέʔδͷόʔδϣϯΛVQHSBEFͨ͠Γɺ ผ؀ڥʹద౰ʹΠϯετʔϧͨ͠Γͯ͠ɺ

    ࢥ͏Α͏ʹίʔυ͕ಈ͔ͳ͍ͱ͍͏ܦݧ͸͋Γ·ͤΜ͔ʁ ݪҼͱͯ͠ߟ͑ΒΕΔͷ͸ w JOTUBMMQBDLBHFT ࣮ߦλΠϛϯάʹΑΔόʔδϣϯͷมԽ w ޓ׵ੑͷͳ͍มߋʹΑΔڍಈ͕มΘͬͨΓಈ͔ͳ͘ͳͬͨΓ͢ΔՄೳੑ ύοέʔδͷΞοϓάϨʔυ΍ΠϯετʔϧͰ ΋͏೰Έͨ͘ͳ͍ Ͳ͏͢Ε͹ɺ҆શʹύοέʔδΠϯετʔϧͰ͖Δ͔
  4. ҆શʹύοέʔδΠϯετʔϧ͢Δʹ͸ Safely Operating Packages ࠶ݱੑ͋Δύοέʔδ؅ཧͷϑϩʔ  ϓϩδΣΫτ͝ͱʹɺ࢖༻͢ΔϥΠϒϥϦΛܾ ΊͯϓϩδΣΫτΛ։࢝͢Δ  ࢖༻͢ΔϥΠϒϥϦͷύοέʔδΛΠϯετʔ

    ϧ͢Δ  ίʔυͱҰॹʹϥΠϒϥϦͷঢ়ଶΛه࿥͢Δ  ؀ڥΛ࠶࡞੒͢Δඞཁ͕͋Δͱ͖ʹϥΠϒϥϦ Λ෮ݩ͢Δ ͜ΕΛύοέʔδԽͨ͠ͷ͕ɺ SFOW 1+ͷ࡞੒ Πϯετʔϧ ίʔυΛॻ͘ ه࿥ ෮ݩ
  5. SFOWͷ࢖͍ํ Workflow # ॳظԽ (·ͬ͞ΒͳRscriptΛઌʹ։͍͓ͯ͘) > renv::init() # ύοέʔδͷΠϯετʔϧ >

    renv::install("pkg") # Rscriptʹlibrary("pkg")ͱॻ͍ͯอଘ͢Δ # Πϯετʔϧঢ়گͷ֬ೝ > renv::status() # Πϯετʔϧঢ়گΛrenv.lockϑΝΠϧʹॻ͖ग़͢ > renv::snapshot()  ॳظԽ  ඞཁͳύοέʔδͷΠϯετʔϧ  MJCSBSZ QLH Λॻ͍ͯอଘ   Πϯετʔϧঢ়گͷ֬ೝ  Πϯετʔϧঢ়گΛॻ͖ग़͠ dΛ܁Γฦ͠ͳ͕Β࡞ۀ͢Δ อଘΛ๨ΕΔͱTUBUVT ͕൓Ԡ͠ͳ͍ͷͰ஫ҙ͢Δ͜ͱ ஫ɿSFOW͸ߋ৽͕͔ͳΓߦΘΕ͍ͯΔͷͰɺɹɹ ࢖͍ํ͕େ͖͘มΘΔՄೳੑ͕͋Δ
  6. SFOWͷΠϯετʔϧ How to install renv  3Λ্ཱͪ͛Δ  SFNPUFTΛJOTUBMM 

    SFOWΛΠϯετʔϧ  ͦͷ͏ͪ$3"/͔ΒΠϯετʔϧͰ͖ΔͬΆ͍ ݱࡏ > if (!requireNamespace("remotes")) install.packages("remotes") > remotes::install_github("rstudio/renv")
  7. SFOWJOJU  Initialize a Project > setwd("~/local/Project/sandbox_r/") > renv::init() #

    private R libraryͷσΟϨΫτϦ͕࡞ΒΕΔ > .libPaths() ύοέʔδ؅ཧ։࢝ίϚϯυ  ద౰ͳσΟϨΫτϦͰ࡞ۀ͢Δ   ॳظԽ͢Δͱ෼͚ΒΕͨ؀ڥ͕Ͱ͖Δ   ֘౰σΟϨΫτϦʹMJCSBSZஔ͖৔͕Ͱ͖Δ  )0.&ͩͱJOJUBMJ[FͰ͖ͳ͍ͬΆ͍ͷͰ࡞ۀ༻EJSͰߦ͏ ɹ34UVEJPͳΒOFXQSPKFDUͳͲͰద౰ʹ࡞੒͢ΔͳͲ͢Δ ɹ·ͬ͞Βͳ34DSJQUΛ࡞੒ͯ͠อଘ͓ͯ͘͠ SFOWMPDLͱ͍͏ύοέʔδͷόʔδϣϯͳͲΛ؅ཧ͢ΔϑΝΠϧ͕Ͱ͖Δɻ͜ͷϑΝ ΠϧΛ(JUͰ؅ཧ͢Δ͜ͱΛਪ঑͢Δ جຊతʹHMPCBMDBDIFͷEJSʹΠϯετʔϧ͞Εͯɺ͜͜ʹ͸γϯϘϦοΫϦϯΫ͕ షΒΕΔɻॳΊͯͷ࣌͸HMPCBMDBDIF͕ͳ͍ͷͰΠϯετʔϧ͢ΔͨΊͷ͕͔͔࣌ؒΔ * '~/Library/Application Support/renv' has been created. * Discovering package dependencies ... Done! * Copying packages into the cache ... Done! * Resolving missing dependencies ... * Querying repositories for available binary packages ... Done! * Querying repositories for available source packages ... Done! The following package(s) will be updated in the lockfile: # GitHub ============================= - renv [* -> rstudio/renv] * Lockfile written to ‘~/local/Project/sandbox_r/renv.lock’. [1] “/Users/blacktanktop/local/Project/sandbox_r/renv/library/ R-3.6/x86_64-apple-darwin15.6.0” [2] "/private/var/folders/h1/m7p6hhnx11g_r55plhrm9z3c0000gn/T/ RtmpB41ftz/renv-system-library"
  8. SFOWJOTUBMM  Install packages > setwd("~/local/Project/sandbox_r/") > renv::install("dplyr") # ࠷৽൛ͷΠϯετʔϧ

    > renv::install("dplyr") # ಛఆͷόʔδϣϯͷΠϯετʔϧ > renv::install("[email protected]") # GitHub(࠷৽dev.version)͔ΒͷΠϯετʔϧ > renv::install("tidyverse/dplyr") # ιʔείʔυ͔ΒͷΠϯετʔϧ > renv::install("~/path/to/package") ΠϯετʔϧίϚϯυ  HMPCBMDBDIFʹΠϯετʔϧ͞Ε͍ͯΔ͔Λ ֬ೝͯ͠طʹ͋Ε͹ɺDBDIFʹϦϯΫΛషΔ ͚ͩͷॲཧ͕ߦΘΕΔͨΊɺΠϯετʔϧͷ࣌ ͕ؒ୹ॖ͞ΕΔ   ׅހ಺ͷॻ͖ํʹΑͬͯɺΠϯετʔϧݩΛม ߋͰ͖Δ DBDIFΛ࢖Θͣʹɺ࣮ମࣗମΛQSJWBUFMJCSBSZʹஔ͘͜ͱ΋Ͱ͖Δ PQUJPOT SFOWTIJNT'"-4& ΍SFOWTFUUJOHTVTFDBDIF '"-4& Λ͔ͭ͏ ৄࡉ͸IUUQTSTUVEJPHJUIVCJPSFOWBSUJDMFTSFOWIUNM * Querying repositories for available binary packages ... Done! * Querying repositories for available source packages ... Done!
  9. SFOWTUBUVT  Report differences between renv.lock file and the current

    state Πϯετʔϧঢ়گ֬ೝίϚϯυ  Πϯετʔϧޙɺ3TDSJQUʹMJCSBSZ QLH ͱ ॻ͍ͯอଘ͢Δ   Πϯετʔϧ͞Ε͍ͯΔ͕ɺSFOWMPDLʹه࿥ ͞Ε͍ͯͳ͍  มߋ͕ಉظ͞Ε͍ͯͳ͍ ͷ̎ͭʹ෼͔Εͯදࣔ͞ΕΔ Πϯετʔϧͱ3TDSJQU্ͷMJCSBSZ QLH ͷঢ়ଶͱSFOWMPDLͷঢ়ଶΛൺֱ͢ΔͨΊ ӈྫ͸ɺHHQMPUͷΠϯετʔϧͱɺEBUBUBCMF ͷόʔδϣϯมߋͷঢ়گ ʢʜͷ෦෼͸࣮ࡍ͸શ෦දࣔ͞ΕΔʣ > renv::status() The following package(s) are installed but not recorded in the lockfile: _ Rcpp [1.0.2] ggplot2 [3.2.1] ………লུ……… reshape2 [1.4.3] magrittr [1.5] ………লུ……… Use `renv::snapshot()` to add these packages to your lockfile. The following package(s) are out of sync: Package Lockfile Version Library Version data.table 1.12.2 1.11.8 Use `renv::snapshot()` to save the state of your library to the lockfile. Use `renv::restore()` to restore your library from the state of the lockfile.
  10. SFOWTOBQTIPU  Create a renv.lock file capturing R package dependencies

    > renv::snapshot() The following package(s) will be updated in the lockfile: # CRAN =============================== - data.table [1.12.2 -> 1.11.8] - Rcpp [* -> 1.0.2] - pillar [* -> 1.4.2] ………ศ্ٓলུ……… - ggplot2 [* -> 3.2.1] - reshape2 [* -> 1.4.3] ………ศ্ٓলུ……… Do you want to proceed? [y/N]: y * Lockfile written to ‘~/local/Project/sandbox_r/renv.lock’. Πϯετʔϧঢ়گه࿥ίϚϯυ Πϯετʔϧঢ়گͷมߋΛSFOWMPDLʹه࿥͢Δ ӈྫ͸ɺEBUBUBCMFόʔδϣϯมߋͱHHQMPUͷ Πϯετʔϧͯ͠ɺه࿥ͨ͠ঢ়گ
  11. SFOWSFTUPSF  Restore a project's dependencies from a renv.lock file

    # new projectΛ࡞੒ # ر๬ͷrenv.lockΛஔ͘ > renv::init() * The library is already synchronized with the lockfile. Restarting R session... * Project ‘~/local/Project/sandbox_r' loaded. [renv 0.7.0-108] > renv::restore() The following package(s) will be updated: # CRAN =============================== - data.table [* -> 1.12.2] Do you want to proceed? [y/N]: y Installing data.table [1.12.2] ... OK (linked cache) ෮چίϚϯυ SFOWMPDLͷঢ়ଶΛ࠶ݱͰ͖Δ  ӈྫ͸ɺ·ͬ͞Βͳঢ়ଶͰɺ࠶ݱ͍ͨ͠؀ڥͷ SFOWMPDLΛஔ͖ɺJOJU ͰॳظԽɺSFTUPSF Ͱ ෮چ͍ͯ͠Δঢ়گ SFTUPSFͷޙ͸TFTTJPOΛ3FTUBSU͢Δ͜ͱ
  12. SFOWSFTUPSF  Restore a project's dependencies from a renv.lock file

    > renv::install("data.table") Installing data.table [1.12.2] ... OK (linked cache) > renv::status() The following package(s) are out of sync: Package Lockfile Version Library Version data.table 1.11.0 1.12.2 # ৽ͨͳversionͯ͠ෆ۩߹Λ֬ೝ # ໰୊͕͋Δ৔߹͸snapshotΛऔΒͣʹ > renv::restore() The following package(s) will be updated: # CRAN =============================== - data.table [1.12.2 -> 1.11.0] Do you want to proceed? [y/N]: y Installing data.table [1.11.0] ... OK (linked cache) ෮چίϚϯυ SFOWMPDLͷঢ়ଶΛ࠶ݱͰ͖ΔͷͰɺΠϯετʔ ϧ͞ΕͨύοέʔδΛ࡟আ΍όʔδϣϯมߋͯ͠ɺ ؾʹ৯Θͳ͔ͬͨΒ෮چ͢Δ͜ͱ͕Ͱ͖Δ  ӈྫ͸ɺEBUBUBCMF͕Πϯετʔϧ͞Ε ͍ͯΔঢ়ଶͰ࠷৽ʹVQHSBEF͠ɺSFTUPSF Ͱ෮ چ͍ͯ͠Δঢ়گ SFTUPSFͷޙ͸TFTTJPOΛ3FTUBSU͢Δ͜ͱ Ұ୴ฏؾͱࢥͬͯɺTOBQTIPUΛऔͬͯ͠·͏ͱ ໭ͤͳ͍ͷͰɺͦͷରࡦͱͯ͠ɺSFOWMPDLΛ(JU ؅ཧ͢Δඞཁ͕͋Δ
  13. SFOWIJTUPSZ  Find prior versions of the renv.lock file >

    renv::history() commit author_date committer_date subject 1 26b79f55452169f97c38f959bd94756a73e3b5ab 2019-09-29 09:49:32 2019-09-29 09:49:32 [email protected] 2 1a394b4f4de56084d23b62fbb05e2c31ae58e3f1 2019-09-29 09:47:15 2019-09-29 09:47:15 [email protected] 3 40446467d7b9d714ae4822c4b81c0b6ce34fc5e9 2019-09-29 09:45:40 2019-09-29 09:45:40 first commit ҎԼͱಉ͡Ͱ͋Δ͜ͱ͕Θ͔Δɻ % git log commit 26b79f55452169f97c38f959bd94756a73e3b5ab (HEAD -> master) Author: blacktanktop Date: Sun Sep 29 09:49:32 2019 +0900 [email protected] commit 1a394b4f4de56084d23b62fbb05e2c31ae58e3f1 Author: blacktanktop Date: Sun Sep 29 09:47:15 2019 +0900 [email protected] commit 40446467d7b9d714ae4822c4b81c0b6ce34fc5e9 Author: blacktanktop Date: Sun Sep 29 09:45:40 2019 +0900 ίϛοτཤྺͷࢀরίϚϯυ SFOWMPDLΛ(JU؅ཧ͍ͯ͠Δલఏ  HJUMPHΛฦ͍ͯ͠Δͷ͕Θ͔Δ ӈྫ͸ɺSFOWJOJU ௚ޙɺ౓ͷҟͳΔόʔδϣ ϯΛJOTUBMMޙʹDPNNJUͯ͋͠Δঢ়گͰɺ֬ೝ͠ ͨ΋ͷ SFOWJOJU ͷޙͰ֘౰EJSͰHJUJOJUͯ͠SFOWMPDLΛDPNNJUɻTOBQTIPUऔΔͨͼʹ DPNNJU͍ͯ͠Δঢ়ଶɻ
  14. SFOWSFWFSU  Revert renv.lock file > renv::revert(commit = "1a394b4f4de56084d23b62fbb05e2c31ae58e3f1") diff

    --git a/renv.lock b/renv.lock index f6c69b2..da44e6d 100644 --- a/renv.lock +++ b/renv.lock @@ -11,10 +11,10 @@ "Packages": { "data.table": { "Package": "data.table", - "Version": "1.12.2", + "Version": "1.11.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "f2f76b5c5394350c4d4ee77dab51d35c" + "Hash": "7af1b7412c98cb4fde6d4f940218924e" }, "renv": { "Package": "renv", * renv.lock from commit 1a394b4f4de56084d23b62fbb05e2c31ae58e3f1 has been checked out. > > > renv::restore() The following package(s) will be updated: # CRAN =============================== - data.table [1.12.2 -> 1.11.0] Do you want to proceed? [y/N]: y Installing data.table [1.11.0] ... OK (linked cache) SFOWMPDLpMF෮چίϚϯυ IJTUPSZͷDPNNJUཤྺΛ࢖ͬͯɺSFOWMPDLΛ ෮چ͢ΔɻͦͷޙɺSFTUPSFͰ؀ڥ΋෮چՄ  TOBQTIPUͷͨͼʹDPNNJU͑ͯ͋͞͠Ε͹ɺ޷ ͖ͳSFOWMPDLpMFͷঢ়ଶʹ෮چͰ͖Δ ӈྫ͸ɺલͷεϥΠυʹ͓͍ͯɺ EBUBUBCMF!ͷঢ়ଶʹ໭͍ͯ͠Δঢ়گɻ SFTUPSFͷޙ͸TFTTJPOΛ3FTUBSU͢Δ͜ͱ > renv::revert(commit = "1a394b4f4de56084d23b62fbb05e2c31ae58e3f1") diff --git a/renv.lock b/renv.lock index f6c69b2..da44e6d 100644 --- a/renv.lock +++ b/renv.lock @@ -11,10 +11,10 @@ "Packages": { "data.table": { "Package": "data.table", - "Version": "1.12.2", + "Version": "1.11.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "f2f76b5c5394350c4d4ee77dab51d35c" + "Hash": "7af1b7412c98cb4fde6d4f940218924e" }, "renv": { "Package": "renv", * renv.lock from commit 1a394b4f4de56084d23b62fbb05e2c31ae58e3f1 has been checked out. # renv.lock͕໭ͬͨͷͰrestore͢Ε͹؀ڥ΋໭ͤΔ > renv::restore()
  15. ·ͱΊ summary SFOWΛ࢖͏͜ͱͰɺ  TZTUFNMJCSBSZͱ੾Γ෼͚Δ͜ͱ͕Ͱ͖ɺશମ΁ͷӨڹΛແͤ͘Δ  HMPCBMQBDLBHFDBDIFʹΑͬͯɺ౎౓ͷΠϯετʔϧ࣌ؒΛݮΒͤΔ  ͙͢ݩʹ໭ͤΔͨΊɺ҆৺ͯ͠ύοέʔδͷΠϯετʔϧ͕Ͱ͖Δ 

    SFOWMPDLʹΑͬͯ࠶ݱੑ͋Δ؀ڥΛൺֱతָʹ࡞Δ͜ͱ͕Ͱ͖Δ SFOW͸࢖͍উख͕ඇৗʹྑ͍ͷͰɺ ෼ੳͷ࠶ݱੑΛ୲อ͢ΔͨΊʹ΋ੋඇ࢖ͬͯΈͯ΄͍͠ɻ
  16. $MBOHͱ(/6'PSUSBOͷΠϯετʔϧ Install Clang compiler and GNU Fortran ίϯύΠϥʔؔ࿈ͷΠϯετʔϧ  SFOW͸աڈͷόʔδϣϯͷύοέʔδΛΠϯ

    ετʔϧ͢Δ࣌͸ɺιʔεͰΞʔΧΠϒ͞Εͯ ͍Δ΋ͷΛΠϯετʔϧ͜ͱʹͳΔɻ  .BDͰDMBOHͱGPSUSBO͕ͳͯ͘ίϯύΠϧΤ ϥʔ͕ग़ͨ࣌ͷରԠ  ެࣜʹ΋ॻ͔Ε͍ͯΔ  ͱΓ͋͑ͣΠϯετʔϧ͢Δ  ΂ͭʹCSFXͰΠϯετʔϧͯ͠΋͍͍  IUUQTDSBOSQSPKFDUPSHCJONBDPTY UPPMT  d3.BLFWBST 1. దٓίϝϯτΛೖΕସ͑ͭͭ 2. # gcc 3. # CC=/usr/local/bin/gcc-8 4. # CXX=/usr/local/bin/g++-8 6. # llvm 7. CC=/usr/local/clang7/bin/clang 8. CXX=/usr/local/clang7/bin/clang++