OS-independent way to select directory interactively in R

后端 未结 5 1477
难免孤独
难免孤独 2021-02-12 11:17

I would like users to be able to select a directory interactively in R. The solution needs to work on different platforms (at least on Linux, Windows and Mac machines that have

5条回答
  •  感动是毒
    2021-02-12 11:43

    Here is a simple directory navigation menu (using menu{utils}):

    d=1
    while(d != 0) {
      a = getwd()
      a = strsplit(a, "/")
      a = unlist(a)
      b = list.dirs(recursive = F, full.names = F)
      c = paste("..", a[length(a) - 1], a[length(a)], sep = "/")
      d = menu(c("..", b), title = c, graphics = T)
      if(d==1){
        e=paste(paste(a[1:(length(a)-1)],collapse = '/',sep = ''),'/',sep = '')
        #print(e)
        setwd(e)
      }else{
        e=paste(paste(a,collapse = '/',sep = ''),'/',b[d-1],sep='')
        #print(e)
        setwd(e)
      }
    }
    

    Note: I did not (yet) test it under different systems. Here is what the documentation says:

    If graphics = TRUE and a windowing system is available (Windows, macOS or X11 via Tcl/Tk) a listbox widget is used, otherwise a text menu. It is an error to use menu in a non-interactive session.

    One limitation: The title = can only be a single line.

提交回复
热议问题