[R] suprising behaviour of tryCatch()

Henrik Bengtsson henr|k@bengt@@on @end|ng |rom gm@||@com
Thu May 18 17:35:38 CEST 2023


... or just put the R expression inside curly brackets, e.g.

tryCatch({
  sexsnp[i] = fisher.test(table(data[,3], data[,i+38]))$p
}, error=function(e) print(NA))

Exercise: Compare

> list(a = 2)
$a
[1] 2

with

> list({ a = 2 })
[[1]]
[1] 2

and

> list(b = { a = 2 })
$b
[1] 2

BTW, note how the latter two assigned a <- 2 to the global environment.

/Henrik

On Thu, May 18, 2023 at 8:22 AM Berwin A Turlach
<berwin.turlach using gmail.com> wrote:
>
> G'day Federico,
>
> On Wed, 17 May 2023 10:42:17 +0000
> "Calboli Federico (LUKE)" <federico.calboli using luke.fi> wrote:
>
> > 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] ="
>
> Try:
>
> R> for(i in 1:1750){tryCatch(eval(expression("sexsnp[i] = fisher.test(table(data[,3], data[,i+38]))$p")), error=function(e)print(NA))}
>
> or
>
> R> for(i in 1:1750){tryCatch(bquote("sexsnp[i] = fisher.test(table(data[,3], data[,i+38]))$p"), error=function(e) print(NA))}
>
> or
>
> R> for(i in 1:1750){tryCatch(.("sexsnp[i] = fisher.test(table(data[,3], data[,i+38]))$p"), error=function(e) print(NA))}
>
> If you want to use the '='.
>
> Cheers,
>
>         Berwin
>
> ______________________________________________
> 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.



More information about the R-help mailing list