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?

Old Pro
  • 24,624
  • 7
  • 58
  • 106
ugh
  • 769
  • 2
  • 6
  • 6

2 Answers2

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...).

David Robinson
  • 77,383
  • 16
  • 167
  • 187
Martin Morgan
  • 45,935
  • 7
  • 84
  • 112
  • 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? – krishnab Mar 27 '14 at 04:56
  • 2
    @krishnab 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. – Martin Morgan Mar 27 '14 at 05:06
  • 2
    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. – Jeffrey Evans Jan 27 '15 at 01:56
  • 2
    You can also use import except: https://stackoverflow.com/questions/51899220/import-all-the-functions-of-a-package-except-one-when-building-a-package – Mikko May 14 '20 at 20:57
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.

user338714
  • 2,315
  • 5
  • 27
  • 36