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
35 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
1 answer

How to check if a file already exists or not in R?

Check out file.exists() function!! The function file.exists() returns a ...READ MORE

answered Oct 29 in Data Analytics by Cherukuri
• 31,840 points
22 views
0 votes
1 answer

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

Add a limit to axis ticks using ...READ MORE

answered Nov 3 in Data Analytics by anonymous
• 31,840 points
19 views
0 votes
1 answer

How to check if a element is present in the list?

There are multiple functions for this purpose ...READ MORE

answered Oct 28 in Data Analytics by Cherukuri
• 31,840 points
15 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,720 points
87 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
• 31,840 points
3,155 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
58 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
77 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
187 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
136 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
150 views