【问题标题】:Access formal arguments of an R6 method访问 R6 方法的形式参数
【发布时间】:2021-11-22 17:27:31
【问题描述】:

有没有办法访问 R6 方法的参数? 例如,我可以通过以下方式访问rstan::stan() 的参数:

library(rstan)
formalArgs(rstan::stan)
#>  [1] "file"            "model_name"      "model_code"      "fit"            
#>  [5] "data"            "pars"            "chains"          "iter"           
#>  [9] "warmup"          "thin"            "init"            "seed"           
#> [13] "algorithm"       "control"         "sample_file"     "diagnostic_file"
#> [17] "save_dso"        "verbose"         "include"         "cores"          
#> [21] "open_progress"   "..."             "boost_lib"       "eigen_lib"

但是,cmdstanr 使用 R6 类。 所以我可以通过查看$sample() 方法的所有参数 随附的帮助页面(``?cmdstanr::model-method-sample\``)。 但我似乎找不到以相同方式访问参数的方法。 例如,这会失败:

# remotes::install_github("stan-dev/cmdstanr")
library(cmdstanr)
formalArgs(cmdstanr::`model-method-sample`)
#> Error: 'model-method-sample' is not an exported object from 'namespace:cmdstanr'

有没有办法访问 R6 方法的形式参数?

reprex package (v2.0.1) 于 2021 年 11 月 22 日创建

【问题讨论】:

    标签: r r6


    【解决方案1】:

    R6 的一个基本特征是方法属于对象。它们未绑定在包命名空间中,本身,因此您无法使用::::: 访问它们也就不足为奇了。下面是一个直接取自?R6::R6Class的例子:

    ## An "R6ClassGenerator" object defining an R6 class
    Queue <- R6::R6Class("Queue",
      public = list(
        initialize = function(...) {
          for (item in list(...)) {
            self$add(item)
          }
        },
        add = function(x) {
          private$queue <- c(private$queue, list(x))
          invisible(self)
        },
        remove = function() {
          if (private$length() == 0) return(NULL)
          # Can use private$queue for explicit access
          head <- private$queue[[1]]
          private$queue <- private$queue[-1]
          head
        }
      ),
      private = list(
        queue = list(),
        length = function() base::length(private$queue)
      )
    )
    
    ## A "Queue" object defining an R6 class instance
    q <- Queue$new(5, 6, "foo")
    

    您可以从"R6ClassGenerator" 对象或"Queue" 对象访问公共方法,如下所示:

    a1 <- Queue$public_methods$add
    a2 <- q$add
    identical(a1, a2, ignore.environment = TRUE) # TRUE
    

    a1a2 传递给formalArgs 获取形式参数的名称。

    a1
    # function(x) {
    #                      private$queue <- c(private$queue, list(x))
    #                      invisible(self)
    #                    }
    
    formalArgs(a1)
    # [1] "x"
    

    cmdstanr 内部的 R6 类是 "CmdStanModel",而$sample() 是该类的公共方法,因此您可以访问 $sample() 的形式参数的名称

    formalArgs(cmdstanr:::CmdStanModel$public_methods$sample)
    #  [1] "data"                   "seed"                  
    #  [3] "refresh"                "init"                  
    #  [5] "save_latent_dynamics"   "output_dir"            
    #  [7] "output_basename"        "sig_figs"              
    #  [9] "chains"                 "parallel_chains"       
    # [11] "chain_ids"              "threads_per_chain"     
    # [13] "opencl_ids"             "iter_warmup"           
    # [15] "iter_sampling"          "save_warmup"           
    # [17] "thin"                   "max_treedepth"         
    # [19] "adapt_engaged"          "adapt_delta"           
    # [21] "step_size"              "metric"                
    # [23] "metric_file"            "inv_metric"            
    # [25] "init_buffer"            "term_buffer"           
    # [27] "window"                 "fixed_param"           
    # [29] "validate_csv"           "show_messages"         
    # [31] "cores"                  "num_cores"             
    # [33] "num_chains"             "num_warmup"            
    # [35] "num_samples"            "save_extra_diagnostics"
    # [37] "max_depth"              "stepsize"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      相关资源
      最近更新 更多