【发布时间】:2014-12-22 07:22:27
【问题描述】:
我有一个类似于下面的函数,在循环中创建矩阵。
foo <- function(x, y, z) {
out <- list(a1 = NULL, a2 = NULL, a3 = NULL)
for (i in 1:3) {
t <- 100 * i
a <- matrix(x, y + t, z)
out[[i]] <- t(a)
}
return(out)
}
以下运行良好。 p
但以下给出cannot allocate vector of length 错误
q <- foo(NA, 3500000, 50)
我想在我的函数中出现的cannot allocate vector of length 或too many elements specified 错误之后添加一些额外的message,例如adjust arguments 'y' and 'z'。
我正在尝试try 和tryCatch,但当错误发生在循环中时,似乎无法获得预期的结果。如何做到这一点?
【问题讨论】:
-
有
try和tryCatch代码你试过了吗?你能指定这个测试的精确“期望结果”是什么吗? -
如果函数以
Error: cannot allocate vector of size或too many elements specified停止,请提供附加消息adjust arguments 'y' and 'z'。
标签: r error-handling try-catch