0

I'm working with the following code:

Y_Columns <- c("Y.1.1")

paste('{"ImportId":"', Y_Columns, '"}', sep = "")

The paste function produces the following output:

"{\"ImportId\":\"Y.1.1\"}"

How do I get the paste function to omit the \? Such that, the output is:

"{"ImportId":"Y.1.1"}"

Thank you for your help.

Sharif Amlani
  • 1,138
  • 1
  • 11
  • 25
  • 1
    There are no backslashes in that string.character value. You are confused because R uses the backslash as an escape character.You asked to put double quote characters in the result and the only way to represent them unambiguously with the default `print`output is to escape them. – IRTFM Mar 11 '21 at 00:41
  • Interesting. So while I see a backslash, there isn't actually one in the string? What is an escape character? – Sharif Amlani Mar 11 '21 at 00:43
  • Right. Searching SO was not very fruitful but searching Google was:https://en.wikipedia.org/wiki/Escape_character – IRTFM Mar 11 '21 at 01:14
  • Using `cat` will show you the actual string : `cat(paste('{"ImportId":"', Y_Columns, '"}', sep = ""))` – Ronak Shah Mar 11 '21 at 02:22

1 Answers1

0

Note: I did do a search on SO to see if there were any Q's that asked "what is an escape character in R". But I didn't review all the 160 answers, only the first 20.

This is one way of demonstrating what I wrote in my comment:

 out <- paste('{"ImportId":"', Y_Columns, '"}', sep = "")
 out
#[1] "{\"ImportId\":\"Y.1.1\"}"
 ?print
 print(out,quote=FALSE)
#[1] {"ImportId":"Y.1.1"}

Both R and regex patterns use escape characters to allow special characters to be displayed in print output or input. (And sometimes regex patterns need to have doubled escapes.) R has a few characters that need to be "escaped" in certain situation. You illustrated one such situation: including double-quote character inside a result that will be printed with surrounding double-quotes. If you were intending to include any single quotes inside a character value that was delimited by single quotes at the time of creation, they would have needed to be escaped as well.

out2 <- '\'quoted\''
nchar(out2)
#[1] 8   ... note that neither the surround single-quotes nor the backslashes get counted
> out2
[1] "'quoted'"  ... and the default output quote-char is a double-quote.

Here's a good Q&A to review:How to replace '+' using gsub() function in R

It has two answers, both useful: one shows how to double escape a special character and the other shows how to use teh fixed argument to get around that requirement.

And another potentially useful Q&A on the topic of handling Windows paths: File path issues in R using Windows ("Hex digits in character string" error)

And some further useful reading suggestions: Look at the series of help pages that start with capital letters. (Since I can never remember which one has which nugget of essential information, I tried ?Syntax first and it has a "See Also" list of essential reading: Arithmetic, Comparison, Control, Extract, Logic, NumericConstants, Paren, Quotes, Reserved. and I then realized what I wanted to refer you to was most likely ?Quotes where all the R-specific escape sequence letters should be listed.

IRTFM
  • 258,963
  • 21
  • 364
  • 487