2

R 参照クラスのフィールドを保存しようとしています:

myclass = setRefClass("myclass",
                       fields = list(
                           x = "numeric",
                           y = "numeric"
                       ))


myclass$methods(
    dfunc = function(i) {
        message("In dfunc, I save x and y...")
        base::save(.self$x, .self$y, file="/tmp/xy.rda")
    }
    )


myclass$methods(
    initialize = function(x, y) {
        x <<- x
        y <<- y
    }
)

どうやらそれはうまくいきません:

x = myclass(5, 6)
x$afunc(1)
x$dfunc()

Error in base::save(.self$x, .self$y, file = "/tmp/xy.rda") : 
  objects ‘.self$x’, ‘.self$y’ not found 

次に、.self を添付しようとしました。

myclasr = setRefClass("myclass",
                       fields = list(
                           x = "numeric",
                           y = "numeric"
                       ))


myclass$methods(
    dfunc = function(i) {
        message("In dfunc, I save x and y...")
        attach(.self)
        base::save(x, y, file="/tmp/xy.rda")
        detach(.self)
    }
    )


myclass$methods(
    initialize = function(x, y) {
        x <<- x
        y <<- y
    }
)

別のエラーが発生しました:

Error in attach(.self) : 
  'attach' only works for lists, data frames and environments
4

1 に答える 1

2

使用できます

myclass$methods(
  dfunc = function(i) {
    message("In dfunc, I save x and y...")
    tempx <- .self$x
    tempy <- .self$y
    base::save(tempx, tempy, file="xy.rda")
  }
)

また

myclass$methods(
  dfunc = function(i) {
    message("In dfunc, I save x and y...")
    base::save(x, y, file="xy.rda")
  }
)

saveもちろん存在しないas.character(substitute(list(...)))[-1L]という名前の変数を文字通り探します。.self$x

于 2014-08-13T12:53:16.357 に答える