使用 Windows 的 R 中的文件路径问题(“字符串中的十六进制数字”错误)

发布于 2024-12-20 06:35:37 字数 279 浏览 3 评论 0原文

我在 Windows 上运行 R,并在桌面上有一个 csv 文件。我按如下方式加载它,

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

但 R 给出以下错误消息

错误:以“C:\U”开头的字符串中没有使用十六进制数字“\U”

那么加载此文件的正确方法是什么。我使用的是Vista

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

<逆流佳人身旁 2024-12-27 06:35:37

将所有 \ 替换为 \\

它试图转义下一个字符,在本例中为 U,因此要插入 \,您需要插入转义的 \,即 \\

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 \\

赠佳期 2024-12-27 06:35:37

请不要将此响应标记为正确,因为 smitec 已正确回答。我在 .First 库中添加了一个便利函数,该函数可以将 Windows 路径转换为 ​​R 中可用的格式(Sacha Epskamp 描述的方法)。只需将路径复制到剪贴板 (ctrl + c),然后以 pathPrep() 形式运行该函数。无需争论。该路径会正确打印到控制台并写入剪贴板,以便轻松粘贴到脚本。希望这有帮助。

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)
}

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)
}
心房的律动 2024-12-27 06:35:37

解决方案

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

解释< /strong>

R 无法正确理解正常的 Windows 路径,因为 "\" 具有特殊含义 - 它用作转义字符以赋予以下字符特殊含义 (\n 对于换行符,\t 表示制表符,\r 表示回车符,...,看看这里)。

因为 R 不知道序列 \U 它会抱怨。只需将 "\" 替换为 "/" 或使用额外的 "\" 来转义 "\" > 从其特殊含义来看,一切顺利。

替代

在 Windows 上,我认为在 R 中使用 Windows 特定路径改进工作流程的最佳方法是使用允许自定义热键的 AutoHotkey:

  • 定义一个热键,例如 Cntr-Shift-V
  • 为其分配一个过程,将剪贴板中的反斜杠替换为
    slaches ...
  • 当你想将路径复制粘贴到 R 中时,你可以使用 Cntr-Shift-V 而不是 Cntr -V
  • Et-voila

AutoHotkey 代码片段 (链接到主页)

^+v::
StringReplace, clipboard, clipboard, \, /, All 
SendInput, %clipboard% 

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% 
寒尘 2024-12-27 06:35:37

我的解决方案是定义一个 RStudio 代码片段如下:

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

此代码片段将反斜杠 \ 转换为双反斜杠 \\。如果您希望将反斜杠转换为正斜杠 /,则可以使用以下版本。

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

定义首选代码段后,通过输入 p-p-TAB-ENTER 粘贴剪贴板中的路径(即 pp,然后按 Tab 键,然后 Enter)并且路径将神奇地插入 R 友好的分隔符。

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.

抽个烟儿 2024-12-27 06:35:37

运行 Windows 机器时,将反斜杠 \ 替换为正斜杠 /

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

热风软妹 2024-12-27 06:35:37

我知道这确实很旧,但如果您无论如何都要复制和粘贴,您可以使用:

read.csv(readClipboard())

readClipboard() 为您转义反斜杠。只需记住确保“.csv”包含在您的副本中,也许可以这样:

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

如果您确实想最大程度地减少输入,您可以使用一些功能:

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


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

#copy directory path
setWD()

#copy file name
df <- readCSV()

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()
以歌曲疗慰 2024-12-27 06:35:37

在 Windows 上用正斜杠替换反斜杠对我有用。

Replacing backslash with forward slash worked for me on Windows.

手心的温暖 2024-12-27 06:35:37

对于包含文本挖掘数据(语音、新闻通讯等)的 txt 文件,处理此问题的最佳方法是将“\”替换为“/”。

例子:

file<-Corpus(DirSource("C:/Users/PRATEEK/Desktop/training tool/Text Analytics/text_file_main"))

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"))
2024-12-27 06:35:37

我认为 R 正在读取字符串中的 '\' 作为转义字符。例如,\n 在字符串中创建新行,\t 在字符串中创建新制表符。

'\' 会起作用,因为 R 会将其识别为普通的反斜杠。

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.

趁微风不噪 2024-12-27 06:35:37

readClipboard() 也可以直接工作。将路径复制到剪贴板

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

然后

readClipboard()

显示为

[1] "C:\\Users\\surfcat\\Desktop\\2006_dissimilarity.csv"

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"
百合的盛世恋 2024-12-27 06:35:37

一个简单的方法是使用 python。
在 python 终端类型中

r"C:\Users\surfcat\Desktop\2006_dissimilarity.csv"
你会回来的
'C:\Users\surfcat\Desktop\2006_dissimilarity.csv'

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'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文