How to set working directory to source file location in R

0 votes

I am working on various codes put in a folder. 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 any command to specify my working directory automatically in R.

May 3, 2018 in Data Analytics by Sahiti
• 6,370 points
13,953 views

2 answers to this question.

0 votes

You can try out this solution:

this.dir <- dirname(parent.frame(2)$ofile)
setwd(this.dir)

Put the above code somewhere in the file so that the working directory is changed according to that file.

answered May 3, 2018 by DataKing99
• 8,250 points
0 votes

Hi, 

I'm not sure about the command but you can try this way to see your files, directory as well as set it as your working directory.

You have an option on the right bottom of the screen to set the working directory by going to that file location or directory.

Click on files ->Then go to the file location or directory.

Then go to more and click on set as a working directory.

Hope it helps!

answered Aug 20, 2019 by Cherukuri
• 33,030 points

Related Questions In Data Analytics

0 votes
3 answers

How to write lines to a text file in R?

sink("outfile.txt") cat("hello") cat("\n" ...READ MORE

answered May 24, 2019 in Data Analytics by anonymous
19,012 views
0 votes
1 answer

How to set global variables in R

To set global variables, you can use ...READ MORE

answered Apr 24, 2018 in Data Analytics by kappa3010
• 2,090 points
1,274 views
0 votes
1 answer

How can I import a file in R without giving the destination/file path to the specified file?

You can use the window explorer to ...READ MORE

answered Aug 28, 2018 in Data Analytics by Abhi
• 3,720 points

edited Aug 28, 2018 by Vardhan 2,990 views
0 votes
1 answer

How do I convert data frame to csv file in R?

Hey @Ali, its very simple one line ...READ MORE

answered Nov 21, 2018 in Data Analytics by Maverick
• 10,840 points
2,452 views
0 votes
1 answer

How to check if a directory exists and how to create and create if doesn't exist?

You can use showWarnings = FALSE NOTE:  showWarnings ...READ MORE

answered Apr 17, 2018 in Data Analytics by DataKing99
• 8,250 points
2,577 views
0 votes
1 answer

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,570 points
1,030 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
1,178 views
0 votes
1 answer

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
5,890 views
0 votes
1 answer

How to find out the sum/mean for multiple variables per group in R?

You can use the reshape2 package for ...READ MORE

answered Apr 12, 2018 in Data Analytics by DataKing99
• 8,250 points
3,824 views
0 votes
1 answer

How to filter a data frame with dplyr and tidy evaluation in R?

Requires the use of map_df to run each model, ...READ MORE

answered May 17, 2018 in Data Analytics by DataKing99
• 8,250 points
1,897 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP