This is closely related to my struggles with installing from GitHub to an offline Window machine.
I've figured out how to get all the R code from a package into my namespace to behave as if it's been loaded with library
. The remaining barrier to manually attaching a package is to make sure my functions have access to any primitive calls (C/C++/Fortran/etc code).
Where I'm stuck is that I don't know how exactly R knows how to find a given source function. Let's take my pet data.table
as an example. The %chin%
function appeals directly to C
code, so I think it's a good representative example:
`%chin%`
function (x, table)
{
.Call(Cchmatchwrapper, x, table, NA_integer_, TRUE)
}
Suppose I hadn't attached data.table
with library
yet. My search
path from a fresh start is:
search()
# [1] ".GlobalEnv" "tools:rstudio" "package:stats"
# [4] "package:graphics" "package:grDevices" "package:utils"
# [7] "package:datasets" "package:methods" "Autoloads"
# [10] "package:base"
I can spoof %chin%
into a data.table
name space like so:
attach(environment(), name = "package:data.table")
sys.source("~/data.table/R/data.table.R",
envir = as.environment("package:data.table"))
But %chin%
won't work:
u = as.character(as.hexmode(1:10))
y = sample(u,5,replace=TRUE)
x = sample(u)
x %chin% y
Error in
x %chin% y
: objectCchmatchwrapper
not found
Is there any way to do this without needing library
?
Supposing I can, is there any other thing I would be missing to get my "spoofed" package to behave exactly like a typically attached package?
I was inspired by @MrFlick's suggested reading in the comments to try one more thing -- setting the "path"
attr
of my spoof environment properly, in the hopes that .Call
would know where to look as a result:
attach(environment(), name = "package:data.table")
sys.source("~/data.table/R/data.table.R",
envir = x <- as.environment("package:data.table"))
attr(x, "path") <- "/home/michael/data.table/"
(but it didn't work)