10

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.

7
  • 2
    get working directory? getwd() Nov 20, 2016 at 22:19
  • 2
    That would assume that I never change the working directory from the main project folder correct?
    – Jim Raynor
    Nov 20, 2016 at 22:21
  • Im not sure Im clear on your question but getwd just spits out your current wd -emphasis on current. So if you have multiple subdirectories in your project directory, the default -when you open up a project -will be the main project's directory unless you've manually set a new working directory. I think. Nov 20, 2016 at 22:24
  • You might want to check out the rprojroot package. Nov 20, 2016 at 23:02
  • 4
    @KevinUshey Awesome! The function rprojroot::find_rstudio_root_file() is exactly what I was looking for. Why don't you set it as an answer and I will accept it.
    – Jim Raynor
    Nov 20, 2016 at 23:15

2 Answers 2

9

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.

0
4

Also here::here() would do the job in this case!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.