R Windows OS choose.dir() File chooser won't open at working directory

末鹿安然 提交于 2019-12-30 12:51:09

问题


The choose.dir function reference page there is an example:

choose.dir(getwd(), "Choose a suitable folder")

Which should start the folder choice window at the working directory. However I am only having the folder choice window open at 'My Computer'. What reasons could there be for this function not working as intended?


回答1:


You are right in that you should not use choose.dir(), as it is OS-specific. I can indeed reproduce the issue you report - my guess is that it won't let you start in a directory that belongs to the "Root" user (whatever that may mean in Windows), because it seems to work well for other directories, not under 'Root':

 getwd()
 # [1] "C:/Users/Root/Documents"
 choose.dir(getwd(), "Choose a suitable folder") # leads to 'Computer'

 setwd("C:/datathon")
 choose.dir(getwd(), "Choose a suitable folder") # select subfolder 'scripts', works OK
 # [1] "C:\\datathon\\scripts"

There are two OS-independent solutions; the first, as it has been pointed out before, is to use the following functionality from the tcltk package:

 library(tcltk)
 setwd('~')
 getwd()
 # [1] "C:/Users/Root/Documents"
 dir <- tclvalue(tkchooseDirectory())  # opens a dialog window in 'My Documents'

The second is to use the rChoiceDialogs package (requires rJava):

 library(rJava)
 library(rChoiceDialogs)
 getwd()
 # [1] "C:/Users/Root/Documents"
 jchoose.dir()  # opens the dialog window in "C:\\Users\\Root\\Documents"


来源:https://stackoverflow.com/questions/33511964/r-windows-os-choose-dir-file-chooser-wont-open-at-working-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!