[R] suprising behaviour of tryCatch()

Greg Snow 538280 @end|ng |rom gm@||@com
Thu May 18 16:57:55 CEST 2023


Because `<-` and `=` do different things (and I am one of the old
fossils that wish they had not been confounded).

`fun(x <- expr)`  Assigns the value of `expr` to the variable `x` in
the frame/environment that `fun` is called from.
`fun(x = expr)` Assigns the value of `expr`to the variable `x` in the
frame/environment created for and used by `fun` subject to the rules
of argument matching.

These are very different (see documentation/tutorials on variable
scoping if you do not already understand how different these are).

As a simple analogy, think of a function like the refrigerator in your house.

refrigerator( topshelf = bottle )  will put the bottle on the top
shelf inside of the refrigerator
refrigerator( topshelf <- bottle ) will put the bottle on the top
shelf in the same room that the refrigerator is in (but not in the
fridge itself)


When you run the code with `<-` it, then the ith element of the global
variable `sexsnp` is assigned the p-value.  When you run the version
with `=` then R tries to find an argument to `tryCatch` that matches
(possibly partially) `sexsnp[i]` and gives the error because it does
not find a match (not sure why it is not handled by `...`, but
tryCatch may be parsed special, or non-legal argument names on the RHS
of `-` may be checked).

Unfortunately allowing `=` in certain common places to mimic `<-`
hides this difference and leads to these types of errors when the
difference is important.



On Thu, May 18, 2023 at 8:38 AM Calboli Federico (LUKE)
<federico.calboli using luke.fi> wrote:
>
> Hello,
>
> I run a fisher.test() in a loop, with the issue that some of the data will not be useable. To protect the loop I used tryCatch but:
>
> sexsnp = rep(NA, 1750)
> for(i in 1:1750){tryCatch(sexsnp[i] = fisher.test(table(data[,3], data[,i + 38]))$p, error = function(e) print(NA))}
> Error: unexpected '=' in "for(i in 1:1750){tryCatch(sexsnp[i] ="
>
> But this works:
>
> for(i in 1:1750){tryCatch(sexsnp[i] <- fisher.test(table(data[,3], data[,i + 38]))$p, error = function(e) print(NA))}
> [1] NA
> [1] NA
> [1] NA
>
> The only difference is the use of ’=’ or ‘<-‘ to assign the p value of the fisher test to the vector.  I stopped using ‘<-‘ eons ago so it took a bit to figure out.
>
> Tested on R 4.1.2 on ContOS 8 , and on  R 4.3.0 on a M1 mac with the same result.  I’d be obliged if someone can explain why tryCatch assigns items with ‘<-‘ and not ‘=’.
>
> Cheers
>
> F
>
> Federico Calboli
> Tutkija
> Genomiikka ja jalostus
> Luonnonvarakeskus
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using 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/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Gregory (Greg) L. Snow Ph.D.
538280 using gmail.com



More information about the R-help mailing list