运行 多行代码但在 R 中因错误而停止(来自 RStudio)?

Run multiple lines of code but stop on error in R (from RStudio)?

我有一个很长很精致的脚本,我想 运行 每一行连续的代码 如果上一行成功(即没有错误)。如果 any 行错误,代码执行应立即停止。 RStudio 中是否有任何键盘快捷键可以让我们实现这一点(例如类似于 command + enter,但在出错时停止)?

示例和期望的输出

如果我们突出显示这些代码行并 运行 它们(在 RStudio 或 R 的其他地方,例如在终端中)

2 * 2
b * 5
4 * 4
7 * 1

我们看到了

> 2 * 2
[1] 4
> b * 5
Error: object 'b' not found
> 4 * 4
[1] 16
> 7 * 1
[1] 7

我想要某种方式让我只能看到

> 2 * 2
[1] 4
> b * 5
Error: object 'b' not found

即代码执行在第一个错误处停止

备注

我会尝试在不改变代码本身的情况下解决这个问题,最好是在 RStudio 中使用键盘快捷键,但我对其他想法非常开放,例如设置选项 (options())

使用Ctrl+Shift+Enter(或Mac等效项, Command+Shift+Enter) 到 运行 脚本在第一个错误处停止,所以这可能是一个简单的解决方案。