{"action":"created","issue":{"url":"https://api.github.com/repos/wch/R6/issues/69","labels_url":"https://api.github.com/repos/wch/R6/issues/69/labels{/name}","comments_url":"https://api.github.com/repos/wch/R6/issues/69/comments","events_url":"https://api.github.com/repos/wch/R6/issues/69/events","html_url":"https://github.com/wch/R6/issues/69","id":98851168,"number":69,"title":"Assigning values to self within a class without knowing their names","user":{"login":"sckott","id":577668,"avatar_url":"https://avatars.githubusercontent.com/u/577668?v=3","gravatar_id":"","url":"https://api.github.com/users/sckott","html_url":"https://github.com/sckott","followers_url":"https://api.github.com/users/sckott/followers","following_url":"https://api.github.com/users/sckott/following{/other_user}","gists_url":"https://api.github.com/users/sckott/gists{/gist_id}","starred_url":"https://api.github.com/users/sckott/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sckott/subscriptions","organizations_url":"https://api.github.com/users/sckott/orgs","repos_url":"https://api.github.com/users/sckott/repos","events_url":"https://api.github.com/users/sckott/events{/privacy}","received_events_url":"https://api.github.com/users/sckott/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2015-08-03T22:36:24Z","updated_at":"2015-08-04T17:15:09Z","closed_at":null,"body":"I want to make an R6 object that has a function that takes in a file, reads it, then assigns new values to the class that are then available to the user. However, I won't know the names of these values ahead of time. E.g., I want to do something like\r\n\r\n```r\r\nIni <- R6Class(\"Ini\",\r\n   portable = FALSE,\r\n   public = list(\r\n     file = NA,\r\n     parsed = NA,\r\n     initialize = function(file) {\r\n       self$file <- file\r\n     },\r\n     read = function() {\r\n       self$parsed <- read_file(self$file)\r\n       # where self$parsed is a named list\r\n       for (i in seq_along(self$parsed)) {\r\n        self[ names(self$parsed)[i] ] <- self$parsed[[i]]\r\n       } \r\n       self$parsed # return list to be printed\r\n     },\r\n     print = function() {\r\n       cat(paste0(\"<file: > \", self$file, \".\\n\"))\r\n       # other print info\r\n     }\r\n   )\r\n)\r\n```\r\n\r\nI could create a new object like `obj <- Ini$new(\"filepath\")`, then I want to access those new values like `obj$key` \r\n\r\nBut when this gives \r\n\r\n```r\r\nError in self[names(self$parsed)[i]] <- self$parsed[[i]] : \r\n  object of type 'environment' is not subsettable\r\n```\r\n\r\nI'm sure I'm just missing something easy here...Thanks for the help. "},"comment":{"url":"https://api.github.com/repos/wch/R6/issues/comments/127679688","html_url":"https://github.com/wch/R6/issues/69#issuecomment-127679688","issue_url":"https://api.github.com/repos/wch/R6/issues/69","id":127679688,"user":{"login":"wch","id":86978,"avatar_url":"https://avatars.githubusercontent.com/u/86978?v=3","gravatar_id":"","url":"https://api.github.com/users/wch","html_url":"https://github.com/wch","followers_url":"https://api.github.com/users/wch/followers","following_url":"https://api.github.com/users/wch/following{/other_user}","gists_url":"https://api.github.com/users/wch/gists{/gist_id}","starred_url":"https://api.github.com/users/wch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wch/subscriptions","organizations_url":"https://api.github.com/users/wch/orgs","repos_url":"https://api.github.com/users/wch/repos","events_url":"https://api.github.com/users/wch/events{/privacy}","received_events_url":"https://api.github.com/users/wch/received_events","type":"User","site_admin":false},"created_at":"2015-08-04T17:15:09Z","updated_at":"2015-08-04T17:15:09Z","body":"@sckott This works for me. I think it should be similar to what you're doing:\r\n\r\n```R\r\nlibrary(R6)\r\nTest <- R6Class(\"Test\",\r\n  portable = FALSE,\r\n  lock_objects = FALSE,\r\n  public = list(\r\n    add = function(name, value) {\r\n      self[[name]] <- value\r\n    }\r\n  )\r\n)\r\n\r\n\r\nobj <- Test$new()\r\nobj$add(\"x\", 10)\r\nobj$x\r\n#> [1] 10\r\n```"}}