Question

I am creating an R package. The package defines several reference classes. There is a dependency between the classes because one is the base class and the others are subclasses. Due to this dependency, the package files must be loaded in a certain order: the base class first, then the subclasses. With just my luck, however, R is loading the subclasses first and complaining that the base class is not defined.

How can I force the package's source files to be executed in a particular order?

My current, kludge "solution" is to put all class definitions into one source file, with the base class defined first. Talk about ugly. There's gotta be a better way.

Was it helpful?

Solution

I think the Collate: field does that for S4 classes and may help here too. From the Writing R Extensions manual, Section 1.1.1

An 'Collate' field can be used for controlling the collation order for the R code files in a package when these are processed for package installation. The default is to collate according to the 'C' locale. If present, the collate specification must list all R code files in the package (taking possible OS-specific subdirectories into account, see "Package subdirectories") as a whitespace separated list of file paths relative to the 'R' subdirectory. Paths containing white space or quotes need to be quoted. An OS-specific collation field ('Collate.unix' or 'Collate.windows') will be used instead of 'Collate'.

And for example the XML package has

Collate: AAA.R DTD.R DTDClasses.R DTDRef.R SAXMethods.S XMLClasses.R
        applyDOM.R assignChild.R catalog.R createNode.R dynSupports.R
        error.R flatTree.R nodeAccessors.R parseDTD.R schema.S
        summary.R tangle.R toString.S tree.R version.R xmlErrorEnums.R
        xmlEventHandler.R xmlEventParse.R xmlHandler.R
        xmlInternalSource.R xmlOutputDOM.R xmlNodes.R xmlOutputBuffer.R
        xmlTree.R xmlTreeParse.R htmlParse.R hashTree.R zzz.R
        supports.R parser.R libxmlFeatures.R xmlString.R saveXML.R
        namespaces.R readHTMLTable.R reflection.R xmlToDataFrame.R
        bitList.R compare.R encoding.R fixNS.R xmlRoot.R serialize.R
        xmlMemoryMgmt.R keyValueDB.R solrDocs.R XMLRErrorInfo.R
        xincludes.R namespaceHandlers.R tangle1.R
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top