Make R use the notation C when exiting the terminal

Not sure if I'm using the correct terminology here, but I need the print or deparse use the C notation (for example, "\x05" instead of "\005" ) when avoiding bytes from the regular character set.

 x <- "This is a \x05 symbol" print(x) [1] "This is a \005 symbol" 

Is there any way to accomplish this?

I need this to generate BSON: http://bsonspec.org/#/specification . All examples explicitly use the notation \x05 .

+7
source share
2 answers

Hacking inside print internals seems like a bad idea. Instead, I think you should execute a line that escapes itself, and end up using cat to print the line without any extra escaping.

You can use encodeString to do the initial escaping, gregexpr to identify octal \0.. screens, strtoi to convert strings representing octal numbers to these numbers, sprintf to print numbers in hexadecimal format and regenmatches to work with the corresponding details. The whole process will look something like this:

 inputString <- "This is a \005 symbol. \x13 is \\x13." x <- encodeString(inputString) m <- gregexpr("\\\\[0-3][0-7][0-7]", x) charcodes <- strtoi(substring(regmatches(x, m)[[1]], 2, 4), 8) regmatches(x, m) <- list(sprintf("\\x%02x", charcodes)) cat(x, "\n") 

Note that this approach will convert octal escape sequences such as \005 to hexadecimal escape sequences such as \x05 , but other escape sequences such as \t or \a will not be affected. You may need even more code to solve these problems, but the above should contain all the necessary ingredients.

Please note that the BSON specification you are referring to almost certainly meant raw bytes, so if your line contains a character with code 5, which you can write as "\x05" at your input, and you write this line to the desired output in binary mode, it shouldn't matter how R prints this line for you. After all, octal \005 and hexadecimal \x05 are just two representations of the same byte that you write.

+2
source

Does cat your needs? Note: you need to avoid backslash:

 > x <- "This is a \\x05 symbol\n" > cat(x) This is a \x05 symbol 
-one
source

All Articles