I have a simple function such as:
foo <- function(x) {
call <- lapply(match.call(), deparse)
testit <- capture.output(tryCatch(eval(x), error = function(e) e))
if (grepl("Error", testit)) {
return(call$x)
}
}
and I would like to detect a formula when x is not an object:
# this works> foo(A + B)
[1] "A + B"
# but this doesn't> foo(A + B => C)
Error: unexpected '=' in "foo(A + B ="
Can I prevent it from evaluating the "=" sign?
The addition sign "+" hasn't been evaluated, and I was hoping the
"=" would
not get evaluated either. The "=>" sign is important for other
purposes,
not related to this example.
Thank you in advance,
Adrian
--
Adrian Dusa
University of Bucharest
Romanian Social Data Archive
Soseaua Panduri nr.90
050663 Bucharest sector 5
Romania
[[alternative HTML version deleted]]
On 12/04/2016 6:24 AM, Adrian Du?a wrote:> I have a simple function such as: > > foo <- function(x) { > call <- lapply(match.call(), deparse) > testit <- capture.output(tryCatch(eval(x), error = function(e) e)) > if (grepl("Error", testit)) { > return(call$x) > } > } > > and I would like to detect a formula when x is not an object: > > # this works >> foo(A + B) > [1] "A + B" > > # but this doesn't >> foo(A + B => C) > Error: unexpected '=' in "foo(A + B =" > > Can I prevent it from evaluating the "=" sign?It never gets to evaluating it. It is not a legal R statement, so the parser signals an error. If you want to pass arbitrary strings to a function, you need to put them in quotes. Duncan Murdoch> The addition sign "+" hasn't been evaluated, and I was hoping the "=" would > not get evaluated either. The "=>" sign is important for other purposes, > not related to this example. > > Thank you in advance, > Adrian > > -- > Adrian Dusa > University of Bucharest > Romanian Social Data Archive > Soseaua Panduri nr.90 > 050663 Bucharest sector 5 > Romania > > [[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. >
On 12/04/2016 11:24, Adrian Du?a wrote:> I have a simple function such as: > > foo <- function(x) { > call <- lapply(match.call(), deparse) > testit <- capture.output(tryCatch(eval(x), error = function(e) e)) > if (grepl("Error", testit)) { > return(call$x) > } > } > > and I would like to detect a formula when x is not an object: > > # this works >> foo(A + B) > [1] "A + B" > > # but this doesn't >> foo(A + B => C) > Error: unexpected '=' in "foo(A + B =" > > Can I prevent it from evaluating the "=" sign? > The addition sign "+" hasn't been evaluated, and I was hoping the "=" would > not get evaluated either. The "=>" sign is important for other purposes, > not related to this example. > > Thank you in advance, > Adrian > > -- > Adrian Dusa > University of Bucharest > Romanian Social Data Archive > Soseaua Panduri nr.90 > 050663 Bucharest sector 5 > Romania > > [[alternative HTML version deleted]] >Did you mean > foo (A + B >= C) ??
On Tue, Apr 12, 2016 at 2:08 PM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> [...] > > It never gets to evaluating it. It is not a legal R statement, so theparser signals an error.> If you want to pass arbitrary strings to a function, you need to put themin quotes. I see. I thought it was parsed inside the function, but if it's parsed before then quoting is the only option. To Keith: no, I mean it like this "A + B => C" which is translated as: "the union of A and B is sufficient for C" in set theoretic language. The "=>" operator means sufficiency, while "<=" means necessity. Quoting the expression is good enough, I was just curious if the quotes could be made redundant, somehow. Thank you both, Adrian -- Adrian Dusa University of Bucharest Romanian Social Data Archive Soseaua Panduri nr.90 050663 Bucharest sector 5 Romania [[alternative HTML version deleted]]