1

I use my flash drive to store all my R csv files but I write them from my desktop, laptop, and work computer. Is there a way to build a function that will ignore setwd if that drive is not present?

Working_D_Noah.H1N1 <- function(){

 #my work space miller lab computer
 if(setwd("E:/Miller Lab/Noah.H1N1/CSV Files")) return ("E:/Miller Lab/Noah.H1N1/CSV Files")

 #my desktop
 else(setwd("I:/Miller Lab/Noah.H1N1/CSV Files")) return("I:/Miller Lab/Noah.H1N1/CSV Files")

          #my lap top
          (setwd("F:/Miller Lab/Noah.H1N1/CSV Files")) return("F:/Miller Lab/Noah.H1N1/CSV Files")
                }
Working_D_Noah.H1N1

As an example, I expect that if I am on my desktop and run this code, that R will select one working directory that is available.

Hector Haffenden
  • 1,360
  • 10
  • 25
Noah_Seagull
  • 337
  • 5
  • 18
  • 1
    You can [check that the directory exists](https://stackoverflow.com/questions/4216753/check-existence-of-directory-and-create-if-doesnt-exist) before using `setwd()`. Do you use R studio? Because RStudio projects will set the working directory relative to the project file so you don't have to bother with the `setwd()` yourself and it makes things much nicer. – MrFlick May 22 '19 at 13:57
  • `if(dir.exists("I:/etc"))` – MDEWITT May 22 '19 at 14:06
  • Yes! that worked MDEWITT, you made my morning. – Noah_Seagull May 22 '19 at 15:07

0 Answers0