Re: [R] suprising behaviour of tryCatch()

2023-05-18 Thread Berwin A Turlach
G'day Henrik,

On Thu, 18 May 2023 08:35:38 -0700
Henrik Bengtsson  wrote:

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

I was wondering whether I was having a fortunes::fortune(106) moment. :-)

Cheers,

Berwin

__
R-help@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.


Re: [R] suprising behaviour of tryCatch()

2023-05-18 Thread Berwin A Turlach
G'day Greg,

On Thu, 18 May 2023 08:57:55 -0600
Greg Snow <538...@gmail.com> wrote:

[...]
> `fun(x <- expr)`  Assigns the value of `expr` to the variable `x` in
> the frame/environment that `fun` is called from.

Only if the argument 'x' is evaluated during the function call AFAIK.
If it is not, due to lazy evaluation rules, then no assignment takes
place. 

> 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).

After exact matching on names comes partial matching on names and then
positional matching.  If after these three rounds there are still
actual arguments left over, they are assigned to "..." (if it exist as
formal argument).

So under the usual rules of how actual arguments
are passed to formal arguments in Federico's call the argument
"sexsnp[i] = fisher.test(table(data[,3], data[,i + 38]))$p" should be
assigned to the formal argument "expr" of tryCatch() and "error =
function(e) print(NA))" should be absorbed by "...".

But it seems, as "=" has a different meaning in function call, the
expression passed to the "expr" formal argument is not allowed to
contain a "=".  Not sure why. :-)

I guess that the intended use is actually:

R> for(i in 1:1750){sexsnp[i] = tryCatch(fisher.test(table(data[,3], data[,i + 
38]))$p, error = function(e) print(NA))}

I.e. tryCatch() returns the result of the evaluated expression (if
successful, otherwise the result of an error handler) and that result
can be assigned.  It is not expected the that expression contains an
assignment operation.  But I am just guessing. :)

Cheers,

Berwin

__
R-help@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.


Re: [R] suprising behaviour of tryCatch()

2023-05-18 Thread Henrik Bengtsson
... 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
 wrote:
>
> G'day Federico,
>
> On Wed, 17 May 2023 10:42:17 +
> "Calboli Federico (LUKE)"  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@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.

__
R-help@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.


Re: [R] suprising behaviour of tryCatch()

2023-05-18 Thread Berwin A Turlach
G'day Federico,

On Wed, 17 May 2023 10:42:17 +
"Calboli Federico (LUKE)"  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@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.


Re: [R] suprising behaviour of tryCatch()

2023-05-18 Thread Berwin A Turlach
G'day Federico,

On Wed, 17 May 2023 10:42:17 +
"Calboli Federico (LUKE)"  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

__
R-help@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.


Re: [R] suprising behaviour of tryCatch()

2023-05-18 Thread Greg Snow
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)
 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@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.
538...@gmail.com

__
R-help@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.


Re: [R] suprising behaviour of tryCatch()

2023-05-18 Thread Bill Dunlap
>  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.

The '=' has a context-dependent meaning - in a function call it is used to
name arguments and outside of a function call it is used for assignments.
This has nothing to do with tryCatch in particular.  E.g.,

> f <- function(x) x + 100
> f(y <- 10)
[1] 110
> y
[1] 10
> f(y = 20)
Error in f(y = 20) : unused argument (y = 20)
> y
[1] 10


I recommend starting to use <- for assignments again (or not doing
assignments inside of function calls).

-Bill

On Thu, May 18, 2023 at 7:38 AM Calboli Federico (LUKE) <
federico.calb...@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@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


[R] suprising behaviour of tryCatch()

2023-05-18 Thread Calboli Federico (LUKE)
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@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.