2

As per data.table ver 1.8.8 %between% is defined as follow:

> `%between%`
function (x, y) 
between(x, y[1], y[2], incbounds = TRUE)
<bytecode: 0x0000000050912810>
<environment: namespace:data.table>

I thought that, with this tiny change, this function would be vectorised,

function (x, y) 
    between(x, y[[1]], y[[2]], incbounds = TRUE)

like between

s <- c(2, 5)
d <- c(7, 9)
> between(3, s, d)
[1]  TRUE FALSE

The idea came from having a list with two vectors, which suggested me this possible usage:

`between2%` <- function(x, lst) between(x, lst[[1]], lst[[2]], incbounds = TRUE)

> 3%between%c(s,d)
[1] TRUE
> 3%between2%list(s,d)
[1]  TRUE FALSE

My question is: if I replaced %between% would any functionality in data.table package be affected? I think it shouldn't, [[ should work with atomic vector as [ does. Am I correct? thanks

> 3%between2%c(1,5)
[1] TRUE
Michele
  • 8,563
  • 6
  • 45
  • 72
  • The answer is NO. You should be able to overwrite any function in any package and this should not affect the package. – agstudy Jul 01 '13 at 11:16
  • @agstudy ok thanks, just wonder about any dependencies ( in terms of other functions calling this one) and about `[[` vs `[` with atomic vectors. thanks again – Michele Jul 01 '13 at 11:20

1 Answers1

3

I thought this was an interesting question because I wondered how one might look, in general, for uses of a function by other functions. As far as I know, there's no way to directly do this (perhaps someone can correct me?). But, I put together a little code that looks for function names in text representations of other functions. For %between%, here's the following:

library(data.table)
objs <- objects("package:data.table")
z <- textConnection("funs", open="w")
dump(list=objs, file=z)
close(z)

# find all mentions of `%between%` in `data.table` functions
funs[grep("%between%",funs)] ## only mentions are when %between% is defined

# find all mentions of all `data.table` functions in `data.table` functions
locations <- lapply(objs, function(x) grep(x,funs))
names(locations) <- objs

UPDATE: After doing some more searching, this question/answer also seem to provide some more information on how to detect dependencies programmatically using foodweb from library(mvbutils).

Community
  • 1
  • 1
Thomas
  • 43,637
  • 12
  • 109
  • 140
  • thanks a lot! very helpful. I also found a strange behavior. My R session crashes as soon as `R` gets this line `dump(list=objs, file=z)`. In my workspace there are several tens of `data.frame` and `data.table`, the .RData is almost 1 GB and occupies 10GB in the ram. But running your code in an empty workspace this does not happen. Anyway, thanks again – Michele Jul 01 '13 at 14:22
  • That's interesting...we need someone far more expert than I to figure that one out. – Thomas Jul 01 '13 at 14:30
  • I think it would be difficult either. I can't reproduce this scenario, I would have raised a question :-). I tried with some dummy tables, but nothing. Btw, I just saw your `rite` package. very good. Could you tell me your ideas in regards of [this](http://support.rstudio.org/help/discussions/questions/4258-code-completion-inside-functions) – Michele Jul 01 '13 at 14:38
  • I've thought about incorporating something like that in `rite`. But first I want to build a better interface to the existing syntax highlighting, because I'd like people to be able to dynamically control it. Then doing the same for new objects and variables within dataframes would be relatively simple. I don't know how to do in rstudio, though. – Thomas Jul 01 '13 at 14:48
  • Of course I meant in your package. If you do that I'll switch over! – Michele Jul 01 '13 at 14:57
  • 1
    Hi, why do you choose `objects("package:data.table")`? `objects(asNamespace("data.table"))` has much more names :-) – Ferdinand.kraft Aug 19 '13 at 23:55
  • @Ferdinand.kraft That's an alternative, I suppose...but if I understand it what I have calls only things exported by the package, which is theoretically all that any other package should use. `asNamespace` isn't well documented, though, so I can't really evaluate the trade-offs. – Thomas Aug 20 '13 at 06:02