Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in R Programming by (5.3k points)

I am working out some tutorials in R. Each R code is contained in a specific folder. There are data files and other files in there. I want to open the .r file and source it such that I do not have to change the working directory in Rstudio as shown below:

enter image description here

Is there a way to specify my working directory automatically in R.

1 Answer

0 votes
by
edited by

To set the working directory to the source file location, you can do the following:

To get the source file directory:

dirname(rstudioapi::getActiveDocumentContext()$path)

To set the working directory to source file directory:

setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...