在我的 Rscripts 中,通常,我使用最简单的方式来加载文件。
data1 <- read.table("~/user/document/data/cache/distances.dat", quote="\"", comment.char="")
但是我想在其他电脑上使用这个脚本,所以路径会改变。
缓存目录始终存在于文件距离中。在我的 Bash 脚本中,我使用它来避免这个问题:
WORKING_DIRECTORY=`pwd`/cache
但我不知道如何在 R 中使用它。
我想要的是这样的:
data1 <- read.table("'pwd'/cache/distances.dat", quote="\"", comment.char="")
最佳答案
您可以使用 getwd()
和 file.path
来获取文件的路径:
file.path(getwd(), "cache/distances.dat")
在你的情况下:
data1 <- read.table(file.path(getwd(), "cache/distances.dat"), quote="\"", comment.char="")
关于r - 如何在 R 中使用命令 "pwd"加载数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38902954/