rticulate / import

An Import Mechanism For R
https://import.rticulate.org
Other
222 stars 14 forks source link

issue while importing functions that use data.table syntaxis #75

Closed phisanti closed 1 year ago

phisanti commented 1 year ago

I am a huge fan of your package, however, I have just noticed it seems to be a bit problematic when dealing with some special objects of the data.table package, particularly, the := function. See this example:

file_1.R

foo1 <- function(d) {
d[, newcol := rnorm(.N)]
}

file_2.R

foo2 <- function(x) {
y <- x+ 1

return(y)
}

file 3

import::from("file.R", foo1)
import::from("file.R", foo2)

For some reason, after importing the functions that contain the function :=, it is impossible to load any other further function. In this particular case, if one loads first foo2 and then foo1, it would be possible to have both functions, yet again, it won't be possible to add any other function via impot::

torfason commented 1 year ago

Thanks for the issue, I tried adding this to the test harness, and I am not reproducing this. A reprex::reprex(wd=".") (after renaming the modules) looks like this:

set.seed(42)
import::from("module_issue_colonequals_1.R", foo1)
dt <- data.table::as.data.table(cars[1:3,])
foo1(dt)
print(dt)
#>    speed dist     newcol
#> 1:     4    2  1.3709584
#> 2:     4   10 -0.5646982
#> 3:     7    4  0.3631284

import::from("module_issue_colonequals_2.R", foo2)
foo2(dt$newcol)
#> [1] 2.3709584 0.4353018 1.3631284

Created on 2023-03-13 with reprex v2.0.2

So it looks like something else must be going on for you.

phisanti commented 1 year ago

Yes, that is correct, but as soon as I start importing more functions, the import::from() stops working. Have you tried including two or three more function imports in the example?

torfason commented 1 year ago

Yupp, tried running the example and then importing some additional functions, but I'm not seeing any issues. Not sure what is going on there.

If you can build on the reprex that I sent (copy code that reproduces the issue reliably, and then run reprex::reprex(wd=".") then I can take a closer look. But I suspect this might have to do with some additional complexities in your setup.

torfason commented 1 year ago

I'll close this issue now since I am not able to reproduce this. If a reprex can be prepared to reliably reproduce this, we can open it again.