2

I am expanding the doSvUnit.R script so that it will include the examples in its report.

one initial step in the task is associating a test to a function, where the name of the function is in a variable. (my real test function does not only checkTrue, it invokes the examples for the function named funcName. I'm here only showing the concept.)

> funcName <- "double.threshold"
> test(get(funcName)) <- function() checkTrue(TRUE)
Error in test(get(funcName)) <- function() checkTrue(TRUE) : 
  could not find function "get<-"
>

or even

> test(get("double.threshold")) <- function() checkTrue(TRUE)
Error in test(get("double.threshold")) <- function() checkTrue(TRUE) : 
  target of assignment expands to non-language object
>

I don't understand the reason behind either error message and I don't understand why I get two different error messages for what I see as the same thing.

thanks to the second error message, I found a workaround by storing the function in an object, but I don't understand why it should be necessary and I am not so sure this is specific to svUnit.

> f <- get(funcName)
> test(f) <- function() checkTrue(TRUE)
> 
mariotomo
  • 9,438
  • 8
  • 47
  • 66
  • Check [Section 3.4.4](http://stat.ethz.ch/R-manual/R-devel/doc/manual/R-lang.html#Subset-assignment) of the R Language Reference to see why you get the error. Depending on how `test` is implemented, your workaround may or may not work, though... – Nick Sabbe Sep 07 '11 at 08:31
  • can you make it a bit more explicit? why are you pointing me to subset assignment? – mariotomo Sep 07 '11 at 08:46
  • 1
    It's not subset assignment per se, but this is the only part I know of that explains how assignment works. Check the `names` example in that section for something somewhat similar to your situation. – Nick Sabbe Sep 07 '11 at 09:05
  • wow! this explains that puzzling "get<-"! – mariotomo Sep 07 '11 at 14:52
  • Nick, if you post your comment into an answer, I can accept it. – mariotomo Sep 08 '11 at 08:05

2 Answers2

1

Section3.4.4 of the R Language Reference explains how (sub)assignment works.

Particularly the example on names shows how R interprets statements with nested function calls (on the left hand side) and assignment.

Basically (an oversimplification, but precise enough for most circumstances), it shows that it needs an assignment version for each of the functions on the left hand side.

Note that these same issues bug many people in different situations: see this great answer by Gavin Simpson to one of my own questions, that opened my eyes to what goes on behind the scenes.

Community
  • 1
  • 1
Nick Sabbe
  • 11,684
  • 1
  • 43
  • 57
0

I also doubt that it is specific to svUnit. It is telling you that the parser goes looking for a get<- function and fails to find one.

> ttt <- function(x) x+2
> fname <- "ttt"
> body(ttt ) <- quote(x+4)
> ttt
function (x) 
x + 4

That operation succeeds but this one doesn't:

> body(get(fname) ) <- quote(x+4)
Error in body(get(fname)) <- quote(x + 4) : 
  could not find function "get<-"
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • and the comment by [Nick Sabbe](http://stackoverflow.com/users/709529) points at the explanation for this curious behavior. – mariotomo Sep 08 '11 at 08:04
  • I can understand how the first three sentences of Nick's answer apply to this question, but have not yet seen how the strange behavior of '$<-' is relevant here. – IRTFM Sep 08 '11 at 12:29
  • I think it's not about "behaviour" but "definition". the clue is that we are doing a nested assignment. so you write `body(get(fname)) <- ...` and we focus on the `get` and using that value for doing a `body<-`, but `R` thinks differently and tries to do both first a `get<-` and then a `body<-`. – mariotomo Sep 08 '11 at 12:51