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,130 points
2,165 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,240 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, 2019 in Data Analytics by Cherukuri
• 33,030 points
66,789 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, 2019 in Data Analytics by anonymous
• 33,030 points
515 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 29, 2019 in Data Analytics by Cherukuri
• 33,030 points
20,969 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

edited Jun 9, 2020 by Gitika 909 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, 2019 in Data Analytics by Cherukuri
• 33,030 points
13,433 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,610 points
764 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
835 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,543 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 17, 2018 in Data Analytics by DataKing99
• 8,240 points
1,631 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,240 points
1,057 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