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)
>
Section3.4.4 of the R Language Reference explains how (sub)assignment works.
Particularly the example on
namesshows 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.