简体   繁体   中英

R ff ffbase ffwhich error in a function call?

Here is My code call ffwhich in a function:

library(ffbase)
rm(a,b)
test <- function(x) {
  a <- 1
  b <- 3
  ffwhich(x, x > a & x < b)
}
x <- ff(1:10)
test(x)
Error in eval(expr, envir, enclos) (from <text>#1) : object 'a' not found

traceback()
6: eval(expr, envir, enclos)
5: eval(e)
4: which(eval(e))
3: ffwhich.ff_vector(x, x > a & x < b)
2: ffwhich(x, x > a & x < b) at #4
1: test(x)

It may caused by lazy evaluation? The eval() can not find the a and b which is bounded in function test. How can I use ffwhich in a function?

  • R 2.15.2
  • ffbase 0.6-3
  • ff 2.2-10
  • OS opensuse 12.2 64 bit

Yes, it looks like an eval issue like Arun is indicating. I normally use the following when using ffwhich which is like an eval.

library(ffbase)
rm(a,b)
test <- function(x) {
  a <- 1
  b <- 3
  idx <- x > a & x < b
  idx <- ffwhich(idx, idx == TRUE)
  idx
}
x <- ff(1:10)
test(x)

I was having the same problem, and the answer given was not solving it, because we can not pass the argument "condition" to the function. I just got a way to do that. Here it is ::

require(ffdf) 
# the data :: 
x <- as.ffdf( data.frame(a = c(1:4,1),b=5:1))
x[,]

# Now the function below is working :: 

idx_ffdf <- function(data, condition){
 exp <-substitute( (condition) %in% TRUE) 
 # substitute will take the value of condition (non-evaluated). 
 # %in% TRUE makes the condition be false when there is NAs... 
 idx <- do.call(ffwhich, list(data, exp) ) # here is the trick: do.call !!! 
 return(idx)
}
# testing : 
idx <- idx_ffdf(x,a==1)
idx[] # gives the correct 1,5 ...

idx <- idx_ffdf(x,b>3)
idx[] # gives the correct 1,2 ... 

Hope this helps somebody !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM