How to use relative paths to read data from a directory on mac in R?

I am writing code that should work for users on both Mac and Windows. All users have a local copy of the google drive directory on their computer. I have a piece of code that automatically sets the working directory to the original location of the file. Lets call this directory "directory1". Inside directory1 there is a folder called "directory2" that contains the data file.

the following part of the code works for all users using Windows machines with the working directory installed in directory1,

data = read.csv(file="directory2\\filename", header=F)

however the following code does not work, at least for some Mac users

data = read.csv(file="directory2/filename", header=F)

I confirmed that Mac users have a working directory installed in directory1, and that there is a directory2 located in their directory1, and that all spelling is correct. However, they receive a warning that directory2 does not exist. All users of this problem use Rstudio. I asked the user to include the full path, and it really worked. Can't you use relative paths on mac in Rstudio? I also tried. / And / to start the relative path, but none of them work.

+4
source share
1 answer

Have you tried data <- read.csv(file=file.path("directory2", "filename"), header=F)? file.pathshould work on different platforms.

+4
source

Source: https://habr.com/ru/post/1570494/


All Articles