0

I am writing a package that contains two R files, file1.R and file2.R. The first file contains several function and S4 object definitions that the second file uses. I tried adding source("file1.R") to the top of file2.R but when I installed the library, it threw an error: cannot open file 'rgrokit.R': No such file or directory even though the two files are located in the same directory.

How do I make the second file dependent on the first?

Edit: If I do not try to create some form of dependency, the library installation will throw an error.

Error in setMethod("foo", "bar", function(.Object, ...) { : 
  no existing definition for function ‘foo’

This code appears in file2.R.

In this example, bar is an S4 class defined in file1.R and foo is a method which was created using setGeneric in file1.R

John Paul
  • 12,196
  • 6
  • 55
  • 75
Jon Claus
  • 2,862
  • 4
  • 22
  • 33
  • Because I'm curious, could you also describe what happens when you try to build the library without the `source()` command in the second file? – joran Jul 26 '13 at 18:05
  • You don't need the explicit `source` when building a package. The build process will automatically source all your .R files; the presence of the statement is probably confusing it. – Hong Ooi Jul 26 '13 at 18:07
  • The problem here is that the build process is sourcing file2.R before file1.R. See if combining the two files into one fixes it. – Hong Ooi Jul 26 '13 at 18:16
  • If I move the `setGeneric` call to create the method to `file2.R` as well, then it will correctly install. However, I would like for all the `setGeneric` calls to be in `file1.R` for the sake of organization. I would like to create all the methods for the S4 object in `file1.R` and then define them elsewhere. I think this is somewhat analogous to defining a class in a .h file and then defining the member functions in a .cpp file for C++. – Jon Claus Jul 26 '13 at 18:18
  • @Hong Ooi: yes, putting all the code in the one file would fix this issue. However, that file is getting rather lengthy (about 1000 lines) and I would like to split it up so it is more organized and easier to debug/edit later. – Jon Claus Jul 26 '13 at 18:19
  • @HongOoi Can't you change the order in which they are processed using `Collate`? – joran Jul 26 '13 at 18:24
  • As @joran mentioned using Collate to specify the order the files should be read in is the way to go here. – Dason Apr 26 '14 at 04:25

0 Answers0