File path issues in R using Windows ("Hex digits in character string" error)
Asked Answered
W

11

93

I run R on Windows, and have a csv file on the Desktop. I load it as follows,

x<-read.csv("C:\Users\surfcat\Desktop\2006_dissimilarity.csv",header=TRUE)

but the R gives the following error message

Error: '\U' used without hex digits in character string starting "C:\U"

So what's the correct way to load this file. I am using Vista

Whipstock answered 8/12, 2011 at 2:30 Comment(0)
M
135

replace all the \ with \\.

it's trying to escape the next character in this case the U so to insert a \ you need to insert an escaped \ which is \\

Millwork answered 8/12, 2011 at 2:32 Comment(2)
This is true simply replace \ with \\ and the script runs successfully. Thanks Smit!!Gerber
This is a better answer. Path from Windows in r like C:/Users/... work and is less confusing than \\. The only situation where you will want the path to be with \ is when you do a shell() call like shell("cd C:\\Users\\ && do something")Receptive
L
29

Please do not mark this response as correct as smitec has already answered correctly. I'm including a convenience function I keep in my .First library that makes converting a windows path to the format that works in R (the methods described by Sacha Epskamp). Simply copy the path to your clipboard (ctrl + c) and then run the function as pathPrep(). No need for an argument. The path is printed to your console correctly and written to your clipboard for easy pasting to a script. Hope this is helpful.

pathPrep <- function(path = "clipboard") {
    y <- if (path == "clipboard") {
        readClipboard()
    } else {
        cat("Please enter the path:\n\n")
        readline()
    }
    x <- chartr("\\", "/", y)
    writeClipboard(x)
    return(x)
}
Lisp answered 8/12, 2011 at 3:1 Comment(2)
I was about to write this and I'm glad I checked first. Thanks a ton. I'm really surprised this hasn't leaked into one of the more popular packages out there (unless I'm just missing it). I'm going to be using this in my script so I'll post the slight variation when I'm done.Calton
It's now in the reports development package and will be pushed to CRAN eventually. See WP (windows path) in the reports dev package.Lisp
C
12

Solution

Try this: x <- read.csv("C:/Users/surfcat/Desktop/2006_dissimilarity.csv", header=TRUE)

Explanation

R is not able to understand normal windows paths correctly because the "\" has special meaning - it is used as escape character to give following characters special meaning (\n for newline, \t for tab, \r for carriage return, ..., have a look here ).

Because R does not know the sequence \U it complains. Just replace the "\" with "/" or use an additional "\" to escape the "\" from its special meaning and everything works smooth.

Alternative

On windows, I think the best thing to do to improve your workflow with windows specific paths in R is to use e.g. AutoHotkey which allows for custom hotkeys:

  • define a Hotkey, e.g. Cntr-Shift-V
  • assigns it an procedure that replaces backslashes within your Clipboard with slaches ...
  • when ever you want to copy paste a path into R you can use Cntr-Shift-V instead of Cntr-V
  • Et-voila

AutoHotkey Code Snippet (link to homepage)

^+v::
StringReplace, clipboard, clipboard, \, /, All 
SendInput, %clipboard% 
Clercq answered 15/1, 2013 at 10:2 Comment(0)
B
7

My Solution is to define an RStudio snippet as follows:

snippet pp
    "`r gsub("\\\\", "\\\\\\\\\\\\\\\\", readClipboard())`"

This snippet converts backslashes \ into double backslashes \\. The following version will work if you prefer to convert backslahes to forward slashes /.

snippet pp
    "`r gsub("\\\\", "/", readClipboard())`"

Once your preferred snippet is defined, paste a path from the clipboard by typing p-p-TAB-ENTER (that is pp and then the tab key and then enter) and the path will be magically inserted with R friendly delimiters.

Bywaters answered 14/11, 2016 at 4:45 Comment(1)
I didn't know that snippets existed in R Studio until I saw this. This is awesome.Dodd
G
3

Replace back slashes \ with forward slashes / when running windows machine

Govea answered 16/1, 2018 at 11:53 Comment(0)
C
3

I know this is really old, but if you are copying and pasting anyway, you can just use:

read.csv(readClipboard())

readClipboard() escapes the back-slashes for you. Just remember to make sure the ".csv" is included in your copy, perhaps with this:

read.csv(paste0(readClipboard(),'.csv'))

And if you really want to minimize your typing you can use some functions:

setWD <- function(){
  setwd(readClipboard())
}


readCSV <- function(){
  return(readr::read_csv(paste0(readClipboard(),'.csv')))
} 

#copy directory path
setWD()

#copy file name
df <- readCSV()
Chromatograph answered 30/11, 2018 at 16:53 Comment(0)
T
2

Replacing backslash with forward slash worked for me on Windows.

Tarazi answered 29/11, 2018 at 23:8 Comment(0)
F
1

The best way to deal with this in case of txt file which contains data for text mining (speech, newsletter, etc.) is to replace "\" with "/".

Example:

file<-Corpus(DirSource("C:/Users/PRATEEK/Desktop/training tool/Text Analytics/text_file_main"))
Faceharden answered 12/11, 2017 at 17:34 Comment(0)
E
0

I think that R is reading the '\' in the string as an escape character. For example \n creates a new line within a string, \t creates a new tab within the string.

'\' will work because R will recognize this as a normal backslash.

Enlistee answered 21/11, 2014 at 23:12 Comment(0)
G
0

readClipboard() works directly too. Copy the path into your clipboard

C:\Users\surfcat\Desktop\2006_dissimilarity.csv

Then

readClipboard()

appears as

[1] "C:\\Users\\surfcat\\Desktop\\2006_dissimilarity.csv"
Gong answered 16/1, 2019 at 12:46 Comment(0)
R
-6

A simple way is to use python. in python terminal type

r"C:\Users\surfcat\Desktop\2006_dissimilarity.csv" and you'll get back 'C:\Users\surfcat\Desktop\2006_dissimilarity.csv'

Ruprecht answered 2/6, 2015 at 5:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.