Skip to content

equivalent of choose.dir()

4 messages · Hans-Jörg Bibiko, Ivan Calandra, Jean-Paul Marcel Vallée

#
Dear Mac-users,

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?

Thanks in advance,
Ivan


sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: i386-apple-darwin9.8.0/i386 (32-bit)
locale:
[1] fr_FR.UTF-8/fr_FR.UTF-8/fr_FR.UTF-8/C/fr_FR.UTF-8/fr_FR.UTF-8
attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods base
#
On Nov 29, 2012, at 9:37 AM, Ivan Calandra wrote:

            
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
#
Thanks Hans, it works like a charm!

Ivan

--
Ivan CALANDRA
Universit? de Bourgogne
UMR CNRS/uB 6282 Biog?osciences
6 Boulevard Gabriel
21000 Dijon, FRANCE
+33(0)3.80.39.63.06
ivan.calandra at u-bourgogne.fr
http://biogeosciences.u-bourgogne.fr/calandra
On Nov 29, 2012, at 9:37 AM, Ivan Calandra wrote:

            
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
#
HI,

when I run the choose.dir function, a second R application is starting and I have finally 2 R icon running on my Mac and I have to quit manually the second R application. Is there a way to quit  the second R application automatically?

Thanks,

JPV


Le 29 nov. 2012 ? 11:06, Hans-J?rg Bibiko a ?crit :