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 darklord
• 6,190 points
3,542 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,130 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 by Cherukuri
• 32,260 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 in Data Analytics by anonymous
2,839 views
0 votes
1 answer

How to set global variables in R

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

answered Apr 23, 2018 in Data Analytics by kappa3010
• 2,070 points
142 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 Anmol
• 3,620 points

edited Aug 28, 2018 by Vardhan 62 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,040 points
86 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,130 points
39 views
0 votes
1 answer

Big Data transformations with R

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

answered Dec 17, 2017 in Data Analytics by Sudhir
• 1,610 points
67 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,560 points
85 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,560 points
248 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,130 points
473 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 16, 2018 in Data Analytics by DataKing99
• 8,130 points
150 views