R - 将 Python class 翻译成 R 的 R6 class - 唯一名称错误
R - translating Python class to R's R6 class - unique names error
我正在翻译在 Julia 中实现的单词搜索算法(主要代码 -- https://rosettacode.org/wiki/Word_search#Julia) and Python (for the creation of the class for Grid -- https://rosettacode.org/wiki/Word_search#Python)。
我正在尝试将 Grid 的 class 定义从 Python(见下文)重写为 R:
class Grid:
def __init__(self):
self.num_attempts = 0
self.cells = [['' for _ in range(n_cols)] for _ in range(n_rows)]
self.solutions = []
以下是我尝试将 Python class 翻译成 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
}
}
}
)
)
以下是我在R中收到的错误信息:
Error in R6Class("Grid", public = list(num_attempts = NULL, cells = NULL, :
All items in public, private, and active must have unique names.
请提供有关如何正确执行此翻译的建议。
谢谢。
您引用了单元格两次。一旦将其设置为 Null,然后设置为一个函数。我相信这是导致你的错误的原因。
我正在翻译在 Julia 中实现的单词搜索算法(主要代码 -- https://rosettacode.org/wiki/Word_search#Julia) and Python (for the creation of the class for Grid -- https://rosettacode.org/wiki/Word_search#Python)。
我正在尝试将 Grid 的 class 定义从 Python(见下文)重写为 R:
class Grid:
def __init__(self):
self.num_attempts = 0
self.cells = [['' for _ in range(n_cols)] for _ in range(n_rows)]
self.solutions = []
以下是我尝试将 Python class 翻译成 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
}
}
}
)
)
以下是我在R中收到的错误信息:
Error in R6Class("Grid", public = list(num_attempts = NULL, cells = NULL, :
All items in public, private, and active must have unique names.
请提供有关如何正确执行此翻译的建议。
谢谢。
您引用了单元格两次。一旦将其设置为 Null,然后设置为一个函数。我相信这是导致你的错误的原因。