1

When I try to use shift in data.table, when package rasterVis is already loaded, I get this error:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘shift’ for signature ‘"integer"’

For example, this works flawlessly:

library(data.table)
# library(rasterVis)
DT <- data.table(a = 1:50)
DT[, lagged.a := shift(a, 12, type = 'lag')]

but this not, even if I try to unload the package:

library(data.table)
library(rasterVis)
DT <- data.table(a = 1:50)
DT[, lagged.a := shift(a, 12, type = 'lag')] # ERROR
detach('package:rasterVis', unload = TRUE) # or
unloadNamespace(rasterVis)
library(data.table) # I reload data.table, but still nothing
DT[, lagged.a := shift(a, 12, type = 'lag')] # still ERROR

I can obviously run the shift related lines to a new R environment, but I am curious if I can fix it without doing so.

Konstantinos
  • 4,096
  • 3
  • 19
  • 28
  • 4
    Um, start a new session and don't load whatever you don't want loaded...? Or refer to `data.table::shift(a, 12, type = 'lag')`? – Frank Mar 15 '16 at 19:03
  • Does it do the same thing if you use `detach("package:rasterVis", unload=TRUE)` instead of `unloadNamespace(rasterVis)`? ...but Frank's right, the easiest way is to just use `::` notation to specify which `shift` you mean. – alistaire Mar 15 '16 at 19:13
  • Frank this is totally easy to implement. many thanks. @alistaire, yeap it does the same and yes Frank's right! Thank you guys! – Konstantinos Mar 15 '16 at 19:30
  • 2
    Another trick which will work, but is not wholly recommended: load `data.table` after `rasterVis`, so `data.table::shift` will mask `rasterVis::shift` instead of vice versa. It's really probably better to specify with `::`, though, unless you know you're not going to use one or the other. – alistaire Mar 15 '16 at 19:35

0 Answers0