Raw text strings for file paths in R

A slightly different approach I use with a custom made function that takes a windows path and corrects it for R.

pathPrep <- function() {                        
    cat("Please enter the path:\\n\\n")         
    oldstring <- readline()                     
    chartr("\\\\", "/", oldstring)              
}                                               

Let's try it out!

When prompted paste the path into console or use ctrl + r on everything at once

(x <- pathPrep())                      
C:/Users/Me/Desktop/SomeFolder/example.csv      

Now you can feed it to a function

shell.exec(x) #this piece would work only if    
              #  this file really exists in the 
              #  location specified  

But as others pointed out what you want is not truly possible.


  1. If E:\DATA\example.csv is on the clipboard then do this:

     example.csv <- scan("clipboard", what = "")
     ## Read 1 item
     example.csv
     ## [1] "E:\\DATA\\example.csv"
    

Now you can copy "E:\\DATA\\example.csv" from the above output above onto the clipboard and then paste that into your source code if you need to hard code the path.

Similar remarks apply if E:\DATA\example.csv is in a file.

  1. If the file exists then another thing to try is:

    example.csv <- file.choose()

and then navigate to it and continue as in 1) above (except the file.choose line replaces the scan statement there).

  1. Note that its not true that you need to change the backslashes to forward slashes for read.csv on Windows but if for some reason you truly need to do that translation then if the file exists then this will translate backslashes to forward slashes (but if it does not exist then it will give an annoying warning so you might want to use one of the other approaches below):

    normalizePath(example.csv, winslash = "/")

and these translate backslashes to forward slashes even if the file does not exist:

gsub("\\", "/", example.csv, fixed = TRUE)
## [1] "E:/DATA/example.csv"

or

chartr("\\", "/", example.csv)
## [1] "E:/DATA/example.csv"
  1. In 4.0+ the following syntax is supported. ?Quotes discusses additional variations.

     r"{E:\DATA\example.csv}"
    

EDIT: Added more info on normalizePath. EDIT: Added (4).


It is now possible with R version 4.0.0. See ?Quotes for more.

Example

r"(c:\Program files\R)"
## "c:\\Program files\\R"

You can use file.path to construct the correct file path, independent of operating system.

file.path("E:", "DATA", "example.csv")
[1] "E:/DATA/example.csv"

It is also possible to convert a file path to the canonical form for your operating system, using normalizePath:

zz <- file.path("E:", "DATA", "example.csv")
normalizePath(zz)
[1] "E:\\DATA\\example.csv"

But in direct response to your question: I am not aware of a way to ignore the escape sequence using R. In other words, I do not believe it is possible to copy a file path from Windows and paste it directly into R.

However, if what you are really after is a way of copying and pasting from the Windows Clipboard and get a valid R string, try readClipboard

For example, if I copy a file path from Windows Explorer, then run the following code, I get a valid file path:

zz <- readClipboard()
zz
[1] "C:\\Users\\Andrie\\R\\win-library\\"

Tags:

String

R