0

I'm trying to combine some stings to one. In the end this string should be generated: //*[@id="coll276"] So my inner part of the string is an vector: tag <- 'coll276' I already used the paste() method like this:

paste('//*[@id="',tag,'"]', sep = "")

But my result looks like following: //*[@id=\"coll276\"] I don't why R is putting some \ into my string, but how can I fix this problem?

Thanks a lot!

Leon
  • 371
  • 4
  • 11
  • Check out [this answer](https://stackoverflow.com/questions/4453020/paste-quotation-marks-into-character-string-within-a-loop?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) for an explanation. Try using `cat` instead of paste for output to the console if you want to see that your strings are being concatenated properly – Luke C May 30 '18 at 22:15

1 Answers1

3

tldr: Don't worry about them, they're not really there. It's just something added by print

Those \ are escape characters that tell R to ignore the special properties of the characters that follow them. Look at the output of your paste function:

paste('//*[@id="',tag,'"]', sep = "")
[1] "//*[@id=\"coll276\"]"

You'll see that the output, since it is a string, is enclosed in double quotes "". Normally, the double quotes inside your string would break the string up into two strings with bare code in the middle: "//*[@id\" coll276 "]"

To prevent this, R "escapes" the quotes in your string so they don't do this. This is just a visual effect. If you write your string to a file, you'll see that those escaping \ aren't actually there:

write(paste('//*[@id="',tag,'"]', sep = ""), 'out.txt')

This is what is in the file:

//*[@id="coll276"]

You can use cat to print the exact value of the string to the console (Thanks @LukeC):

cat(paste('//*[@id="',tag,'"]', sep = ""))
//*[@id="coll276"]

Or use single quotes (if possible):

paste('//*[@id=\'',tag,'\']', sep = "")
[1] "//*[@id='coll276']"
divibisan
  • 11,659
  • 11
  • 40
  • 58