簡體   English   中英

使用.onLoad()在R Package中將對象加載到全局環境中

[英]Loading object into global environment in R Package using .onLoad()

我正在研究一個R包,其中我需要隨着時間的推移管理各種對象的狀態。 從概念上講,當程序包加載(.onLoad)時,它將檢查緩存中的狀態對象,如果狀態對象不存在,則會創建一個新實例,將其保存到緩存中並在全局環境中分配。 使用devtools :: build()構建網站后,無法在.onLoad()全局環境中查看對象。 因此,我有三個問題:

  1. .onLoad()函數適合此功能嗎? 如果是這樣,使狀態變量在全局環境中可見的當前最佳實踐是什么?
  2. 是否已經開發了用於在“ R個會話”中管理狀態的解決方案(軟件包)?
  3. 是否有比我采用的方法更好的概念方法?

解決方案已嘗試...到目前為止

我搜尋了SE,閱讀(並重新閱讀了)Hadley關於R Packages的書,以及Advanced R,沉思了Winston Chang在R6上的短篇小說(文章底部的鏈接),並將我的實驗精簡為三種失敗的方法。 首先,這是一個簡單的“ GameClass”,它使用三個變量實例化游戲,即玩家1,玩家2和游戲狀態。

 #' GameClass
 #' \code{GameClass} Class that...#'
 #' @export
 GameClass <- R6::R6Class(
   "GameClass",
   public = list(
     player1 = character(0),
     player2 = character(0),
     state  = character(0),
     initialize = function(player1, player2) {
       self$player1 <- player1
       self$player2 <- player2
       self$state <- "1st Match"
     }
   )
 )

方法1

  Assign the variable to the global environment using the <<- operator


  .onLoad <- function(libname, pkgname) {

    gameFile <- "./gameFile.Rdata"
    if (file.exists(gameFile)) {
      game <<- load(gameFile)
    } else {
     game  <<- GameClass$new("Eric", "Cassie")
     save(game, file = gameFile)
    } 
  }

方法二:

創建一個新環境並返回

  .onLoad <- function(libname, pkgname) {
    gameFile <- "./gameFile.Rdata"
    e <- new.env()

    if (file.exists(gameFile)) {
      e$game <- load(gameFile)
    } else {
      e$game <- GameClass$new("Eric", "Cassie")
      save(e$game, file = gameFile)
    }  
    e
  }

方法3:

  .onLoad <- function(libname, pkgname) {
    gameFile <- "./gameFile.Rdata"

    if (file.exists(gameFile)) {
      game <- load(gameFile)
    } else {
      game <- GameClass$new("Eric", "Cassie")
      save(game, file = gameFile)
    }
    assign("game", game, envir = .GlobalEnv)
  }

會議信息

 R version 3.4.1 (2017-06-30)
 Platform: x86_64-w64-mingw32/x64 (64-bit)
 Running under: Windows >= 8 x64 (build 9200)

 Matrix products: default

 locale:
 [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United 
 States.1252    LC_MONETARY=English_United States.1252
 [4] LC_NUMERIC=C                           LC_TIME=English_United 
 States.1252    

 attached base packages:
 [1] stats     graphics  grDevices utils     datasets  methods   base     

 other attached packages:
 [1] R6Lab_0.1.0

 loaded via a namespace (and not attached):
 [1] compiler_3.4.1 R6_2.2.2       tools_3.4.1    yaml_2.1.14   

我是OOP的新手,R6的新手,這是我的第一個R軟件包,並且我已經使用R大約一年了。顯然,我可以從這里的一些見解中受益。

提前致謝。

## References ##
[Hadley's Advanced R][1]
[Hadley's R Packages][2]
[Introduction to R6 Classes][3]
[How to define hidden global variables inside R Packages][4]
[Global variables in packages in r][5]
[Global variables in r][6]
[Global variable in a package which approach is more recommended][7]

  [1]: http://adv-r.had.co.nz/
  [2]: http://r-pkgs.had.co.nz/
  [3]: https://cran.r-project.org/web/packages/R6/vignettes/Introduction.html
  [4]: https://stackoverflow.com/questions/34254716/how-to-define-hidden-global-variables-inside-r-packages
  [5]: https://stackoverflow.com/questions/12598242/global-variables-in-packages-in-r
  [6]: https://stackoverflow.com/questions/1236620/global-variables-in-r
  [7]: https://stackoverflow.com/questions/28246952/global-variable-in-a-package-which-approach-is-more-recommended

在明顯的解決方案中尋找復雜的答案應該是一個詞。 沒有比這更明顯了。

R代碼工作流程

使用軟件包的第一個實際好處是重新加載代碼很容易。 您可以運行devtools :: load_all() ,或在RStudio中按Ctrl / Cmd + Shift + L,這也會保存所有打開的文件,從而節省了擊鍵次數。 此鍵盤快捷鍵導致工作流程流暢:

  1. 編輯一個R文件。
  2. 按Ctrl / Cmd + Shift +L。
  3. 在控制台中瀏覽代碼。
  4. 沖洗並重復。

恭喜你! 您已經學習了第一個軟件包開發工作流程。 即使您從本書中學不到的東西,也將獲得一個有用的工作流,用於編輯和重新加載R代碼

Load_all()。 哇! 就這么簡單。 全部加載都運行.onload()函數,並將對象呈現到全局環境中。 誰知道?

參考: R代碼工作流程,R軟件包,Hadley

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM