0

When working in R projects, my workspace is usualy organized with several R child-files sourced by a centralized mother-file.

Basically this looks like:

mother.r

print("Go")
source("child1.r")
print("Step 1 OK")
source("child2.r")
print("Step 2 OK")

child1.r and child2.r

print("child1.r, starting")
doSomething()
doSomethingElse()
everythingIsFine = TRUE
stopifnot(everythingIsFine)
#testing
doSomeVerifications()

With this, I can source mother.r to have my study regenerated after some modifications, which will print a report.

The point is that the end of my childX.r is full of code that can be useful for some testings, but should not be sourced by mother.r. But if I use stop() before doSomeVerifications(), the mother sourcing will stop too.

How can I stop the sourcing just before the verifications ?

Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92
  • 2
    You can just change create minimal R package with your functions and use what you want instead of sourcing everything :-) – pogibas Aug 27 '18 at 10:00
  • 2
    You could use a global boolean variable `runTests` and place testing lines within if statements using `runTests`. Or use `try()` within mother.r? – Teun Aug 27 '18 at 10:03
  • @PoGibas I used functions for the example, but there is actually a lot of code in each child (since there is one for package loading, data-management, testings, reporteRs output, etc). – Dan Chaltiel Aug 27 '18 at 10:06
  • @Teun the global variable is a good idea. If there is nothing more base R specific I will probably choose that. `try()` could also do the trick with some additional code, I have to think about it. – Dan Chaltiel Aug 27 '18 at 10:10
  • along the lines of the comments here, check out: https://stackoverflow.com/questions/14612190/is-there-a-way-to-source-and-continue-after-an-error – chinsoon12 Aug 27 '18 at 10:24
  • @chinsoon12 yes I've already seen this question, but answers are way not tidy enough. I think the purpose of the question is not the same than mine. – Dan Chaltiel Aug 27 '18 at 10:27

0 Answers0