Reputation: 2527
I am using RStudio on Mac and like to highlight a chunk of code and hit Command + Return
to get this chunk to run. For some reason, when I do this now, the code stops running when an error appears. How can I get it to go back to the way it used to be when all of the lines would run even if a previous line returned an error?
Upvotes: 2
Views: 1695
Reputation: 286
In case this helps anyone who comes to the page, this was due to a change in the default behaviour by RStudio. The issue was raised and discussed on github.
They've added a checkbox to return to the old behaviour in the new daily builds. After installing the latest version you can go to Tools - Global Options - Console, and then uncheck "Discard pending console input on error".
Upvotes: 4
Reputation: 461
It sounds as though your shortcut may be bound to the source
execution method, which executes the file as a whole, instead of the interactive run
. You may need to check or update your shortcuts.
Upvotes: 1
Reputation: 684
Possible duplicate of: How to ignore error while running r script.
The easiest method is to use the tryCatch()
function. As explained in detail here: Debugging, condition handling, and defensive programming
A good example of the syntax is given here: How to write trycatch in R. As shown:
result = tryCatch({
expr
}, warning = function(warning_condition) {
warning-handler-code
}, error = function(error_condition) {
error-handler-code
}, finally={
cleanup-code
})
You can test it like this:
result <- tryCatch({1==0}, error=function(e) e)
if (inherits(result, "error")) {
print("There was an error")
} else {
# pass through, there was no error.
}
Here is a good example of how to implement this tryCatch()
in to a function: github/cran/seastests/R/qs.R.
Upvotes: 1