gpt4 book ai didi

r - 使用 .onLoad() 将对象加载到 R 包中的全局环境中

转载 作者:行者123 更新时间:2023-12-04 04:27:37 27 4
gpt4 key购买 nike

我正在开发一个 R 包,我需要在其中随时间管理各种对象的状态。从概念上讲,当包加载 (.onLoad) 时,它会检查缓存中的状态对象,如果它不存在,则会创建一个新实例,保存到缓存中,并在全局环境中分配。使用 devtools::build() 构建站点后,我无法使用 .onLoad() 在全局环境中看到对象。所以,我有三个问题:

  • .onLoad() 函数是否适合此功能?如果是这样,当前使状态变量在全局环境中可见的最佳实践是什么?
  • 是否开发了用于跨“R session ”管理状态的解决方案(包)?
  • 有没有比我采用的方法更好的解决问题的概念方法?

  • 尝试过的解决方案......到目前为止

    我搜索了 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"
    }
    )
    )

    方法一
      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
    }

    方法三:
      .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)
    }

    session 信息
     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 code workflow

    The first practical advantage to using a package is that it’s easy tore-load your code. You can either run devtools::load_all(), or inRStudio press Ctrl/Cmd + Shift + L, which also saves all open files,saving you a keystroke. This keyboard shortcut leads to a fluiddevelopment workflow:

    1. Edit an R file.
    2. Press Ctrl/Cmd + Shift + L.
    3. Explore the code in the console.
    4. Rinse and repeat.

    Congratulations! You’ve learned your first package development workflow. Even if you learn nothing else from thisbook, you’ll have gained a useful workflow for editing and reloading Rcode


    加载_all()。哇!就这么简单。 Load all 运行 .onload() 函数并将对象渲染到全局环境中。谁知道?
    引用: R Code Workflow, R Packages, Hadley

    关于r - 使用 .onLoad() 将对象加载到 R 包中的全局环境中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45294223/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com