Search code examples
rpython-3.xr6

R - translating Python class to R's R6 class - unique names error


I am working on translating the word search algorithm implemented in both Julia (for the main code -- https://rosettacode.org/wiki/Word_search#Julia) and Python (for the creation of the class for Grid -- https://rosettacode.org/wiki/Word_search#Python).

I am attempting to rewrite the class definition of Grid from Python (see below) into R:

class Grid:
    def __init__(self):
        self.num_attempts = 0
        self.cells = [['' for _ in range(n_cols)] for _ in range(n_rows)]
        self.solutions = []

The following is my attempt at translating the Python class into an R6 class:

library("R6")

Grid <- R6Class("Grid",
public = list(
num_attempts = NULL,
cells = NULL,
solutions = NULL,
initialize = function(num_attempts = NA, cells = NA, solutions = NA) {
self$num_attempts <- 0
self$cells <- cells
self$solutions()
},
cells = function(val) {
for (val in seq_along(ncols)) {
for (val in seq_along(nrows))
{
result <- vector("character")
result
}
}
}
)
)

The following is the error message that I receive in R:

Error in R6Class("Grid", public = list(num_attempts = NULL, cells = NULL,  : 
  All items in public, private, and active must have unique names.

Please offer suggestions on how to correctly perform this translation.

Thank you.


Solution

  • You refer to cells twice. Once you set it to Null, then to a function. That’s causing your error, I believe.