'How to use relative paths to load .RData files from working directory?

I am trying to use the load() function to load an .RData file into my current R session. I open my code base by double clicking on the R project file in my directory and then attempt to load the .RData file by running load('./Data/mydata.rdata'). However, this returns a 'No such file or directory' error. I verified that the working directory is correct by using getwd().

I figure I must be using incorrect syntax because I have no issues loading the file when I type in the full file path.

Working directory for R Project file: "/Users/Me/Library/OneDrive/RStuff"

Directory containing .RData file: "/Users/Me/Library/OneDrive/RStuff/Data"

Code that works: load("/Users/Me/Library/OneDrive/RStuff/Data/mydata.rdata")

Code that fails: load('./Data/mydata.rdata')

Do relative paths not work with load() or is my syntax wrong?



Solution 1:[1]

Do relative paths not work with load() or is my syntax wrong?

It looks like there is an error with your paths. You said the working directory is "/Users/Me/Library/OneDrive/RStuff"

But according to the code that works:

load("/Users/Me/Library/OneDrive/RStuff/Data/mydata.rdata")

...the data file is in "/Users/Me/Library/OneDrive/RStuff/Data"

Therefore to use relative paths, you would use:

load("./Data/mydata.rdata")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Robert Long