[R] formula argument evaluation
Duncan Murdoch
murdoch.duncan at gmail.com
Tue Apr 12 13:08:14 CEST 2016
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.
>
More information about the R-help
mailing list