8

Typical example:

path <- "C:/test/path" # great
path <- "C:\\test\\path" # also great
path <- "C:\test\path"

Error: '\p' is an unrecognized escape in character string starting ""C:\test\p"

(of course - \t is actually an escape character.)

Is there any mark that can be used to treat the string as verbatim? Or can it be coded?

It would be really useful when copy/pasting path names in Windows...

meow
  • 925
  • 7
  • 22
  • 2
    Possibly same as [this Question](http://stackoverflow.com/questions/4685737/ignore-escape-characters-backslashes-in-r-strings) – 9Heads Oct 27 '16 at 10:39
  • Is there a way to prevent an R string to consider backslash as the start of an escape character? (In my use case, I need to prevent a file read and print from showing an error because there is a backslash followed by some character in the text) – nikpod Jan 11 '18 at 03:00

4 Answers4

5

R 4.0.0 introduces raw strings:

dir <- r"(c:\Program files\R)"

https://stat.ethz.ch/R-manual/R-devel/library/base/html/Quotes.html

https://blog.revolutionanalytics.com/2020/04/r-400-is-released.html

meow
  • 925
  • 7
  • 22
0

You can use scan ( but only in interactive session -- not in source)

Like

path=scan(what="",allowEscapes=F,nlines=1)
C:\test\path
print(path)

And then Ctrl+A ++ Ctrl+Enter
give you result

But not work in function or source :

{
path=scan(what="character",allowEscapes=F,nlines=1)
C:\test\path
print(path)
}

throw error

Batanichek
  • 7,761
  • 31
  • 49
0

Maybe readline() or scan(what = "charactor"), both work in terminal, not script or function:

1.readline():

> path <- readline()
C:\test\path #paste your path, ENTER
> path
[1] "C:\\test\\path"

2.scan(what = "charactor"):

> path = scan(what = "character")
1: C:\test\path #paste, ENTER
2: #ENTER
#Read 1 item
> path
[1] "C:\\test\\path"

EDIT:

Try this:

1.Define a function getWindowsPath():

> getWindowsPath <- function() #define function 
  {
    return(scan(file = "clipboard", what = "character"))
  }

2.Copy windows path using CTRL+C:

#CTRL+C: C:\test\path  
> getWindowsPath()
#Read 1 item
[1] "C:\\test\\path"
xtluo
  • 1,961
  • 18
  • 26
0

If you are copying and pasting in windows, you can set up a file connection to the clipboard. Then you can use scan to read from it, with allowEscapes turned off. However, Windows allows spaces in file paths, and scan doesn't understand that, so you have to wrap the result in paste0 with collapse set to a 0-length character string.

x = file(description = "clipboard")
y = paste0(scan(file = x, what = "character", allowEscapes = F), collapse = "")

Unfortunately, this only works for the path currently in the clipboard, so if you are copying and pasting lots of paths into an R script, this is not a solution. A workaround in that situation would be to paste each path into a separate text file and save it. Then, in your main script, you could run the following

y = paste0(scan(file = "path1.txt", what = "character", allowEscapes = F), collapse = "")

You would probably need one saved file for each path.

Paul de Barros
  • 1,170
  • 8
  • 22