Why Does the R Interpreter Continue to Excute Code After an Error? -
when send block of r code interpreter in rstudio (or tinn-r or other environment) if there's error on line 1, lines 2, 3, 4 ... still execute. why default behavior? seems contrary how programming languages work , dangerous in sense if line 1 produces error alter subsequent lines of code do. it's particularly bad in long scripts have lot of printed output because can miss error message amidst regular output. there reason, either logical or historical, why r works way? , possible change behavior , ensure interpreter stop upon encountering error?
consider r code error:
print("starting") b+sdlkfjsflkj print("hello world")
if select code , copy interpreter, describe continue past error:
> print("starting") [1] "starting" > b+sdlkfjsflkj error: object 'b' not found > print("hello world") [1] "hello world"
a simple solution causes stop @ error storing in script , running source
:
source("play.r") [1] "starting" error in eval(expr, envir, enclos) : object 'b' not found
if want keep working interactively, option placing code inside block. if copy following code r interactively...
{ print("starting") b+sdlkfjsflkj print("hello world") }
... following output:
[1] "starting" error: object 'b' not found
Comments
Post a Comment