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,060 points
24 views

1 answer to this question.

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,130 points

Related Questions In Data Analytics

0 votes
0 answers

How to edit the labels and limit if a plot using ggplot? - R

How to edit the labels and limit ...READ MORE

Jul 15 in Data Analytics by piyush
12 views
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
76 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
70 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
503 views
0 votes
2 answers

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

Hi,  I'm not sure about the command but you ...READ MORE

answered Aug 20 in Data Analytics by Cherukuri
• 25,900 points
2,366 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
47 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
63 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
127 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
94 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,130 points
118 views