2

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 : object Cchmatchwrapper 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)

Community
  • 1
  • 1
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
  • You might find this article interesting: http://blog.obeautifulcode.com/R/How-R-Searches-And-Finds-Stuff/ – MrFlick Feb 19 '16 at 01:08
  • Why are you not just creating your own package and using `library()` on that? – MrFlick Feb 19 '16 at 01:10
  • @MrFlick indeed I've seen that article before, but I always learn more by re-reading it. Unfortunately it completely girds the issue of where R goes to find `.Call` functions... as to why I'm doing this -- see [this](http://stackoverflow.com/questions/33179156/installing-a-package-offline-from-github?lq=1) question. I can't figure out how to get a package from GitHub to install on my offline machine... trying to work around that by "manually importing" the package. – MichaelChirico Feb 19 '16 at 16:55

0 Answers0