|
File Open/Save dialogs in R tcltkThe Open file dialogrequire(tcltk)fileName <- tclvalue(tkgetOpenFile()) # Very simple, isn't it?if (!nchar(fileName)) { tkmessageBox(message = "No file was selected!")} else { tkmessageBox(message = paste("The file selected was", fileName))}The code above produces the following window:
The Save file dialogrequire(tcltk)fileName <- tclvalue(tkgetSaveFile())if (!nchar(fileName)) { tkmessageBox(message = "No file was selected!")} else { tkmessageBox(message = paste("The file selected was", fileName))}The code above produces the following window:
Now we will assume that the user pressed Cancel:
Opening SPSS Files With The Open File DialogThis example was contributed by [url=mailtozric@web.de]Christian Schulz[/url]. It shows how to tell the OpenFile dialog what type of files to look for.
require(tcltk)getfile <- function() { name <- tclvalue(tkgetOpenFile( filetypes = "{{SPSS Files} {.sav}} {{All files} *}")) if (name == "") return; require(foreign) # read.spss() is located in this package SPSSdata <- read.spss(name, use.value.label = TRUE, to.data.frame = TRUE) assign("myData", SPSSdata, envir = .GlobalEnv)}tt <- tktoplevel()button.widget <- tkbutton(tt, text = "Select SPSS File", command = getfile)tkpack(button.widget)# The content of the SPSS file is placed in the variable 'SPSSdata'
Pressing the button gives the following OpenFile dialog, which knows which file extension to look for. In this case, only files with the extension .sav are displayed. As I don't have SPSS installed on my computer, the .sav file listed below does not have an SPSS icon.
Saving (or opening) files with more than one possible extensionMultiple possibilities for file extensions (e.g. .jpg and .jpeg) can be separated by a space as follows:
jpegFileName <- tclvalue(tkgetSaveFile(initialfile = "foo.jpg", filetypes = "{{JPEG Files} {.jpg .jpeg}} {{All files} *}"))
Created: James Wettenhall.
Last update: 20 Augustus 2005, by Philippe Grosjean.
Tested: R 2.1.1 under Windows XP sp2, with default Tcl/Tk installed with R. |
|