访问 R6 方法的形式参数

Access formal arguments of an R6 method

有没有办法访问 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 创建

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" 对象访问 public 方法,如下所示:

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 class 是 "CmdStanModel"$sample() 是 class 的 public 方法,所以你可以使用

访问 $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"