缺少没有默认值的 x。在 R 函数中调用函数
missing x with no default. Calling functions within functions in R
我正在使用从 YouTube 找到的 video 编写代码来解决数独难题,该代码通过 Python 编写了相同的算法。这段代码需要三个函数才能
- 找到一个空方格。
- 在空白方块中插入一个数字。
- 测试这个数字是否有效来解决这个难题。
这是对求解器使用回溯算法。
我在调用函数时遇到问题,但出现错误:
Error in free_squ(x) : argument "x" is missing, with no default
In addition: Warning message:
In if (empty_sq == FALSE) { :
the condition has length > 1 and only the first element will be used
Called from: free_squ(x)
这令人困惑,因为我只在 运行 这段代码时才明白。所以我可以编写其他函数来调用各个函数来分析插入到覆盖函数中的参数:
function1(argument){
function2(argument){
function3(argument){
***DO STUFF***}}}
为什么下面的代码在主函数中的函数不识别参数?
sudoku_solve <- function(x){
empty_sq <- free_squ(x) # Define a new object to give coordinates of empty square
if(empty_sq == FALSE){ # If no empty square can be found
return(x) # Return the matrix
} else{
empty_sq <- empty_sq # Pointless line kept for clarity
}
for(i in c(1:9)){ # Integers to insert into the found empty square
if(valid(x, i, empty_sq) == TRUE){ # can the intiger be placed in this square?
x[empty_sq[1], empty_sq[2]] = i # if i valid, insert into empty square
}
if(sudoku_solve()){ # are all i's valid?
return(TRUE) # All i's valid
} else{
x[empty_sq[1], empty_sq[2]] = 0 # reset the initial try and try again with another
}
}
return(FALSE)
}
我把数独游戏命名为'puzzle',调用函数如下:
sudoku_solve(puzzle)
我认为在下面的语句中,您没有向函数传递任何值,并且 x 也没有默认值。
if(sudoku_solve()){ # are all i's valid?
return(TRUE) # All i's valid
}
因此,虽然最初传递了参数,但在循环后再次调用该函数时,它是不带参数调用的。所以你在 sudoku_solve() 中传递给 free_sq(x),它给出了一个错误。
empty_sq <- free_squ(x)
确保将值传递给 sudoku_solve 或在 sudoku_solve 或 free_squ class/function 中设置 x 凋零的默认值。
我正在使用从 YouTube 找到的 video 编写代码来解决数独难题,该代码通过 Python 编写了相同的算法。这段代码需要三个函数才能
- 找到一个空方格。
- 在空白方块中插入一个数字。
- 测试这个数字是否有效来解决这个难题。
这是对求解器使用回溯算法。
我在调用函数时遇到问题,但出现错误:
Error in free_squ(x) : argument "x" is missing, with no default
In addition: Warning message:
In if (empty_sq == FALSE) { :
the condition has length > 1 and only the first element will be used
Called from: free_squ(x)
这令人困惑,因为我只在 运行 这段代码时才明白。所以我可以编写其他函数来调用各个函数来分析插入到覆盖函数中的参数:
function1(argument){
function2(argument){
function3(argument){
***DO STUFF***}}}
为什么下面的代码在主函数中的函数不识别参数?
sudoku_solve <- function(x){
empty_sq <- free_squ(x) # Define a new object to give coordinates of empty square
if(empty_sq == FALSE){ # If no empty square can be found
return(x) # Return the matrix
} else{
empty_sq <- empty_sq # Pointless line kept for clarity
}
for(i in c(1:9)){ # Integers to insert into the found empty square
if(valid(x, i, empty_sq) == TRUE){ # can the intiger be placed in this square?
x[empty_sq[1], empty_sq[2]] = i # if i valid, insert into empty square
}
if(sudoku_solve()){ # are all i's valid?
return(TRUE) # All i's valid
} else{
x[empty_sq[1], empty_sq[2]] = 0 # reset the initial try and try again with another
}
}
return(FALSE)
}
我把数独游戏命名为'puzzle',调用函数如下:
sudoku_solve(puzzle)
我认为在下面的语句中,您没有向函数传递任何值,并且 x 也没有默认值。
if(sudoku_solve()){ # are all i's valid?
return(TRUE) # All i's valid
}
因此,虽然最初传递了参数,但在循环后再次调用该函数时,它是不带参数调用的。所以你在 sudoku_solve() 中传递给 free_sq(x),它给出了一个错误。
empty_sq <- free_squ(x)
确保将值传递给 sudoku_solve 或在 sudoku_solve 或 free_squ class/function 中设置 x 凋零的默认值。