[R-SIG-Mac] equivalent of choose.dir()

Hans-Jörg Bibiko bibiko at eva.mpg.de
Thu Nov 29 11:06:22 CET 2012


On Nov 29, 2012, at 9:37 AM, Ivan Calandra wrote:

> I have asked on the R list already, but this Mac-list might be more appropriate.
> I am looking for a function that can choose folders interactively, like choose.dir() on Windows, or like file.choose() does for files.
> 
> I have found tcltk::tk_choose.dir() but R hangs when I try to do anything and I have to force exit.
> 
> Jim Holtman proposed to choose a file with file.choose() and then get the path from it using dirname(). This approach would work, but it would be even better if I could choose a folder directly.
> 
> Duncan Murdoch told me that I could write the equivalent of choose.dir() for MacOS, but it is beyond my reach.
> 
> Can someone help me on this?

Hi,

for the time being until it will implemented here a quick and 'dirty' function:

---------
choose.dir <- function() {
	system("osascript -e 'tell app \"R\" to POSIX path of (choose folder with prompt \"Choose Folder:\")' > /tmp/R_folder", 
			intern = FALSE, ignore.stderr = TRUE)
	p <- system("cat /tmp/R_folder && rm -f /tmp/R_folder", intern = TRUE)
	return(ifelse(length(p), p, NA))
}

a <- choose.dir()

if(is.na(a)) stop("No folder", call. = F)

print(a)
---------

AppleScript displays a "choose folder" GUI; then the chosen path will be stored temporarily in the file /tmp/R_folder (since R's system command can't run with option 'intern=T' - R will hang); the file content will be outputted and stored in the R var "p"; that's it.

Salut,
--Hans



More information about the R-SIG-Mac mailing list