5

Using tk_choose.files or file.choose I am able to select a file interactively. Is there an analogous function wherein I can allow a user to interactively decide where to save the output of a write.table?

LostInTheCode
  • 1,724
  • 2
  • 15
  • 22
Maiasaura
  • 32,226
  • 27
  • 104
  • 108

4 Answers4

6

On Windows 7 and working through the RGUI, I can specify something like:

write.table(x = iris, file = file.choose())

which pops open a Windows Explorer dialogue. I can then navigate to any existing file, create a new file by right clicking, or simply by typing the name of a new file where it will ask to create a new file.

I guess this may not be platform independent...can others with the appropriate OS's verify?

Chase
  • 67,710
  • 18
  • 144
  • 161
2

Old question, but after a long search I found that the tcltk2 package now exists as an improvement of tcltk:

library(tcltk2)
filename <- tclvalue(tkgetSaveFile())
if (!nchar(filename)) {
  tkmessageBox(message = "No file was selected!")
} else {
  tkmessageBox(message = paste("The file selected was", filename))
}
MS Berends
  • 4,489
  • 1
  • 40
  • 53
2

Try

val <- tkgetSaveFile(initialfile="", title="Save a file...")
f <- tclvalue(val)
if(f != "") ...
jverzani
  • 5,600
  • 2
  • 21
  • 17
  • For those searching - the non-documented but valid arguments are: `confirmoverwrite`, `defaultextension`, `filetypes`, `initialdir`, `initialfile`, `parent`, `title`, `typevariable` – MS Berends Oct 12 '22 at 08:32
0

@Chase - this works in OS X (Eclipse and StatET). At least, I tried writing a data.frame (df) as a CSV file:

write.csv(x = df, file = file.choose())
user441706
  • 1,370
  • 2
  • 16
  • 17
  • You can't really do much other than overwrite an existing file. Right clicking to choose a new file in this dialog doesn't work on osx. – Maiasaura Jan 22 '11 at 22:46
  • 1
    With StatET/Eclipse on OS X you get a dialog window in which you can add a name for a new file.. – user441706 Jan 22 '11 at 22:54