0

I have an R.proj file called Food_Choices.Rproj that is supposed to be setting my working directory to ~/Desktop/Food_Choices, a folder containing reproducibility files according to the TIER system like

enter image description here

But it's not setting the working directory properly, because when I knit my processing file with code like this

food<-read_csv("Original_Data/food_coded.csv")

#imagine some processing code in between here

write.csv(food, file = "Analysis-Data/analysis_data.csv")

I get this error:

Error: 'Original_Data/food_coded.csv' does not exist in current working directory ('/Users/IdanCarre/Desktop/Food_Choices/Command_Files').

Which is not the project directory, it's the directory of the processing file!

I thought I set the working directory when I opened the files in the context of the R project, but that doesn't seem to be happening anymore (even though my files from a year ago with the same setup still work??)

NOTE: I don't want to use

library(knitr)
opts_knit$set(root.dir = '/Users/IdanCarre/Desktop/Food_Choices')

Because then new users who want to reproduce the results have to go manually insert their own directory into each file they want to run. That's a lot of work they shouldn't have to do.

UPDATE TO COMMENTS:

I used the here package, and that works satisfactorily for read.csv (it throws a data column de-duplication warning but I think it's probably okay for now), but when I write out the processed data file to the analysis data folder, I'm trying to use

write.csv(food, file = here("Analysis-Data", "analysis_data.csv"))

And the error I get is

Error in file(file, ifelse(append, "a", "w")) : cannot open the connection

I get this same problem if I use

write.csv(food, file = "Analysis-Data/analysis_data.csv")
  • What exactly is the "processing file"? Do you mean the Rmd file? Or how are you knitting this file exactly? – MrFlick Nov 07 '19 at 02:26
  • Have you tried creating a new project with a folder and a file with just these two lines? Does it happen in that case too? – MrFlick Nov 07 '19 at 02:41
  • 1
    I believe it might be the default RStudio setting: Tools -> Global Options -> R Markdown -> Evaluate chunks in directory -> Document, which sets the working directory for code in an Rmd to the directory that the Rmd is in. In particular, using an R script will not show this behaviour. See the `here` package for a way to always write paths relative to the project root. – Calum You Nov 07 '19 at 03:04
  • Note that you can override this in the project settings as well, so when you share the project the issue shouldn't arise even without the use of `here` – Calum You Nov 07 '19 at 03:08
  • Yes, the processing file is an Rmd file. The project I made for testing purposes only has these two lines. When I navigate to the Global options and I change Directory -> Document to Directory -> Project, and choose Apply, the option changes itself back to Directory -> Document when I re-open global settings. – Idan Carre Nov 07 '19 at 12:57
  • Okay now that I better understand here, write.csv(food, file = here("AnalysisData/analysis_data.csv")) seems to be working. For now this will solve the problem. How do I close out this post? – Idan Carre Nov 08 '19 at 22:07

0 Answers0