如何在控制台上没有打印 return 值的情况下创建函数中断?
How to create a break in function with no return value printed on console?
我知道 invisible()
函数,但当有人想打破内部函数时它似乎不起作用:
bar <- function() {
if (file.exists("data/some.rds") | (1 + 1 == 2)) return()
"something else"
}
foo <- function() {
if (file.exists("data/some.rds") | (1 + 1 == 2)) invisible()
"something else"
}
bar()
> NULL
foo()
> [1] "something else"
注意:(1 + 1 == 2)
计算结果为 TRUE 并在此处用于制作可重现的示例。
invisible()
修改对象的属性。如果你想提前离开功能,你仍然需要显式return。这将隐形置于 return.
内
foo <- function() {
if (file.exists("data/some.rds") | (1 + 1 == 2)) return(invisible())
"something else"
}
我知道 invisible()
函数,但当有人想打破内部函数时它似乎不起作用:
bar <- function() {
if (file.exists("data/some.rds") | (1 + 1 == 2)) return()
"something else"
}
foo <- function() {
if (file.exists("data/some.rds") | (1 + 1 == 2)) invisible()
"something else"
}
bar()
> NULL
foo()
> [1] "something else"
注意:(1 + 1 == 2)
计算结果为 TRUE 并在此处用于制作可重现的示例。
invisible()
修改对象的属性。如果你想提前离开功能,你仍然需要显式return。这将隐形置于 return.
foo <- function() {
if (file.exists("data/some.rds") | (1 + 1 == 2)) return(invisible())
"something else"
}