How to put \' in my string using paste0 function [duplicate] - r

This question already has answers here:
How to escape backslashes in R string
(3 answers)
Closed 5 years ago.
I have an array:
t <- c("IMCR01","IMFA02","IMFA03")
I want to make it look like this:
"\'IMCR01\'","\'IMFA02\'","\'IMFA03\'"
I tried different ways like:
paste0("\'",t,"\'")
paste0("\\'",t,"\\'")
paste0("\\\\'",t,"\\\\'")
But none of them is correct. Any other functions are OK as well.

Actually your second attempt is correct:
paste0("\\'",t,"\\'")
If you want to tell paste to use a literal backslash, you need to escape it once (but not twice, as you would need within a regex pattern). This would output the following to the console in R:
[1] "\\'IMCR01\\'" "\\'IMFA02\\'" "\\'IMFA03\\'"
The trick here is that the backslash is even being escaped by R in the console output. If you were instead to write t to a text file, you would only see a single backslash as you wanted:
write(t, file = "/path/to/your/file.txt")
But why does R need to escape backslash when writing to its own console? One possibility is that if it were to write a literal \n then this would actually be interpreted by the console as a newline. Hence the need for eacaping is still there.

Related

Replace latex with r strings using gsub [duplicate]

This question already has an answer here:
"'\w' is an unrecognized escape" in grep
(1 answer)
Closed 1 year ago.
I would like to find and replace tabular instances by tabularx. I tried with gsub but it seems to enter me into a world of escaping pain. Following other questions and answers I find fixed=TRUE which is the best I so far have. The code snippet below almost works, \B is unrecognized. If I escape it twice I get \BEGIN as output!
texText <- '\begin{tabular}{rl}\begin{tabular}{rll}'
texText <- gsub("\begin{tabular}{rl}", "\BEGIN{tabular}{rll}", texText, fixed=TRUE)
I'm using BEGIN as my test to see what is happening. This is before I get to tackling the question of what goes on in the brackets {rl} {ll} {rrl} etc. Ideally I'm looking for a regex that would output:
\begin{tabularx}{rX}\begin{tabularx}{rlX}
That is the final column is replaced by X.
Try using proper escaping:
texText <- "\begin{tabular}{rl}\begin{tabular}{rll}"
output <- gsub("\begin\\{tabular\\}", "\begin{tabularx}", texText)
output
[1] "\begin{tabularx}{rl}\begin{tabularx}{rll}"
A literal backslash requires two backslashes, and also metacharacters such as { and } require two backslashes.

How to automatically handle strings/paths with backslashes? [duplicate]

This question already has answers here:
How to escape backslashes in R string
(3 answers)
Efficiently convert backslash to forward slash in R
(11 answers)
Closed 3 years ago.
I often want to read in csv files and I get the path by using shift + right click and then clicking "copy path".
I paste this path into my code. See an example below:
read_csv("C:\Users\me\data\file.csv")
Obviously this doesn't work because of the backslashes. My current solution is to escape each one, so that my code looks like this:
read_csv("C:\\Users\\me\\data\\file.csv")
It works, but it's annoying and occasionally I'll get errors because I missed one of the backslashes.
I wanted to create a function automatically adds the extra slashes
fix_path <- function(string) str_replace(string, "\\\\", "\\\\\\\\")
but R won't recognize the string in the first place until the backslashes are taken care of.
Is there another way to deal with this? Python has the option of adding an "r" before strings to note that the backslashes should be treated just as regular backslashes, is there anything similar in R? To be clear, I know that I can escape the backslashes, but I am looking for a way to do it automatically.
You can use this hack. Suppose you had copied your path as mentioned then you could use
scan("clipboard", "character", quiet = TRUE)
scan reads the text copied from the clipboard and takes care about the backslashes. Then copy again what is returned from scan

How to remove '+ off' from the end of string? [duplicate]

This question already has answers here:
How do I deal with special characters like \^$.?*|+()[{ in my regex?
(2 answers)
Closed 4 years ago.
Similar to R - delete last two characters in string if they match criteria except I'm trying to get rid of the special character '+' as well. I also attached a picture of my output.
When I attempt to use the escape command of '+', I get an error message saying
Error: '\+' is an unrecognized escape in character string starting ""\\s\+"
As you noticed, + is a metacharacter in regex so it needs to be escaped. \+ escapes that character, but \, itself, is a special character in R character strings so it, too, needs to be escaped. This is an R requirement, not a regex requirement.
This means that, instead of '\+', you need to write '\\+'.

replacing text with a single backslash [duplicate]

This question already has answers here:
Replacing white space with one single backslash
(2 answers)
Closed 6 years ago.
I have this text and I want to replace // with \
This is the text sdfd//dfsadfs
and I want it to be sdfd\dfsadfs
Can gsub work? This does not work: gsub("//","[\]","sdfd//dfsadfs")
I had a similar problem before. Like #Psidom commented, you should use gsub("//","\\\\","sdfd//dfsadfs"). This will replace //(2 characters) with \\ which is actually a single character in R (Check by running nchar("\\")). Even though it is prints as \\, it behaves as \. You can check this by running cat("\\"). If you exported the data after running gsub to a table (or csv), I believe there will be only one \

Formatting R console output [duplicate]

This question already has answers here:
Remove quotes from a character vector in R
(11 answers)
Closed 6 years ago.
I would like to be able to copy and paste the output in R console without always having to remove these tags and quotes
print('love')
[1] "love"
What I require
print('love')
love
Note: I also want to remove the [1] not only the double quotes.
You could use the cat function, it will indeed print your string without parenthesis:
cat('love')
#### love
See the help page ?cat
Outputs the objects, concatenating the representations. cat performs
much less conversion than print.
You might also use print with the quote argument:
print("love", quote=FALSE)
#### [1] love
That way, you still get the [1]
See also this thread: https://stackoverflow.com/a/5218361/3871924

Resources