Murat Tasan
2015-Dec-04 01:28 UTC
[R] system.file(...) self-referencing the containing package
In a package I'm writing, I'm placing all SQL code here: /inst/sql/ And so when referring to these blocks of code from the package's R code, I do something like so: system.file("sql", "my_example_file.sql", package = "ThisPackage", mustWork = TRUE) But, referring to the package itself with the string "ThisPackage" is annoying and somewhat brittle... if the package were ever to change names (e.g. during development), I'll have to replace all such calls with the new package name. And generally referring to the package containing the code itself by name seems inelegant. Anyone know of a safe way to reference additional files in a package, from within that package's code, that doesn't require specifying the name of the package as a string? Cheers, -Murat
William Dunlap
2015-Dec-04 01:42 UTC
[R] system.file(...) self-referencing the containing package
Every package has in it, after it is installed, a character object called ".packageName" containing its name. It is not exported from the package. Functions in your package can refer to it as just .packageName. Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, Dec 3, 2015 at 5:28 PM, Murat Tasan <mmuurr at gmail.com> wrote:> In a package I'm writing, I'm placing all SQL code here: > /inst/sql/ > > And so when referring to these blocks of code from the package's R > code, I do something like so: > > system.file("sql", "my_example_file.sql", package = "ThisPackage", > mustWork = TRUE) > > But, referring to the package itself with the string "ThisPackage" is > annoying and somewhat brittle... if the package were ever to change > names (e.g. during development), I'll have to replace all such calls > with the new package name. > And generally referring to the package containing the code itself by > name seems inelegant. > > Anyone know of a safe way to reference additional files in a package, > from within that package's code, that doesn't require specifying the > name of the package as a string? > > Cheers, > > -Murat > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Murat Tasan
2015-Dec-04 16:28 UTC
[R] system.file(...) self-referencing the containing package
Perfect, thanks! (Any idea if/where this is documented? I checked through the "Writing R Extensions" doc and couldn't find any mention of it.) Thanks much again, -Murat On Thu, Dec 3, 2015 at 6:42 PM, William Dunlap <wdunlap at tibco.com> wrote:> Every package has in it, after it is installed, a character object > called ".packageName" containing its name. It is not exported from > the package. Functions in your package can refer to it as just > .packageName. > Bill Dunlap > TIBCO Software > wdunlap tibco.com > > > On Thu, Dec 3, 2015 at 5:28 PM, Murat Tasan <mmuurr at gmail.com> wrote: >> In a package I'm writing, I'm placing all SQL code here: >> /inst/sql/ >> >> And so when referring to these blocks of code from the package's R >> code, I do something like so: >> >> system.file("sql", "my_example_file.sql", package = "ThisPackage", >> mustWork = TRUE) >> >> But, referring to the package itself with the string "ThisPackage" is >> annoying and somewhat brittle... if the package were ever to change >> names (e.g. during development), I'll have to replace all such calls >> with the new package name. >> And generally referring to the package containing the code itself by >> name seems inelegant. >> >> Anyone know of a safe way to reference additional files in a package, >> from within that package's code, that doesn't require specifying the >> name of the package as a string? >> >> Cheers, >> >> -Murat >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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.