r - 将相对路径转换为绝对路径
问题描述
是否有一种很好的内置方法可以将相对文件路径转换为 R 中的绝对路径,而不需要文件系统上实际存在的路径?
两者都base::normalizePath
没有tools::file_path_as_absolute
在文件系统中查找物理路径,因此当路径不存在时它们都会失败:
> normalizePath('foo')
[1] "foo"
Warning message:
In normalizePath("foo") : path[1]="foo": No such file or directory
> tools::file_path_as_absolute('foo')
Error in tools::file_path_as_absolute("foo") : file 'foo' does not exist
我可以编写以下函数,它在类 Unix 系统上做得不错,但不是跨平台到 Windows 世界:
rel_to_abs <- function(x) {
# Convert `x` to absolute path - does not consult the file system
ifelse(grepl('^/', x), x, file.path(getwd(), x))
}
如果我错过了核心库中的某些内容,我很乐意被指出。
解决方案
您可以使用getAbsolutePath
该R.utils
软件包:
> R.utils::getAbsolutePath("foo")
[1] "/home/stla/Work/R/foo"
推荐阅读
- python - 如果打开现有的 Chrome 窗口,则使用默认选项的 Selenium Webdriver 永远不会返回
- java - 以编程方式将 ImageView 添加到布局
- python - 视频/图像分析以获取轮廓之间的距离
- mysql - 如何从一个表中只选择行 一个 clumns 值大于 MySQL 中的 b 表计数值
- javascript - GWT CellTable:选择一个复选框会选择表格中的每个复选框/行
- mysql - 在 mysql 中使用其他表中的 MAX 函数
- python-3.x - 使用 GRU 将 conv1D 转换为 Conv2D
- python - 仅针对特定 ID/主题进行训练/测试拆分
- javascript - 确保函数仅在 componentDidMount 或 componentDidUpdate 中调用一次
- go - /bin/sh: 1: go: 推送到heroku时找不到