I am trying to port some code to use the mmap package. I am having an issue with accessing data frame columns.
I would like to be able to access data columns with the $
and [[
operators. Here is the results I am getting.
> foo <- as.mmap(mtcars)
> foo[,'mpg'] # works
mpg
1 21.0
2 21.0
3 22.8
4 21.4
5 18.7
...
> foo$mpg #does not work
NULL
> foo[['mpg']] #also does not work
NULL
> foo[]$mpg #works
...
> foo[][['mpg']] #also works
...
Is there any way to make the $
and [[
operators work on the memory mapped object, as they would on a regular data frame?
Edit:
Per Joshua's suggestion I added a function for [[
`[[.mmap` <- function(x,...) `[[`(x[],...)
And for $
which does not seem particularly elegant, but seems to work.
> `$.mmap` <- function(x,...) {
if (...%in%c("storage.mode","bytes","extractFUN","filedesc")){
get(...,envir=x)
}else {
eval(call('$',x[],substitute(...)))
}}
Those functions don't work because they don't have a mmap
method.
> grep("mmap",methods("["),value=TRUE)
[1] "[.mmap"
> grep("mmap",methods("[["),value=TRUE)
character(0)
> grep("mmap",methods("$"),value=TRUE)
character(0)
Therefore, they dispatch to the default methods, which have no idea how to handle a mmap
object. You would need to write mmap
methods for [[
and $
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With