r - R调试()“找不到函数”,即使它存在
问题描述
当我尝试调试某个函数(它本身在函数 NbCluster 中定义)时,出现could not find function
错误。我已经检查过,有问题的函数在debug
被调用时肯定会被加载。
> data("USArrests")
> arrests <- scale(USArrests)
> source("NbCluster_copy.R")
> NbCluster_copy(data = arrests, diss = NULL, distance = "euclidean", min.nc = 2, max.nc = 12,
+ method = "ward.D2", index = "gap", alphaBeale = 0.1)
[1] "Indice.Gap exists"
Error in debug(fun = "Indice.Gap") : could not find function "Indice.Gap"
如果我手动单步执行该函数(通过选择并运行行而不是调用该函数),则不会发生此问题。我尝试制作一个最小的示例,但无法做到,所以我认为问题不是嵌套函数。
###This works as expected, when I run "wrapper", debug is called from within the function:
wrapper <- function(x){
wrapper <- function(x){
fun1 <- function(x){
fun0 <- function(x){
y = x + 1
return(y)
}
debug(fun0)
y = fun0(x) * 2
return(y)
}
fun1(x)
}
> wrapper(2)
debugging in: fun0(x)
debug at #3: {
y = x + 1
return(y)
}
Browse[2]>
debug at #4: y = x + 1
Browse[2]>
debug at #5: return(y)
Browse[2]>
exiting from: fun0(x)
[1] 6
这是我添加到 NbClust 函数中的部分。
if(exists("Indice.Gap")){
print("Indice.Gap exists")
}
debug(fun = "Indice.Gap")
在第一次调用之前Indice.Gap
:
resultSGAP <- Indice.Gap(x = jeu, clall = clall,
reference.distribution = "unif", B = 10, method = "ward.D2",
d = NULL, centrotypes = "centroids")
除了上面显示的之外,我只做了非常小的改动,但如果你想查看整个功能,我的副本在这里:https ://pastebin.com/wxKKDbHy
解决方案
只需删除调试中的引号,它应该可以工作:
debug(Indice.Gap)
应该做的伎俩。
outer_fun <- function() {
inner_fun <- function() 1
## does not work
# debug("inner_fun")
## works
debug(inner_fun)
inner_fun()
}
outer_fun()
在顶层很有趣,您可以将函数名称作为字符串提供:
debug("outer_fun") # works
debug(outer_fun) # works
推荐阅读
- webdriver - 基于期望语句结果的 if-else 条件
- sql - PostgreSQL - 使用 2 个自动递增的数字,第二个从 1 重新开始
- flutter - Flutter ChangeNotifierProxyProvider ChangeNotifier 在创建需要参数
- dictionary - Dart - Map vs Switch 语句性能
- javascript - 如何使用灯光模式嵌入 reddit
- css - CSS过渡/动画:悬停效果旋转
- c# - C# WPF Stlying & 在已以编程方式添加到数据网格中的按钮上添加图像
- sql - 尝试在 impala 中查找 LAST_VALUE() 时出错
- arrays - 如何使用 stringgrid 单元格的值存储在应该保存当前游戏状态的数组中?
- php - 如果 PHP 崩溃或调试停止,Nginx + PHP-FPM 如何返回 5xx 状态码?