File path issues in R using Windows (“Hex digits in character string” error)

前端 未结 11 1933
我寻月下人不归
我寻月下人不归 2020-11-28 19:06

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=T         


        
相关标签:
11条回答
  • 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)
    }
    
    0 讨论(0)
  • 2020-11-28 19:19

    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.

    0 讨论(0)
  • 2020-11-28 19:20

    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.

    0 讨论(0)
  • 2020-11-28 19:22

    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"))
    
    0 讨论(0)
  • 2020-11-28 19:31

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

    0 讨论(0)
  • 2020-11-28 19:32

    Replacing backslash with forward slash worked for me on Windows.

    0 讨论(0)
提交回复
热议问题