Inst and Extdata Folders in R Packaging

inst and extdata folders in R Packaging

You were both very close and essentially had this. A formal reference from 'Writing R Extensions' is:

1.1.3 Package subdirectories

[...]

The contents of the inst subdirectory will be copied recursively
to the installation directory. Subdirectories of inst should not
interfere with those used by R (currently, R, data, demo,
exec, libs, man, help, html and Meta, and earlier versions
used latex, R-ex). The copying of the inst happens after src
is built so its Makefile can create files to be installed. Prior to
R 2.12.2, the files were installed on POSIX platforms with the permissions in the package sources, so care should be taken to ensure
these are not too restrictive: R CMD build will make suitable
adjustments. To exclude files from being installed, one can specify a
list of exclude patterns in file .Rinstignore in the top-level
source directory. These patterns should be Perl-like regular
expressions (see the help for regexp in R for the precise details),
one per line, to be matched(10) against the file and directory paths,
e.g. doc/.*[.]png$ will exclude all PNG files in inst/doc based on
the (lower-case) extension.

Documenting external data in R package for the inst/extdata folder?

Here is the reproducible (on linux) example of solution I provided in comments.

Code below will create minimal package

  • defining dummy DESCRIPTION file
  • example ext/data/data1.csv csv data file
  • example man/data1.Rd R documentation file

Then it will build and install package.

mkdir -p my.pkg
cat > my.pkg/DESCRIPTION <<EOL
Package: my.pkg
Type: Package
Title: My pkg
Version: 1.0.0
Description: Example my pkg.
License: GPL-3
EOL
mkdir -p my.pkg/inst/extdata
cat > my.pkg/inst/extdata/data1.sv <<EOL
a,b,c
1,a,2.5
2,b,5.5
EOL
mkdir my.pkg/man
cat > my.pkg/man/data1.Rd <<EOL
\name{data1}
\alias{data1}
\alias{data5}
\title{
my data
}
\description{
desc of data.
}
EOL
R CMD build my.pkg
R CMD INSTALL my.pkg_1.0.0.tar.gz

Now see that manual can be found

Rscript -e 'library(my.pkg); ?data1'
Rscript -e 'library(my.pkg); ?data5'

As you can see we can refer to documentation of extdata using any name defined in alias inside Rd file.

R: Meaning of extdata ?

This is a convention, not a formally defined term. (However, it's a convention defined by the package authors and coded in the package structure; it's not something you can change unless you mess around with the package structure yourself.) "extdata" is presumably short for "external data".

However, this doesn't mean that you need to use "extdata" when you are structuring your own code; you only need it when finding the files that are included by the package. cron_rscript("~/my_cron_jobs/foo.R") should work fine (provided you actually have something there, and provided that the ~ == home directory shortcut works across OS, which I think it does).

system.file() takes a package argument, but otherwise strings its arguments together into a file path; i.e. system.file(package = "cronR", "extdata", "helloworld.R") means

  • look in the system folder that R has set up for the cronR package (in my case that is /usr/local/lib/R/site-library/cronR, but the precise location will vary by OS and configuration)
  • within that folder look in the extdata folder
  • within that folder look for helloworld.R

So this command will refer in my case to /usr/local/lib/R/site-library/cronR/extdata/helloworld.R.

Since "/" works as a path separator (at least when used from within R) for all current operating systems, you would get the same results from system.file(package="cronR", "extdata/helloworld.R")

When should data go in /data, and when should it go in /inst/extdata?

The data directory supplies data for the data() function and is expected to follow certain customs in terms of file formats and extensions.

The inst/extdata directory becomes extdata/ when installed and is more of a wild west and you can do whatever you want and it is expected that you write your own accessors.

It may be useful to look at empirics. On my machine, among around 240-some installed packages, a full 77 (or not quite a third) have data/, but only 4 (including one of mine) have extdata..

Using inst/extdata with vignette during package checking R 2.14.0

Have you tried using system.file instead of hardcoded relative paths?

EC1 <- dot2HPD(file = system.file("inst", "extdata", "E_coli", "ecoli.dot", package = "your_package+name"))
node.inst <- system.file("inst", "extdata", "E_coli", "NodeInst.csv", package = "your_package_name")

How to properly organise vignettes and inst folders when creating new R package

This is a bit late but, the error went as soon as I stopped saving my built vignettes within the R package.

Instead I only have the rmd file and associated plots in the /vignette folder and have an /inst folder which does not contain any vignette associated material.



Related Topics



Leave a reply



Submit