Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make RStudio stop when meeting error or warning

Tags:

r

rstudio

When I select several lines of codes in a R script, and run it, RStudio will "smoothly" run all of the codes, even though there are some warnings and errors in the middle part. As a result, I have to carefully check the "Console" window, and to see whether there is any red lines. This is really time consuming and I may miss the errors. Are there some ways to make the running stop, when error or warning occurs?

like image 343
T X Avatar asked Mar 07 '19 12:03

T X


2 Answers

RStudio currently works by pasting the selected text into the console. It doesn't care if there are errors in it. A better approach would be to get the text and source it.

You can get the selected text using

selected <- rstudioapi::getSourceEditorContext()$selection[[1]]$text

If you source this text instead of pasting it, it will stop at the first error. Do that using

source(exprs = parse(text = selected), echo = TRUE)

Another way to go would be to copy the text into the clipboard, then sourcing it from there. I don't think RStudio currently has a way to do that, but you can add one.

This function reads from the clipboard on Windows and MacOS; I'm not sure if pbpaste is generally available on Linux, but there should be some equivalent there:

readClipboard <- function() {
  if (.Platform$OS.type == "windows") 
    lines <- readLines("clipboard")
  else
    lines <- system("pbpaste", intern=TRUE)
  lines
}

This code sources the text from the clipboard:

source(exprs = parse(text = readClipboard()), echo = TRUE)

You could put either of these actions on a hot key in RStudio as an add-in. Instructions are here: https://rstudio.github.io/rstudioaddins/.

The advice above only stops on errors. If you want to also stop on warnings, use options(warn = 2) as @FransRodenburg said.

like image 81
user2554330 Avatar answered Oct 13 '22 01:10

user2554330


There are many ways you can force your script to stop when you encounter an error:

  1. Save your script and run it using source(yourscript.R);
  2. Wrap your script in a function and try and use the function;
  3. Work in an Rmarkdown file and try and execute a chunk containing all the code you want to run (or try knitting for that matter);

If you really want to stop your script when a warning occurs, you could force warnings to be errors by using options(warn = 2) at the beginning of your script. If you just want to get rid of the red (lol), you can also suppress harmless warnings you have already checked using suppressWarnings(), or suppress all warnings for your script with options(warn = -1).

Be careful using options() outside a saved script though, lest you forget you have globally disabled warnings, or turned them into errors.

Depending on how large your script is, you may also just want to run it bit by bit using CTRL+Enter, rather than selecting lines.

like image 30
Frans Rodenburg Avatar answered Oct 13 '22 00:10

Frans Rodenburg