rsetwd

R: setwd delay when using file.choose


So what im doing is making my browse button so that when i click on them bring the user straight to the directory that i want them to save their file in or look for their file.

For example

     setwd("C:\\Users\\Eric\\Desktop\\Program\\graphs") #set directory
     file.choose()

However in the earlier script i have already set my work directory at

    setwd("C:\\Users\\Eric\\Desktop\\Proram") #set directory

so when i ran the first example it brought me to directory Program instead of graphs . but when i ran file.choose() on the second time, it then brought me to the graphs directory why is this happening? any idea how to fix this ?


Solution

  • Here's a quick and dirty solution to your problem:

     dirPath <- "C:\\Users\\Eric\\Desktop\\Program\\graphs"
     setwd(dirPath)
     # Tell R to sleep until the current directory matches the expected directory
     while(getwd() != normalizePath(dirPath)) {
       Sys.sleep(0.02)
     }
     file.choose()