[R] suprising behaviour of tryCatch()

Berwin A Turlach berw|n@tur|@ch @end|ng |rom gm@||@com
Thu May 18 16:55:42 CEST 2023


G'day Federico,

On Wed, 17 May 2023 10:42:17 +0000
"Calboli Federico (LUKE)" <federico.calboli using luke.fi> wrote:

> I_d be obliged if someone can explain why tryCatch assigns items with _<-_ and not _=_.

It is not just tryCatch but any function.  In function calls the "=" is
used to assign actual arguments to formal arguments, not for
assignments.  

That is why "plot(fm <- lm(eruptions ~ waiting, faithful)" works but
"plot(fm = lm(eruptions ~ waiting, faithful)" does not.

The only universal assignment operator, AFAIK, is "<-".  Well, and "->".
Long time ago "_" used to be one too, but that is long ago.  

The usual advice was that if you do not know when "=" does not work as
assignment operator then always use "<-" for assignments. :)

The final thing you have to take care of that in function calls you
have to be sure that the argument is actually evaluated (R has lazy
evaluation) to ensure that the assignment is actually executed.

Cheers,
	
	Berwin



More information about the R-help mailing list