Search code examples
rshinyrstudiotcltk

tcltk Dialog Boxes Appear Underneath RStudio/Shiny Windows


I am currently programming my first shiny application and I am having some difficulty with some of the more subtle user interface features. I am using the tcltk library to import a number of simple dialog boxes for the user to select local directories and files (the application is only going to be run locally so I will not be using shiny's fileInput commands). This also has the advantage of not being OS specific like the choose.dir command (see R Windows OS choose.dir() File chooser won't open at working directory for more discussion on that).

However, when I'm working in Windows (I'm testing on Windows 10 although I do most of the development work in Linux), I'm finding that calls to tkchooseDirectory only opens up the dialog box behind the shiny application, if I'm running a shiny application, or behind the RStudio IDE if I'm just calling it from the console there. This doesn't seem to be a new problem: http://r.789695.n4.nabble.com/tkchooseDirectory-opens-dialog-below-browser-window-td4729500.html but I haven't seen any solutions yet.

For my development work with the RStudio console then this is a minor nuisance but, given that the shiny application will be eventually be delivered to the client (a state agency), I would really rather not aggravate them with dialog boxes being hidden by the application.

Thank you for any help that you can provide!

[edit 1] Further information: it appears that when calling tkchooseDirectory from the RStudio console, only the first call to tkchooseDirectory results in a dialog box being displayed behind the application. Subsequent calls place the dialog box to the top of the display as expected. Also, this behaviour does not happen in the R for Windows GUI and seems to be something perculiar to RStudio and its associated products.

[edit 2] It appears that others have experienced similar problems with other tcltk library dialog boxes too: MessageBox in R

[edit 3] The easiest minimal example to see this is by running:

library(tcltk2)
tkchooseDirectory()

in the RStudio console on a Windows 10 system.


Solution

  • So, I don't think there's a direct solution to this unfortunately ...

    One option is to raise a toplevel window and then the directory dialog on top of it (you have to run everything at once here, otherwise the root is in the background again).

    library(tcltk2)
    
    root = tktoplevel("width" = 1, "height" = 1)
    tkraise(root)
    tkchooseDirectory("-parent", root)
    

    Another option would be to use gWidgets.

    dir_ <- gWidgets::gfile(type = "selectdir")