27

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
Christian Hudon
  • 1,881
  • 1
  • 21
  • 42
mhermans
  • 2,097
  • 4
  • 18
  • 31
  • Can you give at least a bit more code around the problem? Where is the string coming from? How does it enter the R code? If you get the error, that is too late, and that error occurs as far as I'm concerned exactly at the point where the string goes into R. – Joris Meys Jan 14 '11 at 11:05

4 Answers4

16

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"
Sacha Epskamp
  • 46,463
  • 20
  • 113
  • 131
  • 2
    allowEscapes=FALSE is the default so one could abbreviate that to `x <- scan(what = "")` – G. Grothendieck Jan 13 '11 at 22:57
  • 3
    Scan() seems to be an interactive command. Is it possible to use in a script, triggered by a dialog (thus run in the background)? – mhermans Jan 14 '11 at 06:32
  • If you put the scan command followed by the input followed by a blank line in a script `abc.R` and then run `Rscript abc.R` at the operating system's command line it will work but it won't work if you use `source("abc.R")` from within R. – G. Grothendieck Jan 14 '11 at 12:28
  • If the file name has spaces in it, the `scan` considers there to be multiple inputs. Reasonable behavior, but annoying for me because one of my folders has a space in it. – Nate Anderson Sep 04 '16 at 15:59
  • I use this to import ASCII art into my applicaitons. – MadmanLee May 01 '19 at 17:39
10

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.

drf
  • 8,461
  • 32
  • 50
5

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`
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • 2
    But R interprets "C:\\Users\\mhermans\\somefile.csv" in the same way as "C:/Users/mhermans/somefile.csv" right? The problem lies in reading a string with single backslashes and turning it in one of those two solutions. – Sacha Epskamp Jan 13 '11 at 23:40
  • No. You don't understand. There were only single back-slashes in the string I created. You need to understand the difference between representation and reality. "\\" is not "/" . Try `nchar("\\")` for further progress toward illumination. – IRTFM Jan 14 '11 at 04:48
  • 1
    I indeed have a string with single quotes as input, not double quotes. In this case the reality according to R is "C:Usershermans", represented by "C:\Users\mhermans". I need R to realize "C:\Users\mhermans" is the reality. – mhermans Jan 14 '11 at 06:36
  • Please offer the output of dput() on that string. That really the way to communicate the internal structure unambiguously. – IRTFM Jan 14 '11 at 13:55
  • 1
    "You needed to double them all" -- how can I do this? In my case I copied the path from Windows Explorer and have the string (with single backslashes) in my paste clipboard. Must I manually edit the string first? Or can I use an R trick? I know I can always set the folder as working directory in RStudio using a UI, but I am curious anyway... – Nate Anderson Sep 04 '16 at 16:03
  • No, you do not need to edit it first, but .... it will be displayed with 2 backslashes if you print it despite only having single backslashes internally – IRTFM May 31 '19 at 19:47
1

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"))
bill_080
  • 4,692
  • 1
  • 23
  • 30
  • That seems to be a possibility, if I write out the string I receive as input from the dialog box to a temporary file, and read it back in. Is there a way to do this without the write/read step? – mhermans Jan 14 '11 at 06:38
  • @mhermans: `readLines` can take input from any connection, not just from files. Where do you get your string from? – Richie Cotton Jan 14 '11 at 10:12
  • @mhermans: As Richie says above, it can take various connections. From your comment about a "dialog box", you might try copying the string to the clipboard. Then use texinp <- suppressWarnings(readLines("clipboard")) – bill_080 Jan 14 '11 at 16:45