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

0 votes

I write R scripts that generate large amount of output.

In my view, I find it clean ti put this output in it's own directory(s).

Below is my approach to find out the existence of a directory and move into it, or create the directory and then move into it.

Is there any better approach?

Main_Directory <- "c:/Users/xyz/main/dir"
Sub_Directory <- "FinalOutputDirectory"

if (file.exists(Sub_Directory)){
    setwd(file.path(Main_Directory, Sub_Directory))
} else {
    dir.create(file.path(Main_Directory, Sub_Directory))
    setwd(file.path(Main_Directory, Sub_Directory))
}
Apr 17, 2018 in Data Analytics by nirvana
• 3,040 points
17 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

You can use showWarnings = FALSE

NOTE:  showWarnings = FALSE, hides warnings such as the directory being uncreateable.

dir.create(file.path(Main_Directory, Sub_Directory), showWarnings = FALSE)
setwd(file.path(Main_Directory, Sub_Directory))

NOTE: dir.create()  does not crash if the directory exists, but it just prints out a warning.

dir.create(file.path(Main_Directory, Sub_Directory))
setwd(file.path(Main_Directory, Sub_Directory))
answered Apr 17, 2018 by DataKing99
• 8,100 points

Related Questions In Data Analytics

0 votes
1 answer

In a dpylr pipline how to use sample and seq?

For avoiding rowwise(), I prefer to use ...READ MORE

answered Apr 6, 2018 in Data Analytics by DeepCoder786
• 1,700 points
35 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,700 points
32 views
0 votes
1 answer

How to create dummy variables based on a categorical variable of lists in R?

You can use mtabulate in the following way: library(qdapTools) cbind(data[1], ...READ MORE

answered Apr 13, 2018 in Data Analytics by CodingByHeart77
• 3,680 points
310 views
0 votes
1 answer

How to replace a value in a data frame based on a conditional 'If' statement?

It's easier to convert alpha to characters ...READ MORE

answered Jun 6, 2018 in Data Analytics by darklord
• 6,140 points
22 views
0 votes
1 answer

How to set working directory to source file location in R?

You can try out this solution: this.dir <- ...READ MORE

answered May 3, 2018 in Data Analytics by DataKing99
• 8,100 points
1,483 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
27 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,550 points
28 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,550 points
37 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,100 points
43 views
0 votes
1 answer

How to create a new R6 Class in R?

You have to first create an object ...READ MORE

answered Jul 5, 2018 in Data Analytics by DataKing99
• 8,100 points
62 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.