2

The choose.dir function reference page there is an example:

choose.dir(getwd(), "Choose a suitable folder")

Which should start the folder choice window at the working directory. However I am only having the folder choice window open at 'My Computer'. What reasons could there be for this function not working as intended?

biotechris
  • 181
  • 3
  • 7
  • 4
    What OS and R version? What does `getwd()` return? – MrFlick Nov 04 '15 at 01:01
  • I should have mentioned that getwd() does not return "My Computer", it gives the path to my working directory. I'm using RStudio 0.99.485 , with R version 3.2.0 on Windows mingw32. getwd() is in the in {utils} package, I'm not sure if that is specific to Windows. What is the result others get when using the example code? – biotechris Nov 04 '15 at 01:08
  • Maybe. I took that to mean that you can't select folder with a path this outside 'Computer', rather than stating that the default location for choose.dir() has to be 'Computer' on Windows. Does 'choose.dir(getwd())' work for others, or is it only not working for me? – biotechris Nov 04 '15 at 01:16
  • 1
    `choose.dir()` only exists on Windows, so I can't check ... *is* your working directory outside `Computer`, i.e. on a different drive (I guess)? – Ben Bolker Nov 04 '15 at 01:42
  • ' getwd() [1] "C:/Users/bla35s/Documents"' and C: is inside 'Computer' – biotechris Nov 04 '15 at 01:52
  • I guess the solution would be to not use choose.dir(), as relying on it would preclude usage by other OS anyway. – biotechris Nov 04 '15 at 02:04

1 Answers1

7

You are right in that you should not use choose.dir(), as it is OS-specific. I can indeed reproduce the issue you report - my guess is that it won't let you start in a directory that belongs to the "Root" user (whatever that may mean in Windows), because it seems to work well for other directories, not under 'Root':

 getwd()
 # [1] "C:/Users/Root/Documents"
 choose.dir(getwd(), "Choose a suitable folder") # leads to 'Computer'

 setwd("C:/datathon")
 choose.dir(getwd(), "Choose a suitable folder") # select subfolder 'scripts', works OK
 # [1] "C:\\datathon\\scripts"

There are two OS-independent solutions; the first, as it has been pointed out before, is to use the following functionality from the tcltk package:

 library(tcltk)
 setwd('~')
 getwd()
 # [1] "C:/Users/Root/Documents"
 dir <- tclvalue(tkchooseDirectory())  # opens a dialog window in 'My Documents'

The second is to use the rChoiceDialogs package (requires rJava):

 library(rJava)
 library(rChoiceDialogs)
 getwd()
 # [1] "C:/Users/Root/Documents"
 jchoose.dir()  # opens the dialog window in "C:\\Users\\Root\\Documents"
desertnaut
  • 57,590
  • 26
  • 140
  • 166