使用什么而不是在 Rstudio 中停止?
What to use instead of stop in Rstudio?
我想知道当用户输入 'bad' 参数 时,是否有一种方法可以让我的函数在图中发送文本(请参阅我的下面的 R 代码),停止进一步处理(即 除了消息 之外没有生成输出), 但不会崩溃(stop
的行为方式)?
例如,当b
不大于a
时,有没有办法只绘制一条消息并停止进一步处理而不使用导致页面的stop
崩溃?
这是我的 R 代码:
GGG = function(a, b){
if(b > a) { c = b - a ## Necessary condition to be met !
} else {
plot(1, axes = F, ty = 'n', ann = F) ## Text Message to be plotted
text(1, 1, "Unable to process this setting", cex = 2, col = 'red4', font = 2)
stop("b must be larger than a" ) ## This just crashes the R studio
}
d = c / 100 ## this should NOT run in this example because b < a in my example
return(d)
}
GGG (a = 3, b = 2) ## b < a, thus function should just plot message
您可以在函数中使用 return()
到 return "nothing"(即 NULL
),并使用 warning()
代替 stop()
所以它不 'crash' 函数
GGG = function(a, b){
if(b > a) {
c = b - a ## Necessary condition to be met !
} else {
plot(1, axes = F, ty = 'n', ann = F) ## Text Message to be plotted
text(1, 1, "Unable to process this setting", cex = 2, col = 'red4', font = 2)
# warning("b must be larger than a" )
# return()
return(message("b must be larger than a" ))
}
d = c / 100
return(d)
}
我想知道当用户输入 'bad' 参数 时,是否有一种方法可以让我的函数在图中发送文本(请参阅我的下面的 R 代码),停止进一步处理(即 除了消息 之外没有生成输出), 但不会崩溃(stop
的行为方式)?
例如,当b
不大于a
时,有没有办法只绘制一条消息并停止进一步处理而不使用导致页面的stop
崩溃?
这是我的 R 代码:
GGG = function(a, b){
if(b > a) { c = b - a ## Necessary condition to be met !
} else {
plot(1, axes = F, ty = 'n', ann = F) ## Text Message to be plotted
text(1, 1, "Unable to process this setting", cex = 2, col = 'red4', font = 2)
stop("b must be larger than a" ) ## This just crashes the R studio
}
d = c / 100 ## this should NOT run in this example because b < a in my example
return(d)
}
GGG (a = 3, b = 2) ## b < a, thus function should just plot message
您可以在函数中使用 return()
到 return "nothing"(即 NULL
),并使用 warning()
代替 stop()
所以它不 'crash' 函数
GGG = function(a, b){
if(b > a) {
c = b - a ## Necessary condition to be met !
} else {
plot(1, axes = F, ty = 'n', ann = F) ## Text Message to be plotted
text(1, 1, "Unable to process this setting", cex = 2, col = 'red4', font = 2)
# warning("b must be larger than a" )
# return()
return(message("b must be larger than a" ))
}
d = c / 100
return(d)
}