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("\\\\", "https://stackoverflow.com/", x)
# [1] "C:/Users/jd/Documents/folder/file.txt"

Leave a Comment