Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interactively ask user for filename before saving file

Tags:

r

I want to save the my tab delim files manually. I mean that I want user to choose the directory and file name when he wants to save the data. (For an example I have merged individual files into single file and want to save it.)

Usually I use write.table but in write.table we define the directory path and file name within that function but I want a function in which user can save file with any name in his desired directory.

like image 370
Dinesh Avatar asked Aug 23 '11 15:08

Dinesh


3 Answers

Just use the file.choose() function,like this:

write.table(yerdata, file = file.choose(new = TRUE))

On Windows, at least, that will bring up a dialog for save-as commands.

like image 94
Matt Parker Avatar answered Nov 17 '22 06:11

Matt Parker


Annoyingly the tcltk package doesn't have a function for 'Save As', it only has a file selector for choosing an existing file.

Luckily you can take the DIY approach via some tcl calls:

require(tcltk)
write.table(yerdata,file = tclvalue(tcl("tk_getSaveFile")))

The tk_choose.files function source could be used as a template to write a nicer interface to tcl("tk_getSaveFile") if needed. Does seem to be a glaring omission in package:tcltk though...

like image 38
Spacedman Avatar answered Nov 17 '22 06:11

Spacedman


Using gWidgets:

gfile("Save yerdata", type = "save", handler = function(h, ...)
{
  write.table(yerdata, file = h$file)
})
like image 41
Richie Cotton Avatar answered Nov 17 '22 07:11

Richie Cotton