Ignore escape characters (backslashes) in R strings

You can try to use the 'allowEscapes' argument in scan()

X=scan(what="character",allowEscapes=F)
C:\Users\mhermans\somefile.csv

print(X)
[1] "C:\\Users\\mhermans\\somefile.csv"

First you need to get it assigned to a name:

pathname <- 'C:\\Users\\mhermans\\somefile.csv'

Notice that in order to get it into a name vector you needed to double them all, which gives a hint about how you could use regex. Actually, if you read it in from a text file, then R will do all the doubling for you. Mind you it not really doubling the backslashes. It is being stored as a single backslash, but it's being displayed like that and needs to be input like that from the console. Otherwise the R interpreter tries (and often fails) to turn it into a special character. And to compound the problem, regex uses the backslash as an escape as well. So to detect an escape with grep or sub or gsub you need to quadruple the backslashes

 gsub("\\\\", "/", pathname)
# [1] "C:/Users/mhermans/somefile.csv"

You needed to doubly "double" the backslashes. The first of each couple of \'s is to signal to the grep machine that what next comes is a literal.

Consider:

 nchar("\\A")
#  returns `[1] 2`

As of version 4.0, introduced in April 2020, R provides a syntax for specifying raw strings. The string in the example can be written as:

path <- r"(C:\Users\mhermans\somefile.csv)"

From ?Quotes:

Raw character constants are also available using a syntax similar to the one used in C++: r"(...)" with ... any character sequence, except that it must not contain the closing sequence )". The delimiter pairs [] and {} can also be used, and R can be used in place of r. For additional flexibility, a number of dashes can be placed between the opening quote and the opening delimiter, as long as the same number of dashes appear between the closing delimiter and the closing quote.


If file E:\Data\junk.txt contains the following text (without quotes): C:\Users\mhermans\somefile.csv

You may get a warning with the following statement, but it will work:

 texinp <- readLines("E:\\Data\\junk.txt")

If file E:\Data\junk.txt contains the following text (with quotes): "C:\Users\mhermans\somefile.csv"

The above readlines statement might also give you a warning, but will now contain:

"\"C:\Users\mhermans\somefile.csv\""

So, to get what you want, make sure there aren't quotes in the incoming file, and use:

 texinp <- suppressWarnings(readLines("E:\\Data\\junk.txt"))