我试过以下代码:
x <- NULL
tryCatch(
x <- sqrt(-1),
warning = function(cond) print(paste("Ignore: ", conditionMessage(cond)))
)
# [1] "Ignore: NaNs produced"
预期输出:
# [1] "Ignore: NaNs produced"
x
#NaN
请注意,x仍然是NULL.未分配任何值.
- 我试了
x <<-
次,但不管用. - 我想展示被忽略的警告,而不是仅仅压制警告.
suppressWarnings(x <- sqrt(-1))
后我不知道如何恢复警告信息.