Enrique Enrique - 1 year ago 39
R Question

How can I use the command "pwd" in R to load data?

In my Rscripts, normally, I use the easiest way to load files.

data1 <- read.table("~/user/document/data/cache/distances.dat", quote="\"", comment.char="")

But I would like to use this script in other computer, therefore the path will change.

The cache directory always be there with the file distances. In my Bash script I'm using this to avoid this problem:


But I have no idea how can I use that in R.

What I would like is something like:

data1 <- read.table("'pwd'/cache/distances.dat", quote="\"", comment.char="")

Answer Source

You can use getwd() with the file.path to get the path to your files:

file.path(getwd(), "cache/distances.dat")

In your case:

data1 <- read.table(file.path(getwd(), "cache/distances.dat"), quote="\"", comment.char="")