Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore escape characters (backslashes) in R strings

Tags:

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 
like image 937
mhermans Avatar asked Jan 13 '11 21:01

mhermans


People also ask

How do I ignore an escape character in a string?

To ignoring escape sequences in the string, we make the string as "raw string" by placing "r" before the string. "raw string" prints as it assigned to the string.

How do you backslash in r string?

In R (and elsewhere), the backslash is the “escape” symbol, which is followed by another symbol to indicate a special character. For example, "\t" represents a “tab” and "\n" is the symbol for a new line (hard return).


2 Answers

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" 
like image 188
Sacha Epskamp Avatar answered Oct 26 '22 08:10

Sacha Epskamp


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` 
like image 29
IRTFM Avatar answered Oct 26 '22 09:10

IRTFM