[R] How do I provide path character string to extdata content?

Duncan Murdoch murdoch.duncan at gmail.com
Mon Feb 16 14:40:21 CET 2015


On 16/02/2015 6:37 AM, Ulrike Grömping wrote:
> Dear helpeRs,
> 
> I have some png files in the inst/extdata directory of a package (e.g.,
> man.png), and I want to provide character strings containing the paths to
> these files; of course, these path strings have to depend on the individual
> installation. So far, I have written a function that - if called - writes
> the correct strings to the global environment, but that is not CRAN
> compatible.

The system.file() function does this.  For example, if your package
named foo contains inst/fig/man.png, you would use
system.file("fig/man.png", package="foo").

> 
> Ideally, I want the package namespace to export these text strings. I have
> played with .onLoad without luck. I do not have a good understanding of
> which hook is for which purpose. How can I go about this?

Rather than exporting the strings, it's easier to export a function that
produces the strings, e.g.

fig <- function(name) system.file(file.path("fig", name), package="foo")

to be called as

fig("man.png")

Duncan Murdoch



More information about the R-help mailing list