Recursive class in R

i'm trying to do an "node" Class in R, like in java: but the code pop up an error, "The class node doesn't exist when i'm creating the value "Next="node" is it not possible to do recursion in a class in R? or how could i do this?

node <- setRefClass("node", fields = list(value="numeric", next="node"))

Error: inesperado '=' in "node <- node <- setRefClass("node", fields = list(value="numeric", next=""

Upvotes: -1

Views: 94

Answers (1)

Oliver
Oliver

Reputation: 8602

The problem here is that you are using the flow Control word next. Try help(next) and this description comes up

These are the basic control-flow constructs of the R language. They function in much the same way as control statements in any Algol-like language. They are all reserved words.

As such they cannot be used for variable names, and if they are used for list names they should be quoted. Eg. this will work:

setRefClass('node', fields = list(value = 'numeric', 'next' = 'node'))

note that I wrote 'next' and not next
As a side note I would suggest checking out the R6 package, which provides a simpler interface to OOP than reference classes while also being much faster than reference classes.

Upvotes: 1

Related Questions