Jim Raynor Jim Raynor - 16 days ago 6
R Question

Automatically finding the path of current R project in R Studio

Say I have an R project opened at the following directory

/home/user/workarea/project1
. Now I have a nested directory structure underneath and I am writing a function that is supposed to perform logging from anywhere inside this project and I want this function to write into files that are located at the directory
/home/user/workarea/project1/log
. I plan to pass the file path as an argument to this function. Now I know that this file will always be located under the /log directory in the project's main directory - but how do I automatically find out which is my project directory, so that I don't always specify relative paths with
..
. I wouldn't want to hard-code my local path
/home/user/workare/project1
, but be able to detect this wherever I clone my project.

Answer

It sounds like the rprojroot package will provide what you're looking for -- in particular, the find_rstudio_root_file() function should provide what you need.