[Rd] Problem with distributing data in package.

Simon Knapp sleepingwell at gmail.com
Fri Jul 19 11:33:20 CEST 2013


Hi List,

I am building a package for a client to help them create and perform
analyses against netcdf files which contain 'a temporal stack' of
grids.

For my examples and test cases, I create an example dataset in code
(as this is a lot more space efficient than providing raw data). The
code creates a netcdf file in tempdir() and an object of class 'ncdf'
in the global namespace. I have placed the code in a .R file in the
data directory of my package and 'load' it with a call to data().

My problem
-----------------
When I build the package, the code gets executed during the build
process, the netcdf file gets created in tempdir() and the 'ncdf'
object gets saved to a .rda file in the package... which is not very
useful, since when I call data() the object gets loaded but the netcdf
file is not present (further, the 'ncdf' object includes the path to
the netcdf file - in my tempdir()).

My question
------------------
Is there a way to circumvent the creation of the .rda file and build
the package such that it contains the .R files instead (I note that
this is what appears to happen with a binary build for windows)? If
not, then is there another 'R-ish' way to achieve a similar result?

Just in case it is useful, I have included the code I use to build the
netcdf file and 'ncdf' object below.

Thanks in advance,
Simon Knapp


#--------------------------R code starts here--------------------------
# create a netcdf file holding a 'temporal stack'
combined.ncdf <- with(new.env(), {
    require(ncdf)
    nLat <- 7
    nLon <- 8
    nTime <- 9
    missing.val <- -999
    filename <- file.path(tempdir(), 'combined_d6fg4s64s6g4l.nc')
    lat.dim <- dim.def.ncdf('lat', 'degrees', 1:nLat)
    lon.dim <- dim.def.ncdf('lon', 'degrees', 1:nLon)
    time.dim <- dim.def.ncdf("time", "days since 1970-01-01",
as.integer(1:nTime), unlim=T)
    var.def <- var.def.ncdf('Data', 'mm', list(lon.dim, lat.dim,
time.dim), missing.val, prec='single')
    nc <- create.ncdf(filename, var.def)
    for(i in 1:nLon)
        for(j in 1:nLat)
            for(k in 1:nTime)
                put.var.ncdf(nc, var.def, i + j*10 + k*100, c(i, j,
k), rep(1, 3))
    close.ncdf(nc) # seems to be required.
    open.ncdf(filename)
})



More information about the R-devel mailing list