When writing my own R package, I can't seem to get other packages to import correctly
Asked Answered
Y

2

40

Alright, first attempt at writing an R package and I'm stuck. Here's how I create the package:

package.skeleton("pkg",code_files=some.filenames)
roxygenize("okg")

I'm using roxygen2 and have the following imports in my "pkg-package.R" file:

@import data.table zoo lubridate

From a terminal, I then run:

R CMD build pkg
R CMD check pkg
R CMD install pkg

During the check phase, I get the following warnings:

** preparing package for lazy loading
Warning: replacing previous import ‘hour’ when loading ‘lubridate’
Warning: replacing previous import ‘mday’ when loading ‘lubridate’
Warning: replacing previous import ‘month’ when loading ‘lubridate’
Warning: replacing previous import ‘wday’ when loading ‘lubridate’
Warning: replacing previous import ‘week’ when loading ‘lubridate'
Warning: replacing previous import ‘yday’ when loading ‘lubridate’
Warning: replacing previous import ‘year’ when loading ‘lubridate’
** help
* installing help indices
** building package indices ...
** testing if installed package can be loaded
Warning messages:
1: replacing previous import ‘hour’ when loading ‘lubridate’
2: replacing previous import ‘mday’ when loading ‘lubridate’
3: replacing previous import ‘month’ when loading ‘lubridate’
4: replacing previous import ‘wday’ when loading ‘lubridate’
5: replacing previous import ‘week’ when loading ‘lubridate’
6: replacing previous import ‘yday’ when loading ‘lubridate’
7: replacing previous import ‘year’ when loading ‘lubridate’

I'm really not sure what to make of those, but they seem like typical warnings from overwriting stuff in namespace. In any case, I am able to install the package, but here's what happens when I try to use it:

library(pkg)
Overriding + and - methods for POSIXt, Date and difftime
Warning messages:
1: replacing previous import ‘hour’ when loading ‘lubridate’
2: replacing previous import ‘mday’ when loading ‘lubridate’
3: replacing previous import ‘month’ when loading ‘lubridate’
4: replacing previous import ‘wday’ when loading ‘lubridate’
5: replacing previous import ‘week’ when loading ‘lubridate’
6: replacing previous import ‘yday’ when loading ‘lubridate’
7: replacing previous import ‘year’ when loading ‘lubridate’
d <- my.function(arg1, arg2)
Error in MATCH(x, x) : could not find function "MATCH"

Using traceback(), I found out that this is being generating during a call to merge.zoo(). So I tried loading zoo by hand during my R session and voila, then the function works correctly without the error message.

I have tried changing the ordering of the imports by hand in both the "pkg-package.R" file, as well as in NAMESPACE. Based on something I found elsewhere, I have not added any Imports or Depends to DESCRIPTION, however. Help?

Younger answered 25/4, 2012 at 23:29 Comment(1)
I'd recommend putting these packages into your Depends in the DESCRIPTION.Bonnibelle
C
43

The warnings are because data.table and lubridate both define a symbol hour, etc; see data.table::hour and lubridate::hour. You could avoid this by importing just the functions from lubridate / data.table that you want, rather than the whole package; a standard NAMESPACE file would contain

importFrom(lubridate, hour)

for instance. In roxygen2 you would use the tag:

@importFrom lubridate hour

The MATCH problem is probably because merge is dispatching incorrectly, probably because zoo should have in its name space S3method(merge, zoo) rather than export(merge.zoo), as described in Writing R Extensions, 1.6.2. The solution here is to contact the maintainer of zoo, packageDescription('zoo')$Maintainer (the maintainer is sufficiently versed in R that I feel like I've mis-diagnosed...).

Chery answered 26/4, 2012 at 0:54 Comment(4)
This is a useful answer, but just a follow up. So if I just import a single function like "hour," do I also need to import all of its internal or potentially private functions as well? Or will importFrom know to import any private functions or internal dependencies?Prosimian
@Prosimian functions look for the symbols they use first in their own environment, then in the environment in which the function was defined, so data.table functions that data.table::hour uses will be found automatically.Chery
What happens when you need to use import for multiple packages to have access to specific object classes and they have name conflicts? I am running into to this with spatstat and raster (area, rotate, shift). I cannot use importFrom because I am using numerous functions from both and need the package classes. Is there a way to mask out the three raster functions that conflict with spatstat? I should note that just using depends in DESCRIPTION is not working.Ventriloquist
You can also use import except: #51899720Agateware
G
1

As a temporary workaround for the MATCH error, I've had success listing the zoo package under the Depends: section of the package's DESCRIPTION file.

Greece answered 1/2, 2013 at 20:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.