以编程方式加载文件 [英] Programmatically load files

查看:30
本文介绍了以编程方式加载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想将文件加载到 R 中时,有什么方法可以通过编程(或半编程)而不是手动复制粘贴路径并将 \ 切换到 / 所以 R 可以读取它(因为 \ 是 R 中的转义字符)

When I want to load a file into R, what would be a way to do so programmatically (or semi programmatically) instead of just manually copy paste the path and switch \ to / so R could read it (because \ is an escape character in R)

@Roland 提到的一种方法是使用 scan

One method mentioned by @Roland is using scan

list.files(scan(file=file(description = "clipboard"), what = "character", allowEscapes = FALSE))

如果路径包含空格,则需要修改

Which will need modifications if the path contains a space like

C:\用户\大卫阿伦堡\"

"C:\Users\David Arenburg\"

推荐答案

normalizepath 也许你在找什么:

> normalizePath(c(R.home(), tempdir()))
[1] "C:\\Program Files\\R\\R-3.1.0"                    
[2] "C:\\Users\\john\\AppData\\Local\\Temp\\RtmpysKuFi"
> normalizePath(c(R.home(), tempdir()), winslash = '/')
[1] "C:/Program Files/R/R-3.1.0"                 
[2] "C:/Users/john/AppData/Local/Temp/RtmpysKuFi"

如果您使用的是 Windows 并且在复制和粘贴文件路径时遇到问题,请使用 readClipboard.例如,我从 Windows 文件夹中复制了一个文件路径,因此它位于剪贴板上,然后我可以使用:

Use readClipboard if you are on windows and have problems copying and pasting filepaths. For example I copy a file path from a windows folder so it is on the clip board then I can use:

> readClipboard()
[1] "C:\\Users\\john\\AppData\\Local\\Temp"

然后您可以使用 `normalizePath 来纠正路径的 Windows 风格.

You can then use `normalizePath to correct the windows flavour of paths.

这篇关于以编程方式加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆