Mrmoleje
Mrmoleje

Reputation: 493

R issue when moving an rmd file from one project to another (working directory issue)

I have two projects in R. I've moved an .Rmd document from project 1 to project 2.

When I used the .Rmd file which I had moved to project 2 to try and read in some data I get the following error message:

cannot open file '"mydata.csv"': No such file or directoryError in file(file, "rt") : cannot open the connection.

This kind of error usually suggests to me it's a working directory issue, however when I run getwd() in the command line it's the correct working directory that is listed and points to where the csv is stored. I've also run getwd() within the rmd doc and again the wd is correct.

Does anyone else have this experience of moving one .Rmd file to another project and then it not working in the new project?

The code in the .Rmd file that I am trying to run is:

Data <- read.csv("mydata.csv", stringsAsFactors = T) and the data is definitely within the project and has the correct title, is a csv etc.

Has anyone else seen this issue when moving an RMarkdown document into another project before?

Thanks

Upvotes: 0

Views: 1656

Answers (3)

MDEWITT
MDEWITT

Reputation: 2368

This is a great application of the here package for dealing with these types of issues.

here https://github.com/jennybc/here_here looks around the Rmd file (and all R files) for the .Rproj file and then uses that as an "anchor" to search for other files using relative references. If for instance you had the following project structure:

-data
|--mydata.csv
-src
|-00-import.R
|-01-make-graphs.R
|-02-do-analysis.R
-report
|--report.Rmd
-yourproject.Rproj

And you wanted to use mydata.csv in your report.Rmd you could do the following:

library(here)
dat <- read.csv(here("data", "mydata.csv"))

here will then convert this path to "~/Users/../data/mydata.csv" for you. Note that you have to be in the Rproject for this use case.

Here is such a great package and solves a lot of issues.

Upvotes: 1

JBGruber
JBGruber

Reputation: 12460

Maybe not relevant but it seems to be the most likely explanation for what's happening here:

The project shouldn't really interfere with your code here. When opening the project it will set your working directory to the root location of the project. However, this shouldn't matter in this case since RMarkdown files automatically set the working directory to the location where the RMarkdown file is saved. You can see this when running getwd() once in the Console and once from the RMarkdown file via run current chunk.

enter image description here

The behavior is the same when the file is knitted. So except in the case when "mydata.csv" is in the same directory as the RMarkdown file, the code above won't work.

There are two workarounds: you can use relative or absolute paths to navigate to your data file. In a project I prefer relative paths. Let's say the rmd file is in a folder called "scripts" and your data file is in a folder called "data" and both are in the same project folder. Then this should work:

Data <- read.csv("../data/mydata.csv", stringsAsFactors = TRUE)

The other option, which I do not recommend, is to set the working diretory in the rmd file via:

opts_knit$set(root.dir = '../data/')

The reason why I wouldn't do that is because the working direcotry is only changed when knitting the document but using the rmd file interactivly, you have a different working directory (the location of the rmd file)

Upvotes: 1

David_O
David_O

Reputation: 1153

This may not be the answer, but rmarkdown and knitr intentionally don't respect setwd(): the code in each block is run from the directory holding the .rmd file. So, if you've moved your .rmd file but are then using setwd() to change to the directory holding the data, that does not persist across code chunks.

If this is the issue, then one solution is to use the knitr options to set the root.dir to the data location:

opts_knit$set(root.dir = '/path/to/the/data')

See here.

Upvotes: 3

Related Questions