r - Remove single backslash - r

I have a string with backslash and I want to remove them.
test = "m \"#\""
Have tried the following but none works :
gsub( "\\\\", "", test )
gsub( "\\\\", "", test, fixed = T )
gsub( "\\", "", test )
gsub( "\\", "", test, fixed = T )
Have looked into similar questions but none of the solutions work.
Replace single backslash in R
Remove Single Backslash String R
Edit : Actually this text is going to be passed in system() function to run a mosquitto client. User will give various parameters as input and the command will be created up on the fly.
The full command looks like this : mosquitto_sub -h test.mosquitto.org -q 0 -k 60 -t \"#\"
However it is expected to be like this : mosquitto_sub -h test.mosquitto.org -q 0 -k 60 -t "#"
Otherwise system() does not take it. Hence is the requirement to remove the backslaches.
The parameters 0 and 60 and # are supplied by user. Hence using paste0() to make this string. After the string is created the backslashes comes up.
The string given in text here is to create a reproducible and short example here.

I think JvdV is right, I don't think the string "m "#"" can exist within R without the backslashes. The backslash makes " and # characters rather than acting as open/close quotation marks and a comment mark respectively.
If you had """" you'd get an error as you have a set of quotation marks within another set which is not possible. The same might occur for "m "#"". However if you input "m "#"" the hash acts a comment symbol, making everything after it a comment and you get "m ". You need backslashes to make " not a quotation mark but a character, and # not a comment symbol but a character.

There is no backslash in your string
grepl("\\", test, fixed = TRUE)
# FALSE

Related

R Shiny: Is there any way to read a single backslash in user input?

I'm making a Shiny app that constructs a bash script to run on a cluster (basically just a txt file). One of the user inputs is a curl command (provided by the database where the files are stored) that they can copy/paste into a textInput field in the app. When run on the cluster, it will download the file for further processing. However, the curl command they provide contains several single backslashes. Example:
curl --cookie jgi_session=/api/sessions/ec32f2d578304a9e62b4646ae2bec6d4 --output download.20210731.211924.zip -d "{\"ids\":[\"5d94dc9fc0d65a87debccfd3\"]}" -H "Content-Type: application/json" https://files.jgi.doe.gov/filedownload/
It works fine if I paste this directly into a script or if I manually add in double backslashes, but I want to keep this as user friendly as possible. Every other post I've seen about this just says to use double backslashes, but I'd rather do this automatically if at all possible. So any ideas? I'm open to alternate solutions, less work for the user the better.
Your code is picking up the curl line as escaped characters. When you write to file, those escaped characters get converted to their actual character (i.e \" gets converted to literal ".
To avoid, replace special escaped characters by the character sequence that literally created the escape sequence. So to build \" in the final written string, you have to produce \\" as escaped character sequence (which is what the output of a print commmand should show).
Once way to achieve this for this particular character sequence is
escapedString = gsub('\"', '\\"', curlString)
Note that, in terms of string interpretation, \" is a single character (converting to "), while \\" is a sequence of two characters: an escaped \ and a literal ", converting to \" when written, which is the desired output.

gsub remove backslash and numbers from string [duplicate]

I'm writing strings which contain backslashes (\) to a file:
x1 = "\\str"
x2 = "\\\str"
# Error: '\s' is an unrecognized escape in character string starting "\\\s"
x2="\\\\str"
write(file = 'test', c(x1, x2))
When I open the file named test, I see this:
\str
\\str
If I want to get a string containing 5 backslashes, should I write 10 backslashes, like this?
x = "\\\\\\\\\\str"
[...] If I want to get a string containing 5 \ ,should i write 10 \ [...]
Yes, you should. To write a single \ in a string, you write it as "\\".
This is because the \ is a special character, reserved to escape the character that follows it. (Perhaps you recognize \n as newline.) It's also useful if you want to write a string containing a single ". You write it as "\"".
The reason why \\\str is invalid, is because it's interpreted as \\ (which corresponds to a single \) followed by \s, which is not valid, since "escaped s" has no meaning.
Have a read of this section about character vectors.
In essence, it says that when you enter character string literals you enclose them in a pair of quotes (" or '). Inside those quotes, you can create special characters using \ as an escape character.
For example, \n denotes new line or \" can be used to enter a " without R thinking it's the end of the string. Since \ is an escape character, you need a way to enter an actual . This is done by using \\. Escaping the escape!
Note that the doubling of backslashes is because you are entering the string at the command line and the string is first parsed by the R parser. You can enter strings in different ways, some of which don't need the doubling. For example:
> tmp <- scan(what='')
1: \\\\\str
2:
Read 1 item
> print(tmp)
[1] "\\\\\\\\\\str"
> cat(tmp, '\n')
\\\\\str
>

fread escapes quotes when not necessary

I'm reading a csv file with quoted fields using the fread function. In some of the fields escaped quotes (\") appear. I don't understand why the fread function escapes these quotes that are already escaped.
I reproduce the behavior with a simple example. I created a file with a single line and a single field:
"Hello \"World\" "
If I run the following R command:
table <- fread(input = "/tmp/quoteprova.csv", header=FALSE, sep = "\t")
the table variable will look like this:
V1
1: Hello \\"World\\"
I would expect instead this result:
V1
1: Hello \"World\"
Am I missing to specify some options in order to get the expected behavior?
You are geting what you want. \\" is two characters: a normal character \ and a ". Because \ is used to escape special characters and \* would be interpreted as a special character that are escaped with \. Thefore the additional \ (the first one) here will tell you that the second \ is not used to escape " and should be treated as is.
see this example:
> nchar('\\"')
[1] 2
> nchar('\"')
[1] 1
also this R faq

Difference between \\ vs \ backreference for regex in r [duplicate]

I'm writing strings which contain backslashes (\) to a file:
x1 = "\\str"
x2 = "\\\str"
# Error: '\s' is an unrecognized escape in character string starting "\\\s"
x2="\\\\str"
write(file = 'test', c(x1, x2))
When I open the file named test, I see this:
\str
\\str
If I want to get a string containing 5 backslashes, should I write 10 backslashes, like this?
x = "\\\\\\\\\\str"
[...] If I want to get a string containing 5 \ ,should i write 10 \ [...]
Yes, you should. To write a single \ in a string, you write it as "\\".
This is because the \ is a special character, reserved to escape the character that follows it. (Perhaps you recognize \n as newline.) It's also useful if you want to write a string containing a single ". You write it as "\"".
The reason why \\\str is invalid, is because it's interpreted as \\ (which corresponds to a single \) followed by \s, which is not valid, since "escaped s" has no meaning.
Have a read of this section about character vectors.
In essence, it says that when you enter character string literals you enclose them in a pair of quotes (" or '). Inside those quotes, you can create special characters using \ as an escape character.
For example, \n denotes new line or \" can be used to enter a " without R thinking it's the end of the string. Since \ is an escape character, you need a way to enter an actual . This is done by using \\. Escaping the escape!
Note that the doubling of backslashes is because you are entering the string at the command line and the string is first parsed by the R parser. You can enter strings in different ways, some of which don't need the doubling. For example:
> tmp <- scan(what='')
1: \\\\\str
2:
Read 1 item
> print(tmp)
[1] "\\\\\\\\\\str"
> cat(tmp, '\n')
\\\\\str
>

How to escape backslashes in R string

I'm writing strings which contain backslashes (\) to a file:
x1 = "\\str"
x2 = "\\\str"
# Error: '\s' is an unrecognized escape in character string starting "\\\s"
x2="\\\\str"
write(file = 'test', c(x1, x2))
When I open the file named test, I see this:
\str
\\str
If I want to get a string containing 5 backslashes, should I write 10 backslashes, like this?
x = "\\\\\\\\\\str"
[...] If I want to get a string containing 5 \ ,should i write 10 \ [...]
Yes, you should. To write a single \ in a string, you write it as "\\".
This is because the \ is a special character, reserved to escape the character that follows it. (Perhaps you recognize \n as newline.) It's also useful if you want to write a string containing a single ". You write it as "\"".
The reason why \\\str is invalid, is because it's interpreted as \\ (which corresponds to a single \) followed by \s, which is not valid, since "escaped s" has no meaning.
Have a read of this section about character vectors.
In essence, it says that when you enter character string literals you enclose them in a pair of quotes (" or '). Inside those quotes, you can create special characters using \ as an escape character.
For example, \n denotes new line or \" can be used to enter a " without R thinking it's the end of the string. Since \ is an escape character, you need a way to enter an actual . This is done by using \\. Escaping the escape!
Note that the doubling of backslashes is because you are entering the string at the command line and the string is first parsed by the R parser. You can enter strings in different ways, some of which don't need the doubling. For example:
> tmp <- scan(what='')
1: \\\\\str
2:
Read 1 item
> print(tmp)
[1] "\\\\\\\\\\str"
> cat(tmp, '\n')
\\\\\str
>

Resources