[R] print (names and) values of parameters of function

William Dunlap wdunlap at tibco.com
Sun Oct 9 20:06:06 CEST 2016


There is no perfect way to do this because you can write functions
that depend on the order of evaluation of their arguments or that
must evaluate some code in the body of the function before evaluating
an argument (e.g., stats:::print.formula) or that don't evaluate them in
the usual sense.  However, print(ls.str(environment())), as the first line
of the function, can be useful:

> trace(nls, quote(print(ls.str(environment()))))
Tracing function "nls" in package "stats"
[1] "nls"
> d <- data.frame(x=log(1:10),y=1:10)
> fit <- nls(y~a*x^b, data=d, start=c(a=1,b=1))
Tracing nls(y ~ a * x^b, data = d, start = c(a = 1, b = 1)) on entry
algorithm :  chr [1:3] "default" "plinear" "port"
control : List of 5
 $ maxiter  : num 50
 $ tol      : num 1e-05
 $ minFactor: num 0.000977
 $ printEval: logi FALSE
 $ warnOnly : logi FALSE
data : 'data.frame':    10 obs. of  2 variables:
 $ x: num  0 0.693 1.099 1.386 1.609 ...
 $ y: int  1 2 3 4 5 6 7 8 9 10
formula : Class 'formula'  language y ~ a * x^b
lower :  num -Inf
model :  logi FALSE
na.action : <missing>
start :  Named num [1:2] 1 1
subset : <missing>
trace :  logi FALSE
upper :  num Inf
weights : <missing>


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Sat, Oct 8, 2016 at 7:21 AM, Christian Hoffmann <christian at echoffmann.ch>
wrote:

> I try to print  (names and) values of parameters of a function within that
> function, like:
>
> F <- function(x, y, z=4, ...) {
>
> print("x = ", x, " , y = ",  y, "... = " , ...)
>
> in a fashion that avoids the explicit mention of "x = ", x, " , y = ",  y,
> "... = " , ...
>
> Combinations of eval, substitute, formals, etc. should be able to do the
> job, but I didn't find help online.
>
> Anyone lend a helping hand?
> TIA  Christian
>
> --
> Christian W. Hoffmann
> CH - 8915 Hausen am Albis, Schweiz
> Rigiblickstrasse 15 b, Tel.+41-44-7640853
> mailto: christian at echoffmann.ch
> home: www.echoffmann.ch
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list