mathematical.coffee
mathematical.coffee

Reputation: 56915

R - winDialogString equivalent in tcltk (tkdialog?)

What's the equivalent to winDialogString in tcltk?


Background

I've been using readline in R in order to ask the user a question and get a response:

response <- readline("What's the air speed velocity of an unladen swallow? ")

Today I was on my Windows laptop and discovered the winDialogString function, which pops up a little modal message box to get the input:

response <- winDialogString("What's the air speed velocity of an unladen swallow? ",
                            default="african or european?")

I wish to use this function, but in the interests of portability, I'd like to use a tcltk version of winDialog so at least my Linux boxes can also have this.

Now, if I was using winDialog the equivalent is tkmessageBox:

# fairly equivalent, tkmessageBox is more portable.
winDialog(type='yesno','Do you like green eggs and ham?')
tkmessageBox(type='yesno',message='Do you like green eggs and ham?')

What's the equivalent to winDialogString in tcltk?

I thought it was tkdialog, so I gave it a go:

tkdialog("What's the air speed velocity of an unladen swallow?")

It gives me this error:

Error in structure(.External("dotTclObjv", objv, PACKAGE = "tcltk"), class = "tclObj") : 
  [tcl] wrong # args: should be "tk_dialog w title text bitmap default ...".

The documentation is quite unhelpful ?tkdialog points to a generic tk help page with no specific documentation. How can I use tkdialog (and is it even what I want?)

I deduced that I might have to feed in all the arguments it's complained about, so I've tried a few things :

> tkdialog(title='',
           text="What's the air speed velocity of an unladen swallow?",
           bitmap='',default="african or european?")
Error in structure(.External("dotTclObjv", objv, PACKAGE = "tcltk"), class = "tclObj") : 
  [tcl] bad window path name "-title".

> tkdialog(tktoplevel(),title='',
           text="What's the air speed velocity of an unladen swallow?",
           bitmap='',default="african or european?")
Error in structure(.External("dotTclObjv", objv, PACKAGE = "tcltk"), class = "tclObj") : 
  [tcl] bitmap "-text" not defined.

How can I get this to work? I'd rather stay within tcltk rather than using something like gWidgets, since tcltk is in most R distros (and if the R distro is tcltk-incompatible, I'll fall back to readline).

cheers.

Upvotes: 2

Views: 2102

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137587

The closest thing to what you're after is indeed tkdialog but it's not really designed for use from R; you'll have to do some work (and I fully admit to not knowing R, I'm on the Tk side of things). What I can do is explain what those parameters need to be.

There is documentation of the underlying tk_dialog command online. In particular, you need:

  1. A window name which is not used for anything else. (This will become the name of the window used to implement the dialog.)
  2. A title for the dialog.
  3. Some text for the body of the dialog.
  4. The string name of a bitmap picture to use. Unless you're doing something very gnarly and low-level, must be one of: error, gray75, gray50, gray25, gray12, hourglass, info, questhead, question, warning (or on OSX, you also get these bonus system pictures: document, stationery, edition, application, accessory, folder, pfolder, trash, floppy, ramdisk, cdrom, preferences, querydoc, stop, note, caution).
  5. The zero-based index of the default button. If -1, there's no default.
  6. (Well, 6, 7, ...) The text to put on the buttons, one string per button.

The API isn't really all that well designed to be used from R. It's not particularly nice from Tcl either, and that list of bitmaps is really obscure (and that's speaking as one of the maintainers of Tk). In case you're interested, the list is documented but I'm not surprised you didn't know that that was what you wanted. I wouldn't tell you about it except that you're using tk_dialog (itself obsolete really).


You could also implement it all yourself. The code to do a simple dialog isn't that complex (tk_dialog is purely scripted on top of conventional features). I'm not nearly a good enough R programmer to be able to help you do it though.

Upvotes: 3

Related Questions