0

I have a function that takes as an argument a logical statement. For example,

cpdist(fitted.model, evidence = (A == "a") & (B == "b"))

I want this function to work with various "evidence" arguments, so I wrote

ev <- paste("(", evidence.variables, "=='",  sapply(evidence.values, as.character), ")",
                  sep = "", collapse = " & ")
cpdist(fitted.model, evidence = eval(parse(text = ev)))

This works when I call it from global environment or from within a for loop. However, I want to wrap this code in a larger function (which itself is called from within a loop). When I do that, I get the error: "Error in parse(text = ev) : object 'ev' not found".

eval() has "envir" and "enclos" arguments. I have tried setting those arguments to different environments, but it hasn't worked. Environments are still a hazy subject to me. Can anyone offer any suggestions?

Count Zero
  • 630
  • 1
  • 6
  • 15
  • A suggestion.... don't use `eval(parse(...` see `library(fortunes);fortune(181)`. As for this specific question. Is `cpdist` your function? Where is the reproducible example? – mnel Nov 25 '13 at 02:13
  • A further suggestion. If ou are using a funciton that is not in one of the packages loaded by default then NAME THE PACKAGE. – IRTFM Nov 25 '13 at 03:56
  • For an example of how to do this sort of thing "correctly" see the code in `subset.data.frame`, in particular the handling of the `subset` argument. – joran Nov 25 '13 at 04:45

0 Answers0