Efficiently convert backslash to forward slash in R
In R, you've to escape the \
with \\
So, your path should be:
x <- "C:\\Users\\jd\\Documents\\folder\\file.txt"
To get that, you can do:
x <- readline()
then, at the prompt, paste your unmodified path (CTRL+V then ENTER)
Finally, to change \\
to /
everywhere, you could use gsub
, once again by escaping the \
, but twice, as follows:
gsub("\\\\", "/", x)
# [1] "C:/Users/jd/Documents/folder/file.txt"
Why R uses forward slash (/) and not backslash (\) in file paths
Interesting question.
First off, the "forward slash" /
is actually more common as it used by Unix, Linux, and macOS.
Second, the "backward slash" \
is actually somewhat painful as it is also an escape character. So whenever you want one, you need to type two in string: "C:\\TEMP"
.
Third, R on Windows knows this and helps! So you can you use a forward slash whereever you would use a backward slash: "C:/TEMP"
works the same!
Fourth, you can have R compute the path for you and it will use use the separator: file.path("some", "dir")
.
So the short answer: R uses both on Windows and lets you pick whichever you find easier. But remember to use two backward slashes (unless you use the very new R 4.0.0 feature on raw strings which I'll skip for now).
Replacing Backward slash in R
You have the arguments in the wrong order and you need to escape the backslashes.
> stringr::str_replace("\\asd", "\\\\", "//")
[1] "//asd"
How to automatically handle strings/paths with backslashes?
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
Replace slash with a single backslash in R
The solution is to escape the escape character which means 4 '\' in the end.
cat(gsub('/', '\\\\', "file_path/file_name.txt"))
Look at the difference between your standard output with like 'print()' which escapes the escape character, or get the plain string by using 'cat()'.
str_replace_all(string = x, pattern = "/", replacement = "\\\\")
cat(str_replace_all(string = x, pattern = "/", replacement = "\\\\"))
Escaping backslash (\) in string or paths in R
From R 4.0.0 you can use r"(...)"
to write a path as raw string constant, which avoids the need for escaping:
r"(E:\RStuff\test.r)"
# [1] "E:\\RStuff\\test.r"
There is a new syntax for specifying raw character constants similar to the one used in C++:
r"(...)"
with...
any character sequence not containing the sequence)"
. This makes it easier to write strings that contain backslashes or both single and double quotes. For more details see?Quotes
.
substitute single backslash in R
When you need to use a backslash in the string in R, you need to put double backslash. Also, when you use gsub("\\", "/", str)
, the first argument is parsed as a regex, and it is not valid as it only contains a single literal backslash that must escape something. In fact, you need to make gsub
treat it as a plain text with fixed=TRUE
.
However, you might want to use normalizePath
, see this SO thread.
Related Topics
How Does One Reorder Columns in a Data Frame
Group by Multiple Columns in Dplyr, Using String Vector Input
Method to Extract Stat_Smooth Line Fit
How to Extract a Single Column from a Data.Frame as a Data.Frame
Painless Way to Install a New Version of R
How to Remove All Whitespace from a String
Incomplete Final Line' Warning When Trying to Read a .Csv File into R
How to Use Grep()/Gsub() to Find Exact Match
Why Do R Objects Not Print in a Function or a "For" Loop
Using the Rjava Package on Win7 64 Bit With R
Finding Percentage in a Sub-Group Using Group_By and Summarise
Generate Sequence Within Group in R