在我的Rscripts中,通常,我使用最简单的方法来加载文件.
data1 <- read.table("~/user/document/data/cache/distances.dat", quote="\"", comment.char="")
Run Code Online (Sandbox Code Playgroud)
但是我想在其他计算机上使用这个脚本,因此路径会改变.
缓存目录始终与文件距离一致.在我的Bash脚本中,我使用它来避免这个问题:
WORKING_DIRECTORY=`pwd`/cache
Run Code Online (Sandbox Code Playgroud)
但我不知道如何在R中使用它.
我想要的是:
data1 <- read.table("'pwd'/cache/distances.dat", quote="\"", comment.char="")
Run Code Online (Sandbox Code Playgroud)
您可以使用getwd()它file.path来获取文件的路径:
file.path(getwd(), "cache/distances.dat")
Run Code Online (Sandbox Code Playgroud)
在你的情况下:
data1 <- read.table(file.path(getwd(), "cache/distances.dat"), quote="\"", comment.char="")
Run Code Online (Sandbox Code Playgroud)