Home > Blockchain >  How to quote() non-standard expressions, e.g. 2A-2B?
How to quote() non-standard expressions, e.g. 2A-2B?

Time:12-19

Or any other non-parseble expression as in igraph::graph_from_literal(1A -- 1B).

Function call quote(1A-2B) gives Error: unexpected symbol in quote(1A".

How to get a result similar to

  • quote(A-B),
  • quote(1-1)?

CodePudding user response:

As in many programming languages, a symbol (i.e. a variable name) in R cannot start with a number. Since 1A is not a valid R symbol, the expression 1A - 2B is not syntactically valid. Because quote will parse but not execute an expression, you cannot use expressions containing invalid symbols like 1A or 2B inside quote.

It is difficult to know what you are trying to achieve here, but it seems likely you want to use the quoted expression in a plot. If this is the case, you can use quote(1*A - 2*B), since this is a valid expression and the * symbols will be removed at plotting.

my_quote <- quote(1*A - 2*B)

plot.new()
text(x = 0.5, y = 0.5, label = my_quote, cex = 6)

Created on 2022-12-17 with reprex v2.0.2

CodePudding user response:

Quote 2A - 2B as quote("2A" - "2B"). When an expression in quote(expr) contains non parsible object names, quote it.

Examples.

A2   <- 1       ## creates object "A2" with value `[1] 1`.
2A   <- 1       ## fails because "2A" is a not syntactically valid R expression.
"2A" <- 1       ## creates object "2A" with value `[1] 1`.
assign("2B", 1) ## creates object "2B" with value `[1] 1`.
if (require(igraph)) g <- graph_from_literal("1A" - "2A")
A2; get("2A"); get("2B")
ls()
## Output.
## [1] "2A" "2B" "A2"
  • Related