0

I have the R code that will create a new folder then will set the directory to the new folder. But I want to add an argument that will check whether the new folder exists in the directory if it does then it wont create the new folder.

Here is my R code

path <- getwd()

newfolder <- "New Folder"

dir.create(file.path(path, newfolder))

new_path <- file.path(path, newfolder)

setwd(new_path)

I am struggling to add the argument to achieve my goal. Can anyone help me with that?

CodingStark
  • 199
  • 3
  • 17

1 Answers1

1

Use dir.exists():

fp <- file.path(path, newfolder)

if (!dir.exists(fp)) dir.create(fp)
andrew_reece
  • 20,390
  • 3
  • 33
  • 58