0

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)?
zephryl
  • 14,633
  • 3
  • 11
  • 30
clp
  • 1,098
  • 5
  • 11
  • 3
    As in many programming languages, a symbol in R cannot start with a number. `1A` is not a valid R symbol, and you can't `quote` a syntactically invalid expression. Can I ask what you are trying to achieve here? There will probably be a work-around, but we need some context to help. – Allan Cameron Dec 17 '22 at 12:52
  • The fragment of code from igraph gives the same error, unless you make the expression into a string: `igraph::graph_from_literal("1A +--+ 1B")` – Allan Cameron Dec 17 '22 at 13:48
  • But the code `igraph::graph_from_literal(1A +--+ 1B)` just results in an unexpected symbol error – Allan Cameron Dec 17 '22 at 14:03
  • 1
    No, this is not a bug in igraph. You cannot have the symbol `1A` in R unless it is wrapped in quotation marks. See my explanation below. You cannot have "non-standard expressions" like this _anywhere_ unless they are wrapped in quotation marks. The R parser will not allow them. Could you please add some context to explain what you are trying to achieve? – Allan Cameron Dec 17 '22 at 14:29
  • 1
    Are you trying to create a variable whose name is 1A-1B? To do that surround it with backquotes ` ``1A-1B`` ` . – G. Grothendieck Dec 17 '22 at 15:55
  • In case of `igraph` the solution is to quote the illegal objects: `igraph::graph_from_literal("1A" +--+ "1B")`. – clp Dec 17 '22 at 19:12

2 Answers2

5

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

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Consider for example path expressions in igraph, In igraph `g2a <- make_graph(c("1A", "2B"))` is valid, but `g2b <- make_graph(~ A1-B2)` is not. – clp Dec 17 '22 at 13:50
  • But these are _strings_, not symbols. If you wrap the symbol in quotation marks, then you can have anything inside the quotes - you don't need syntactically valid expressions inside a string. – Allan Cameron Dec 17 '22 at 14:02
0

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()
## [1] "2A" "2B" "A2"
clp
  • 1,098
  • 5
  • 11