Search code examples
rwindowsdata.tabletestthat

Use data.table in functions/packages (With roxygen)


I am quite new to R but it seems, this question is closely related to the following post 1, 2, 3 and a bit different topic 4. Unfortunately, I have not enough reputation to comment right there. My problem is that after going through all the suggestions there, the code still does not work:

  1. I included "Depends" in the description file
  2. I tried the second method including a change of NAMESPACE (Not reproducable)
  3. I created a example package here containing a very small part of the code which showed a bit different error ("J" not found in routes[J(lat1, lng1, lat2, lng2), .I, roll = "nearest", by = .EACHI] instead of 'lat1' not found in routes[order(lat1, lng1, lat2, lng2, time)])
  4. I tested all scripts using the console and R-scripts. There, the code ran without problems.

Thank you very much for your support!

Edit: @Roland

  1. You are right. Roxygen overwrites the namespace. You have to include #' @import data.table to the function. Do you understand, why only inserting Depends: data.table in the DESCRIPTION file does not work? This might be a useful hint in the documentation or did I miss it?
  2. It was missleading that changing to routes <- routes[order("lat1", "lng1", "lat2", "lng2", "time")] helped at least a bit as this line was suddenly no problem any more. Is it correct, that in this case data.frame order is used? I will see how far I get now. I will let you know the final result...

Solution

  • Answering your questions (after edit).

    1. Quoting R exts manual:

    Almost always packages mentioned in ‘Depends’ should also be imported from in the NAMESPACE file: this ensures that any needed parts of those packages are available when some other package imports the current package.

    So you still should have import in NAMESPACE despite the fact if you depends or import data.table.

    1. The order call doesn't seems to be what you expect, try the following:

    order("lat1", "lng1", "lat2", "lng2", "time")
    
    library(data.table)
    data.table(a=2:1,b=1:2)[order("a","b")]
    

    In case of issues I recommend to start debugging by writing unit test for your expected results. The most basic way to put unit tests in package is just plain R script in tests directory having stopifnot(...) call. Be aware you need to library/require your package at the start of the script.