ices-taf / doc

Community documentation for the TAF project
http://taf.ices.dk
11 stars 4 forks source link

Added argument `local` to sourceTAF() so objects are created outside of the user workspace #131

Closed arni-magnusson closed 6 years ago

colinpmillar commented 6 years ago

@arnima-github I found a quirk in this when useing data.table commands. I will repl

> sourceTAF("model.R")
[18:53:07] Running model.R ...
Error in which(Score == max(Score)) : object 'Score' not found
Failed
> source("model.R")
> env <- new.env()
> source("model.R", local = env)
> (function(){ env <- new.env(); source("model.R", local = env)})()

All the above OK, but souceTAF not…. Very odd!

If it helps, the line of code is here: dt.max <- dt[,.SD[which(Score == max(Score))],by = CSquare]

I will try and get a small reproducible example

colinpmillar commented 6 years ago

@arnima-github here is a small example:

tmpffile <- tempfile(fileext = ".R")
cat("library(data.table)

n <- 1000
df <- data.frame(catch_this = 1:n,
                  y = rnorm(n),
                   cat = sample(letters[1:3], n, replace = TRUE))
dt <- data.table(df)

dt_sd <- dt[,.SD[which(catch_this > 1)], by = cat]
", file = tmpffile)

icesTAF::sourceTAF(tmpffile)
# [14:24:59] Running C:\Users\colin\AppData\Local\Temp\RtmpeQlU0D\file3ecc75483269.R ...
# Error in which(catch_this > 1) : object 'catch_this' not found
# Failed
icesTAF::sourceTAF(tmpffile, local = FALSE)
# [14:26:22] Running C:\Users\colin\AppData\Local\Temp\RtmpeQlU0D\file3ecc75483269.R ...
# Done
source(tmpffile)
arni-magnusson commented 6 years ago

The local argument proved to be problematic, so went back to rm argument instead.