4

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.

2 Answers2

0

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")
erocoar
  • 5,723
  • 3
  • 23
  • 45
  • Thank you so much for the help and I think you might be right that there might not be a general solution to this. Unfortunately I'm finding that your first solution still results in the dialog boxes being displayed behind RStudio/Shiny. The second solution using `gWidgets` also results in dialog boxes behind the RStudio/Shiny application when using the TCL/TK GUI toolkits but I'll try it with one of the other toolkits instead. Hopefully that should solve the problem. – Joe Chipperfield Mar 11 '18 at 12:45
  • I've marked your reply as an answer because I think this is probably the closest we'll manage to get to a solution. Thank you for your time. – Joe Chipperfield Mar 11 '18 at 12:46
0

I found it best to pause using Sys.sleep briefly prior to opening the dialog:

root = tktoplevel("width" = 1, "height" = 1)
tkraise(root)
Sys.sleep(1) #pause just a little for dailogs
selectedDir <- 
  tclvalue(tkchooseDirectory(
    initialdir=getwd(),
    title="Select directory"))
tkdestroy(root)
Ross
  • 1