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]]
> The only difference is the use of ?=? or ?<-? to assign the p value ofthe 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.calboli at 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 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/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
G'day Federico, On Wed, 17 May 2023 10:42:17 +0000 "Calboli Federico (LUKE)" <federico.calboli at 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
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 at 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 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/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com
G'day Federico, On Wed, 17 May 2023 10:42:17 +0000 "Calboli Federico (LUKE)" <federico.calboli at 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