简体   繁体   English

在 R6 class 内部定义:“找不到对象”(或:如何在 R6 类中定义“本地”对象)

[英]Inside R6 class definition: 'object not found' (or: how to define 'local' objects in R6 classes)

I want to define an R6 class that sets up, updates and closes a progress bar.我想定义一个设置、更新和关闭进度条的 R6 class。 For these 3 tasks, I have 3 functions.对于这 3 个任务,我有 3 个功能。 The first, setup_progressbar() , calls R 's txtProgressbar() which returns an object (say, pb ) which needs to be passed on to the second and third functions, update_progressbar() and close_progressbar() .第一个setup_progressbar()调用RtxtProgressbar() ,它返回一个 object (比如pb ),它需要传递给第二个和第三个函数update_progressbar()close_progressbar() But the object pb is not found by the latter two functions.但是后两个函数没有找到 object pb

library(R6)
myprogressbar <- R6Class("my_progress_bar",
                         public = list(
                             n = numeric(1),
                             initialize = function(n) {
                                 stopifnot(n >= 1)
                                 self$n <- n
                             },
                             setup_progressbar = function() {
                                 pb <- txtProgressBar(max = self$n)
                             },
                             update_progressbar = function(i) {
                                 setTxtProgressBar(pb, i)
                             },
                             close_progressbar = function () {
                                 close(pb)
                                 cat("\n")
                             }
                         ))
mypb <- myprogressbar$new(10)
mypb$setup_progressbar()
mypb$update_progressbar(3) # Error in setTxtProgressBar(pb, i) : object 'pb' not found

I tried to add pb to self in the hope it would be found, but then I obtain "cannot add bindings to a locked environment" .我试图将pb添加到self希望它会被发现,但后来我得到"cannot add bindings to a locked environment"

Note: In my actual (non-minimal) example, the i is found/provided/visible, so that's not an additional problem (most likely this is just a problem in the above minimal working example once fixed beyond the 'pb' not found error).注意:在我的实际(非最小)示例中, i被发现/提供/可见,所以这不是一个额外的问题(很可能这只是上述最小工作示例中的一个问题,一旦修复超出了'pb' not found错误)。

The following works:以下作品:

library(R6)
myprogressbar <- R6Class("my_progress_bar",
                         public = list(
                             n = numeric(1),
                             pb = NULL, # provide as argument
                             initialize = function(n, pb = NULL) { # provide with default so that $new() doesn't require 'pb'
                                 stopifnot(n >= 1)
                                 self$n <- n
                             },
                             setup_progressbar = function() {
                                 self$pb <- txtProgressBar(max = self$n)
                             },
                             update_progressbar = function(i) {
                                 setTxtProgressBar(self$pb, i)
                             },
                             close_progressbar = function () {
                                 close(self$pb)
                                 cat("\n")
                             }
                         ))

mypb <- myprogressbar$new(10)
mypb$setup_progressbar()
mypb$update_progressbar(3) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM