gkmagic gkmagic - 1 year ago 80
R Question

Backslash issue (url in url)

I have seen the solution to this problem in other languages but not in R.
I have a simple api call where I make a url that contains other url.
Something like that:


The issue is when I paste the url which is

url = "http://www.someotherserver.com/something/pic.jpg"

When I print url it gives me:


Tried doing gsub, substring, saving to a file but nothing seems to work. How to get rid of these backslashes after q= and after url?

Answer Source

Everything is fine with your code. It produces the string


The backslash is inserted only by the print function to show you that the " does not end the string, but is part of the string.


> "abc"
[1] "abc"
> "abc \"def\""
[1] "abc \"def\""
> cat("abc \"def\"\n")
abc "def"

In the second case, it prints a string representation of the string the same you typed in it. However, this is not the actual string that will. You can see the "raw" string by using cat().

Also, the strings 'abd "def"' and "abc \"def\"" are equal. These are both different ways to enter the same string. When using single quotes, you do not have to escape double quotes. However, when printing a string, R always uses double quotes:

> "abc \"def\""
[1] "abc \"def\""
> 'abc "def"'
[1] "abc \"def\""