[Rd] Problem with distributing data in package.

Simon Knapp sleepingwell at gmail.com
Sun Jul 21 07:14:34 CEST 2013


Hi Brian,

Thanks for the response. My original request contained the code that I
have placed in a file in the data directory, though this had been
removed in Barry's response. I have copied the original email below.
While I did mention "When I build the package" in that email, I did
not, however, state that I did this using the command:

R CMD build <package-name>

I apologise for being remiss. For my own education, is there anything
else I should have provided as part of a reproducible example?

I did not know there was an option --no-resave-data (I had not thought
to look at `R CMD build --help`, there is no mention of it in
R-exts.pdf and my ability to use Google is clearly lacking... though I
did try that), but it appears to be exactly what I need - Thankyou!

Kind Regards,
Simon Knapp




---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
start of original email
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
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)
})

---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
end of original email
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------





On Sun, Jul 21, 2013 at 3:25 AM, Prof Brian Ripley
<ripley at stats.ox.ac.uk> wrote:
> On 20/07/2013 12:01, Simon Knapp wrote:
>>
>> Hi Barry,
>>
>> Thanks for the response, your suggestion was going to be my 'work
>> around'... perhaps I took the second paragraph of section 1.1.5 of
>> R-exts.pdf the wrong way.
>>
>> I'd be interested in knowing why there is a difference between the
>> data in a source package (.rda files) and windows binary package (.R
>> files) if anyone can tell me.
>
>
> We have little idea what you did.  Apparently you feel you are exempted from
> the request in the posting guide for a reproducible example.
>
> At a wild guess, you did not understand the point of R CMD build
> --no-resave-data and did not want your data resaved as a .rda file (the
> documented default).  But there is no mention of running 'R CMD build' here.
>
>
>>
>> On Sat, Jul 20, 2013 at 4:58 PM, Barry Rowlingson
>> <b.rowlingson at lancaster.ac.uk> wrote:
>>>
>>> On Fri, Jul 19, 2013 at 10:33 AM, Simon Knapp <sleepingwell at gmail.com>
>>> wrote:
>>>>
>>>> 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().
>>>
>>>
>>>   Why not just put the function that generates the data file into the
>>> usual place (/R/ folder) and document it so that the user knows to run
>>> 'sampledata=makeSampleNCDF()' before doing things that need it?
>>>
>>>   Trying to put executable code into the data folder does seem a bit
>>> perverse!
>
>
> Actually, it seems to be *interpretable* R code in a .R file.
>
> The help file for data() says:
>
> Details:
>
>      Currently, four formats of data files are supported:
>
>        1. files ending ‘.R’ or ‘.r’ are ‘source()’d in, with the R
>           working directory changed temporarily to the directory
>           containing the respective file.  (‘data’ ensures that the
>           ‘utils’ package is attached, in case it had been run _via_
>           ‘utils::data’.)
> ...
>
> and (in so far as we can tell without the requested example) that was the
> 'format' intended to be used.
>
>>>
>>> Barry
>
>
>
> --
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list