Problems with file paths in R using Windows (error "hexadecimal digits in a character string")

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 R gives the following error message

Error: '\ U' is used without hexadecimal digits in the character string, starting with "C: \ U"

So what is the right way to download this file. I am using Vista p>

+68
r filepath backslash
Dec 08 '11 at 2:30
source share
11 answers

replace all \ with \\ .

it tries to avoid the next character in this case U , so to insert \ you need to paste the escape code \ , which is equal to \\

+103
Dec 08 '11 at 2:32
source share

Please do not mark this answer as correctly as smitec already answered correctly. I turn on the convenience function, which I store in my .First library, which converts the Windows path to a format that works in R (the methods described by Sacha Epskamp). Just copy the path to the clipboard (ctrl + c) and then run the function as pathPrep() . No need for an argument. The path is printed to the console correctly and written to the clipboard for easy pasting into the script. Hope this will be 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) } 
+26
Dec 08 '11 at 3:01
source share

Decision

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

Explanation

R cannot correctly understand the normal Windows paths because "\" has a special meaning - it is used as an escape character to give the following characters special values ​​( \n for a new line, \t for a tab, \r for carriage return ,. .. see here ).

Since R does not know the \U sequence, she complains. Just replace "\" with "/" or use the extra "\" to avoid the "\" from its special meaning, and everything works smoothly.

Alternative

In windows, I find it best to improve the workflow with specific Windows paths in R, to use, for example. AutoHotkey, which allows you to configure hotkeys:

  • define a hotkey, for example. Cntr - Shift - V
  • assigns it a procedure that replaces the backslash in your clipboard slaches ...
  • when you want to copy the path insert into R, you can use Cntr - Shift - V instead of Cntr - V
  • Et voila

AutoHotkey code snippet (link to the main page)

 ^+v:: StringReplace, clipboard, clipboard, \, /, All SendInput, %clipboard% 
+8
Jan 15 '13 at 10:02
source share

My solution is to define an RStudio fragment as follows:

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

This snippet converts a backslash \ to a double backslash \\ . The next version will work if you prefer to convert backslashes to slashes / .

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

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

+4
Nov 14 '16 at 4:45
source share

The best way to handle this in the case of a txt file that 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")) 
+1
Nov 12 '17 at 17:34 on
source share

Replace backslashes \ with slashes / when working with a window machine

+1
Jan 16 '18 at 11:53
source share

I know this is really old, but if you copy and paste anyway, you can simply use:

 read.csv(readClipboard()) 

readClipboard () removes backslashes for you. Remember to make sure that ".csv" is included in your copy, perhaps with this:

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

And if you really want to minimize text input, 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() 
+1
Nov 30 '18 at 16:53
source share

I think R reads "\" in the string as an escape character. For example, \ n creates a new line inside a line, \ t creates a new tab inside a line.

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

0
Nov 21 '14 at 23:12
source share

Replacing a backslash with a slash for me worked on Windows.

0
Nov 29 '18 at 23:08
source share

readClipboard() also works directly. Copy path to clipboard

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

then

 readClipboard() 

displayed as

 [1] "C:\\Users\\surfcat\\Desktop\\2006_dissimilarity.csv" 
0
Jan 16 '19 at 12:46
source share

An easy way is to use python. in python terminal terminal

r "C: \ Users \ surfcat \ Desktop \ 2006_dissimilarity.csv" and you will return 'C: \ Users \ surfcat \ Desktop \ 2006_dissimilarity.csv

-3
Jun 02 '15 at 5:43 on
source share



All Articles