r-使用if语句(或其他任何语句)安静地中断函数,不进行调试



是否可以使用if语句(或其他任何语句(悄悄地中断函数而不进行调试

目的:如果满足特定条件,我想退出函数(如for循环中的break语句(。该函数预计将以与mean()类似的方式使用(例如(,有时在dplyr操作和for循环中使用。此外,我希望避免复杂的嵌套if-else语句
v <- c(1, 2, 5, 1, 2)
break5 <- function(x){
# If this exception occur, then
# end up (escape ?) the whole function
if (x == 5){
# next      # <-- This will raise error message
break     # <-- This will raise error message
# stop()  # <-- This will raise debugging event
} 

# Otherwise, proceed the subsequent process
if (x == 1) print(1)
if (x == 2) print(2)
}

控制台输出:

> break5(1)
[1] 1
> break5(2)
[1] 2
> break5(5)
Error in break5(5) : no loop for break/next, jumping to top level

期望在for循环中得到作为break的静默结果

for (i in v){
if (i == 5) break # <-- Similar with this procedure that 
#     could be included in the function
break5(i)
}

示例1

x0 <- 1:100
y0 <- 1:100
example <- function(x = NULL, y = NULL){

if (is.null(x) | is.null(y)){    
break                            # Case 1
}

if (!(x %in% x0) | !(y %in% y0)){
break                            # Case 2
}

if (x < 2){
z <- 1                           # Case 3
return(z)
}

if ((x >= 80) & (x <= 100)){
break                            # Case 4
}

z <- x + y                        # Otherwise
return(z)
}
example(1)    # Case 1, this should be muted
example(30, 999)    # Case 2, this should be muted
example(1, 1)    # Case 3, this is OK
example(80, 1)    # Case 4, this should be muted
example(11, 1)    # Otherwise, this is OK

示例2:

for (i in 75:90){
print(paste0(i, '+', i, '=', example(i, i)))  # The rest should be muted
}

是的,可以通过返回suppressWarnings()expr来实现

x0 <- 1:100
y0 <- 1:100
example <- function(x = NULL, y = NULL){

if (is.null(x) | is.null(y)){
return(suppressWarnings(warning()))     # Case 1
}
warning
if (!(x %in% x0) | !(y %in% y0)){
return(suppressWarnings(warning()))     # Case 2
}

if (x < 2){
z <- 1                                  # Case 3
return(z)
}

if ((x >= 80) & (x <= 100)){
return(suppressWarnings(warning()))     # Case 4
}

z <- x + y                                  # Otherwise
return(z)
}

示例1

example(1)          # Case 1, Error/Warning message was muted
example(30, 999)    # Case 2, Error/Warning message was muted
example(1, 1)       # Case 3, this is OK
example(80, 1)      # Case 4, Error/Warning message was muted
example(11, 1)      # Otherwise, this is OK

控制台输出:

> example(1)
> example(30, 999) 
> example(1, 1) 
[1] 1
> example(80, 1)
> example(11, 1)
[1] 12

示例2

for (i in 75:90){
print(paste0(i, '+', i, '=', example(i, i)))  # The rest was muted
}

控制台输出:

> for (i in 75:90){
+     print(paste0(i, '+', i, '=', example(i, i)))
+ }
[1] "75+75=150"
[1] "76+76=152"
[1] "77+77=154"
[1] "78+78=156"
[1] "79+79=158"
[1] "80+80="
[1] "81+81="
[1] "82+82="
[1] "83+83="
[1] "84+84="
[1] "85+85="
[1] "86+86="
[1] "87+87="
[1] "88+88="
[1] "89+89="
[1] "90+90="

最新更新