inspect() passes named args incorrectly
Brought to you by:
burgerm
The code inspector doesn't correctly pass a named argument to the inspected function. Rather, it seems to drop the name, and effectively pass it as a positional argument.
Here's a sample session:
> myfun <- function(a, b=98, c=99){
+ cat("a=", a, ", b=", b, ", c=", c, "\n")
+ }
> myfun(1, c=2)
a= 1 , b= 98 , c= 2
## So far so good. Now let's inspect myfun:
> library(RUnit)
RUnit 0.4.17 loaded.
> track <- tracker()
> tracker$init()
Error: attempt to apply non-function
No suitable frames for recover()
> track$init()
> inspect(myfun(1, c=2), track=track)
a= 1 , b= 2 , c= 99
NULL
>
The result looks like I called myfun(1, 2). I expected the same result as from the stand-alone call above.
Logged In: YES
user_id=660859
Originator: YES
The problem is that as.character(), drops the names of the parameters in the call early in inspect().
Below is a patch that fixes my symptom, by maintaining substitute(expr) in mode call.
I hope somebody who understands inspector better can verify whether this is an improvement, or if I introduce other bugs.
It fails on similar calls as before, e.g., inspect(8,track=track).
--- inspector.r 2007-04-12 04:29:06.000000000 -0400
+++ my_inspector.r 2007-11-21 22:42:41.000000000 -0500
@@ -446,7 +446,7 @@
##
##@codestatus : testing
- ## getting the call and his parameter
+ ## getting the call and its parameters, dropping their names
fCall <- as.character(substitute(expr));
## get the original call
@@ -528,22 +528,9 @@
eval(parse(text=c("testFunc <- ",newFunc$modFunc)),envir=sys.frame())
## create function call
- newFunCall <- paste("testFunc(",paste(fCall[-1], collapse=","), ")",sep="")
-
- parsedFunc <- try(parse(text=newFunCall))
-
- ## check for an error
- if(!inherits(parsedFunc,"try-error"))
- {
- ## call the new function
- res <- eval(parsedFunc,envir=parent.frame())
- }
- else
- {
- ## no parsing possible
- ## simple call without tracking
- res <- expr
- }
+ newFunCall <- substitute(expr) # retain mode call
+ newFunCall[[1]] <- as.name("testFunc")
+ res <- eval(newFunCall,envir=parent.frame())
## do here some error checking