Dear R users, Suppose I have 2 R script files: 'test1.R' and 'test2.R' and one R file 'main.R' which sources each of them. I wonder if there is a way to skip rest of code in 'test1.R' once a condition is met but still continue to run rest of script from the 'main'R' file. A simple example is shown below. I hope only 'message1' and 'message3' can be shown up without using if(...) else statement for skipped part in 'test1.R'. #main.R: source("test1.R") source("test2.R") ======================================#test1.R # In this example the goal is to skip executing the line of print("message2"). # without using if() else statement. x <- TRUE if (x) { print("message1") q() # want to skip rest of code in test1.R ONLY } # lots of lines can start from here. print("message2") ======================================#test2.R print("message3") Thank you. Ming-chung Li [[alternative HTML version deleted]]
?try see if: try(source('file')) works when you use 'stop()' instead of 'q()' in your example. On Thu, Apr 30, 2009 at 10:24 AM, Ming-Chung Li <mli@emmes.com> wrote:> Dear R users, > > Suppose I have 2 R script files: 'test1.R' and 'test2.R' and one R file > 'main.R' which sources each of them. I wonder if there is a way to skip rest > of code in 'test1.R' once a condition is met but still continue to run rest > of script from the 'main'R' file. > > A simple example is shown below. I hope only 'message1' and 'message3' can > be shown up without using if(...) else statement for skipped part in > 'test1.R'. > > #main.R: > source("test1.R") > source("test2.R") > ======================================> #test1.R > # In this example the goal is to skip executing the line of > print("message2"). > # without using if() else statement. > x <- TRUE > if (x) { > print("message1") > q() # want to skip rest of code in test1.R ONLY > } > # lots of lines can start from here. > print("message2") > ======================================> #test2.R > print("message3") > > > Thank you. > Ming-chung Li > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
How about: x <- F if (x) { print("true") } else { print("false") } Bart mli-2 wrote:> > Dear R users, > > Suppose I have 2 R script files: 'test1.R' and 'test2.R' and one R file > 'main.R' which sources each of them. I wonder if there is a way to skip > rest of code in 'test1.R' once a condition is met but still continue to > run rest of script from the 'main'R' file. > > A simple example is shown below. I hope only 'message1' and 'message3' can > be shown up without using if(...) else statement for skipped part in > 'test1.R'. > > #main.R: > source("test1.R") > source("test2.R") > ======================================> #test1.R > # In this example the goal is to skip executing the line of > print("message2"). > # without using if() else statement. > x <- TRUE > if (x) { > print("message1") > q() # want to skip rest of code in test1.R ONLY > } > # lots of lines can start from here. > print("message2") > ======================================> #test2.R > print("message3") > > > Thank you. > Ming-chung Li > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- View this message in context: http://www.nabble.com/How-to-skip-rest-of-code-tp23320816p23365295.html Sent from the R help mailing list archive at Nabble.com.
Seemingly Similar Threads
- Queue and Agent functionality
- Howto make deleted itens visible to users?
- Mailbox Hashing
- stubbing a method that yeilds sequential results
- [Bug 973] sshd behaves differently while doing syslog entries for tcpwrappers denied message, with -r and without -r option.