Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 518
ugh Avatar asked Apr 25 '12 23:04

ugh


People also ask

What is dependency in R?

A dependency is a code that your package needs to run. Dependencies are managed by two files. The DESCRIPTION manages dependencies at the package level; i.e. what packages needs to be installed for your package to work. R has a rich set of ways to describe different types of dependencies.

What is namespace R?

Namespaces allow the package writer to hide functions and data that are meant only for internal use, Namespaces prevent functions from breaking when a user (or other package writers) picks a name that clashes with one in the package, and. Namespaces provide a way to refer to an object within a particular package.


2 Answers

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

like image 89
Martin Morgan Avatar answered Oct 16 '22 09:10

Martin Morgan


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.

like image 37
user338714 Avatar answered Oct 16 '22 10:10

user338714