4

I unintentionally ran into this and was wondering if there was an explanation for it. In a toy example I put an escape character in a variable level.

  library(dplyr)
  library(gt)
    mt2 <- mutate(mtcars, cylx = ifelse(cyl == 4, "1\2", "2/3"))

Originally I thought it would simply escape the '2' when cyl was 4. However 1\2 actually evaluates to 1\002. When using View(mt2) you cannot see it but it does evaluate to a special character when you try and print a table gt::gt(mt2) . This will show in all printing options but I used gt as an example. So my question is why does r assume I wanted 1\2 to evaluate to 1\002? Shouldn't r throw an error because I did not explicitly write 1\002 (because \2 is not technically an escape character)?

Mike
  • 3,797
  • 1
  • 11
  • 30
  • 1
    I guess if you squint kind of hard you could argue that this behavior is documented in `?Quotes`, for the octal character codes, and that R just doesn't translate anything below 040? – joran Apr 15 '19 at 21:40
  • 1
    ...I take that back, some things below 040 are translated, but I guess some of them aren't, or aren't meaningful when I'm just printing them on the console. – joran Apr 15 '19 at 21:43

1 Answers1

2

I just want to flush out @joran's answer a bit. ?Quote does (sort of) give the reason here when it mentions octal code

\nnn character with given octal code (1, 2 or 3 digits)

So adding the three digit octal code after the \ will produce the corresponding number/character:

> c('\110' ,'\074', '\076') 
 [1] "H" "<" ">" 

So when you provide '\002', as the link suggests, you will get the octal code 002.

Moreover, R won't require you to provide leading 0s for these octal codes. R just assumes you meant to include them.

 > c('\110' ,'\74', '\76')
 [1] "H" "<" ">" 

 > '\2' == '\002'
 [1] TRUE 

 > '\2' == '\02' 
 [1] TRUE

as.octmode() is another way to think about this:

Convert or print integers in octal format, with as many digits as are needed to display the largest, using leading zeroes as necessary.

 > as.octmode("002")
 [1] "2"
Peter_Evan
  • 947
  • 10
  • 17
  • Thank you for your answer. It is interesting that R assumes the leading 0's if you do not include them. Or at least I think it is interesting because both `'\020'` and `'\002'` are separate codes so I would not expect R to assume users always want leading 0's attached. – Mike Apr 16 '19 at 13:53