setwd in the current working dir

0 votes
I've made a folder list. Each folder has a R script that must be run on all files in the folder. I wrote the script once and then duplicated it in each folder. The issue is that I have a list of roughly 100 folders, so manually settingwd() in the current working dir is hard for me. I'm curious if it's feasible to set the current working directory with a "." for example:

Instead of inputting the folder name every time, use setwd("/User/myname/./") or any simple method to inform R the current working directory.
Jun 19, 2022 in Data Analytics by Avinash
• 1,260 points
373 views

1 answer to this question.

0 votes
# set the working directory to the main folder containing all the directories
setwd( "/user/yourdir/" )

# pull all files and folders (including subfolders) into a character vector
# keep ONLY the files that END with ".R" or ".r"
r.scripts <- list.files( pattern=".*\\.[rR]$" , recursive = TRUE )

# look at the contents.. now you've got just the R scripts..
# i think that's what you want?
r.scripts

# and you can loop through and source() each one
for ( i in r.scripts ) source( i )
answered Jun 24, 2022 by Sohail
• 3,040 points

Related Questions In Data Analytics

0 votes
1 answer

Return the current working directory in R

Use below command to fetch the current ...READ MORE

answered Aug 22, 2019 in Data Analytics by anonymous
• 33,030 points
649 views
0 votes
1 answer

Splunk Cluster instalation document was not uploaded in the cource.

Dear Learner, Greetings ! We would like to inform ...READ MORE

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,570 points
683 views
0 votes
1 answer

Plotting multiple graphs on the same page in R

If you want to plot 4 graphs ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,660 points
1,429 views
+1 vote
2 answers

Custom Function to replace missing values in a vector with the mean of values

Try this. lapply(a,function(x){ifelse(is.na(x),mean(a,na.rm = TRUE ...READ MORE

answered Aug 14, 2019 in Data Analytics by anonymous
1,940 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,035 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,185 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,892 views
0 votes
1 answer

Left Join and Right Join using "dplyr"

The below is the code to perform ...READ MORE

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

Counting the number of elements with the values of x in a vector

You may simply use the table() method: > ...READ MORE

answered Jun 14, 2022 in Data Analytics by Sohail
• 3,040 points
612 views
0 votes
1 answer

How to use the switch statement in R functions?

Switch definitely wasn't intended to operate this ...READ MORE

answered Jun 24, 2022 in Data Analytics by Sohail
• 3,040 points
590 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