string R 中文件路径的原始文本字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8197027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Raw text strings for file paths in R
提问by djq
Is it possible to use a prefix when specifying a filepath string in R to ignore escape characters?
在 R 中指定文件路径字符串时是否可以使用前缀来忽略转义字符?
For example if I want to read in the file example.csv
when using windows, I need to manually change \
to /
or \\
. For example,
例如,如果我想在文件中读取example.csv
使用Windows的时候,我需要手动更改\
到/
或\\
。例如,
'E:\DATA\example.csv'
becomes
变成
'E:/DATA/example.csv'
data <- read.csv('E:/DATA/example.csv')
In python
I can prefix my string using r
to avoid doing this (e.g. r'E:\DATA\example.csv'
). Is there a similar command in R
, or an approach that I can use to avoid having this problem. (I move between windows, mac and linux - this is just a problem on the windows OS obviously).
在python
我可以使用前缀我的字符串r
来避免这样做(例如r'E:\DATA\example.csv'
)。中是否有类似的命令R
,或者我可以使用的方法来避免出现此问题。(我在 windows、mac 和 linux 之间移动 - 这显然只是 windows 操作系统上的一个问题)。
回答by Andrie
You can use file.path
to construct the correct file path, independent of operating system.
您可以使用file.path
构建正确的文件路径,独立于操作系统。
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
:
也可以使用以下命令将文件路径转换为操作系统的规范格式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.
但是直接回答您的问题:我不知道有什么方法可以使用 R 忽略转义序列。换句话说,我不相信可以从 Windows 复制文件路径并将其直接粘贴到 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
但是,如果您真正想要的是一种从 Windows 剪贴板复制和粘贴并获得有效 R 字符串的方法,请尝试 readClipboard
For example, if I copy a file path from Windows Explorer, then run the following code, I get a valid file path:
例如,如果我从 Windows 资源管理器复制文件路径,然后运行以下代码,我会得到一个有效的文件路径:
zz <- readClipboard()
zz
[1] "C:\Users\Andrie\R\win-library\"
回答by G. Grothendieck
1) If E:\DATA\example.csv
is on the clipboard then do this:
1)如果E:\DATA\example.csv
在剪贴板上,则执行以下操作:
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.
现在,您可以"E:\\DATA\\example.csv"
从上面的输出复制到剪贴板,然后将其粘贴到源代码中(如果您需要对路径进行硬编码)。
Similar remarks apply if E:\DATA\example.csv
is in a file.
如果E:\DATA\example.csv
是在文件中,则类似的说明适用。
2) If the file exists then another thing to try is:
2)如果文件存在,那么另一件要尝试的事情是:
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) 一样继续(除了该file.choose
行替换了scan
那里的语句)。
3) 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):
3)请注意,您需要read.csv
在 Windows 上将反斜杠更改为正斜杠是不正确的,但是如果由于某种原因您确实需要进行该转换,那么如果文件存在,那么这会将反斜杠转换为正斜杠(但如果确实如此)不存在,那么它会给出一个恼人的警告,所以你可能想使用下面的其他方法之一):
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"
EDIT: Added more info on normalizePath
.
编辑:添加了有关normalizePath
.
回答by Tyler Rinker
A slightly different approach I use with a custom made function that takes a windows path and corrects it for R.
我使用的一种稍微不同的方法与定制函数一起使用,该函数采用 Windows 路径并针对 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
出现提示时,将路径粘贴到控制台中或立即对所有内容使用 ctrl + r
(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.
但是正如其他人指出的那样,您想要的并不是真正可能的。
回答by Michael Hoffman
No, this is not possible with R versions before 4.0.0. Sorry.
不,这对于 4.0.0 之前的 R 版本是不可能的。对不起。
回答by Caleb Fitzgerald
回答by Matt
Here's an incredibly ugly one-line hack to do this in base R, with no packages necessary:
这是在基本 R 中执行此操作的令人难以置信的丑陋的单行 hack,无需任何包:
setwd(gsub(", ", "", toString(paste0(read.table("clipboard", sep="\", stringsAsFactors=F)[1,], sep="/"))))
Usable in its own little wrapper function thus (using suppressWarnings
for peace of mind):
因此可以在它自己的小包装函数中使用(suppressWarnings
为了安心使用):
> getwd()
[1] "C:/Users/username1/Documents"
> change_wd=function(){
+ suppressWarnings(setwd(gsub(", ", "", toString(paste0(read.table("clipboard", sep="\", stringsAsFactors=F)[1,], sep="/")))))
+ getwd()
+ }
Now you can run it:
现在你可以运行它:
#Copy your new folder path to clipboard
> change_wd()
[1] "C:/Users/username1/Documents/New Folder"
回答by Matthew Strasiotto
To answer the actual question of "Can I parse raw-string in R without having to double-escape backslashes?" which is a good question, and has a lot of uses besides the specific use-case with the clipboard.
回答“我可以在 R 中解析原始字符串而不必双重转义反斜杠吗?”的实际问题。这是一个很好的问题,除了剪贴板的特定用例外,还有很多用途。
I have found a package that appears to provide this functionality:
我找到了一个似乎提供此功能的包:
https://github.com/trinker/pathr
https://github.com/trinker/pathr
See "win_fix". The use-case specified in the docs is exactly the use-case you just stated, however I haven't investigated whether it handles more flexible usage scenarios yet.
参见“win_fix”。文档中指定的用例正是您刚才所说的用例,但是我还没有调查它是否可以处理更灵活的使用场景。