Search code examples
stringrescapingspss

Ignore escape characters (backslashes) in R strings


While running an R-plugin in SPSS, I receive a Windows path string as input e.g.

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

I would like to use that path in subsequent R code, but then the slashes need to be replaced with forward slashes, otherwise R interprets it as escapes (eg. "\U used without hex digits" errors).

I have however not been able to find a function that can replace the backslashes with foward slashes or double escape them. All those functions assume those characters are escaped.

So, is there something along the lines of:

>gsub('\\', '/', 'C:\Users\mhermans')
C:/Users/mhermans

Solution

  • 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"