代码之家  ›  专栏  ›  技术社区  ›  Jake Thompson

访问R6方法的形式参数

r6 r
  •  0
  • Jake Thompson  · 技术社区  · 4 年前

    有没有办法访问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\ ``). 但我似乎找不到以同样的方式访问Arumings的方法。 例如,此操作失败:

    # 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方法的形式参数?

    于2021 11月22日由 reprex package (v2.0.1)

    0 回复  |  直到 4 年前
        1
  •  2
  •   Mikael Jagan    4 年前

    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
    

    通过其中一个 a1 或 a2 到 formalArgs 获取形式参数的名称。

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

    R6类内部到 cmdstanr 是 "CmdStanModel" 和 $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"
    
    推荐文章