user3664020
user3664020

Reputation: 3020

stop code execution if any error

I have code in which I am sourcing multiple codes one after the other. Something like below

source("t1.r")
source("t2.r")
source("t3.r")
source("t4.r")

While running this main script if any source statement gives an error, I don't want to source any remaining scripts (i.e. don't want to run any subsequent statements).

I don't want to write if error condition after every source statement. I want to do something universal and at the beginning only.

What change should I do in the main script to do this?

Upvotes: 1

Views: 727

Answers (1)

CuriousBeing
CuriousBeing

Reputation: 1632

Edited as per suggested by Nicola and RHertel

setwd("/Users/xxxx/Desktop/Sub")
scripts<-list.files(pattern="*.R")         

for (f in scripts)
{
  c<-try(source(f))                        
  ifelse (class(c)!="try-error", print(paste("Script Sourced:", f,sep=" ")), setwd("/Users/xxxx/Desktop")             
}

This script prints the scripts sourced. That way you can recognise which didnt get sourced.

Upvotes: 1

Related Questions