Post

3 followers Follow
3
Avatar

Make "Set working directory to source file location" useable as an r-function

At the moment, I tell my colleagues to "Set working directory to source file location" in Rstudio before executing the scripts that I send them (which have paths relative to source file location, the most sensible thing).

However, when I or they switch from tab to tab and have an incorrect working directory set, we forget about this and get path problems. It would be nice if I could execute the Function Rstudio.setwdtosrcfile() and be certain that the relative paths will work – resorting to the GUI is so bothersome too.

Ian Pylvainen

5 comments

0
Avatar

May be a better solution would be for Rstudio to default to source(..., chdir = TRUE) - I think this is recommended practice for running scripts.

Ian Pylvainen 0 votes
Comment actions Permalink
1
Avatar

That won't work for users who execute line-by-line, switch between tabs etc.
I'm looking for a solution, where I can be fairly sure that non-technical types will get the paths right, no matter how they choose to execute the code (knit it, exec all knitr chunks, line-by-line, source, …).
Smart defaults will likely never suit every single application.
I think it would be a nice IDE feature to have this oft-used command be accessible programmatically (like you do with "View()").
Obviously this would break code in other IDEs, but I for one am happy to be locked in here with you guys ;-)

Ian Pylvainen 1 vote
Comment actions Permalink
0
Avatar

That would be something I am looking for.

I searched around and haven't found a nice solution to at least detect the directory that the current script is in. Hope that function can be added.

Ian Pylvainen 0 votes
Comment actions Permalink
0
Avatar

Speaking as a high school stats teacher planning to use R in the classroom, I'll second this suggestion. It would be very useful.

I know that R has the projects feature, which is tremendously useful for larger projects & more experienced users. However, there are multiple files involved, which makes a project harder to share.

I also know that R has a behaviour that, when launched from double-clicking an .Rmd file for example, it will set the current working directly to the directory that the .Rmd file is located in. This is handy, but as Ian noted, becomes more problematic when students may have multiple Rmd files open in a particular tab.

For reproducibility and ease of sharing single .Rmd files with others (including over Twitter) it would be terrific to have the functionality that Ian has suggested.

Further details about how this would be useful... a typical .Rmd file that I write:

1) fetches a data file from the Internet
2) cleans up the data if necessary
3) saves the data frame to a CSV file

The next time that .Rmd file is loaded, it will check first to see if a local CSV file was created on a previous run. This saves "touching" the network again to get the same data, which is both faster for the user of the Rmd file, and a more polite use of bandwidth on the part of the web server involved.

So... having a line in the .Rmd file that could ensure the working directory is set to the same directory that the .Rmd file exists in would be tremendously useful for this workflow.

Russell Gordon 0 votes
Comment actions Permalink
0
Avatar

Replace my references to "R" with "R Studio" in the previous answer, sorry to conflate the two! I think most readers will know what I mean, regardless.

Russell Gordon 0 votes
Comment actions Permalink