Skip to content

Instantly share code, notes, and snippets.

@yutannihilation
Created February 22, 2015 03:15
Show Gist options
  • Save yutannihilation/98ca0d9eb50ad4386f79 to your computer and use it in GitHub Desktop.
Save yutannihilation/98ca0d9eb50ad4386f79 to your computer and use it in GitHub Desktop.
R6を試したメモ
library(R6)
methods(class = "R6")
#> [1] print.R6*
#>
#> Non-visible functions are asterisked
Numbers <- R6Class("Numbers",
public = list(
x = 100
),
active = list(
x2 = function(value) {
if (missing(value)) return(self$x * 2)
else self$x <- value/2
},
rand = function() rnorm(1)
)
)
n <- Numbers$new()
# タブ補完は利かない
n$x
#> [1] 100
names(n)
#> NULL
ls(n)
#> [1] "rand" "x" "x2"
as.list(n)
#> Error in as.vector(x, "list") :
#> cannot coerce type 'environment' to vector of type 'list'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment