0

I am working on a project in R. In my folder, I have several R files that each contains lots of functions and some codes in the end to test all the functions in that file. Then I have a file called "main.R". I intend to call all the functions that I wrote in other files. I tried to source all those files at the beginning of my "main.R", however, I noticed that when I run "main.R", the test codes written in the sourced files will also run which is not what I want.

Is there any way that I can only source them but not run the tests code? Something like pre-prosser in C++ maybe?

AprilSun
  • 1
  • 1
  • 1
    note that the answer to the linked question *does* run the additional material in the file, it just doesn't return the values. There is a deleted answer to the linked question that attempts to use regular expressions to parse the source file and eliminate everything outside of the function calls ... – Ben Bolker Oct 26 '15 at 02:54
  • 1
    Since you presumably wrote the sourced files and could modify them just make sure that the test code is always at the end and then put a line like this just before the test code: `### test ###`. If the file to be sourced is "s.R" then do this: `Lines <- readLines("s.R"); source(textConnection(Lines[1:grep("### test ###", Lines)]))` – G. Grothendieck Oct 26 '15 at 03:11
  • 1
    Though I like some of the suggestions in the link, assuming that packages (or even [modules](https://github.com/klmr/modules)) aren't your cup-o-tea, one thing I do with work-in-progress source files is encapsulate non-functions in `if (FALSE) {` and `}` ... it allows easy back-and-forth production and doesn't penalize if/when you accidentally source the whole file. (Yes, it's a kludge.) – r2evans Oct 26 '15 at 03:22

0 Answers0