Skip to content

file.choose()

13 messages · Dennis Weygand, David Winsemius, Rui Barradas +3 more

#
How do I change the default directory that file.choose() (Windows-10) uses?
D.P. Weygand
#
On 3/3/20 7:29 AM, Dennis Weygand wrote:
?setwd
#
Hello,

You must set the directory manually.

old_dir <- getwd()
setwd('path/new/dir')

file.choose()

#other stuff, if necessary end with

setwd(old_dir)


Hope this helps,

Rui Barradas

?s 15:29 de 03/03/20, Dennis Weygand escreveu:
#
That's what I was about to answer but tried first and it did not work.
Now I tried more and noticed an unexpected behavior:

getwd()
[1] "C:/Users/.../Documents"
file.choose() ## opens in "C:/Users/.../Documents" as expected

setwd("D:/")
getwd()
[1] "D:/"
file.choose() ## unexpectedly, opens in "C:/Users/.../Documents"

setwd("D:/Data")
getwd()
[1] "D:/Data"
file.choose() ## opens in "D:/Data" as expected

Is that normal behavior?

Ivan

--
Dr. Ivan Calandra
TraCEr, laboratory for Traceology and Controlled Experiments
MONREPOS Archaeological Research Centre and
Museum for Human Behavioural Evolution
Schloss Monrepos
56567 Neuwied, Germany
+49 (0) 2631 9772-243
https://www.researchgate.net/profile/Ivan_Calandra
On 03/03/2020 16:36, David Winsemius wrote:
#
On 03/03/2020 10:47 a.m., Ivan Calandra wrote:
If I recall correctly, the Windows GUI version opens the same directory 
as the user last selected (i.e. the dialog box remembers the last 
selection).   This was by design, because Windows users often start the 
GUI with the working directory being the R home directory, and rarely 
use setwd() to change it.

On Windows, I think you always get that version.  On other platforms it 
may depend on which front end you're using.

Duncan Murdoch
#
Thank you Duncan.

I tried that through RStudio 1.2.5019 with R 3.6.2 on Win10.

And what I did not mention is that I always cancelled the file.choose()
actions, meaning I did not navigate to another folder.
This does not quite fit what you said, but I might have misunderstood
you, or RStudio overrides this behavior.

I still find it strange that my second and third examples behave
differently.

Ivan

--
Dr. Ivan Calandra
TraCEr, laboratory for Traceology and Controlled Experiments
MONREPOS Archaeological Research Centre and
Museum for Human Behavioural Evolution
Schloss Monrepos
56567 Neuwied, Germany
+49 (0) 2631 9772-243
https://www.researchgate.net/profile/Ivan_Calandra
On 03/03/2020 17:26, Duncan Murdoch wrote:
#
Hi Ivan

I tried your examples and they behave as expected. Second one opens D:/ and
third should be setwd("D:/Data/") to open in Data folder.
R Under development (unstable) (2019-07-21 r76854)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

So the problem is in your setting.

Cheers
Petr
http://www.R-project.org/posting-guide.html
#
Thanks Petr for checking.

This unexpected behavior is reproducible on my machine, both with the
RGui and RStudio, both with R3.6.2 and 3.6.3.
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)
Matrix products: default
locale:
[1] LC_COLLATE=English_United Kingdom.1252? LC_CTYPE=English_United
Kingdom.1252??
[3] LC_MONETARY=English_United Kingdom.1252
LC_NUMERIC=C??????????????????????????
[5] LC_TIME=English_United Kingdom.1252???
attached base packages:
[1] stats???? graphics? grDevices utils???? datasets? methods?? base????
loaded via a namespace (and not attached):
[1] compiler_3.6.3 tools_3.6.3

Couldn't be that it behaves differently when the working directory is
the root of the drive (D:/) vs. a folder on the drive (D:/Data)?

Ivan ?

--
Dr. Ivan Calandra
TraCEr, laboratory for Traceology and Controlled Experiments
MONREPOS Archaeological Research Centre and
Museum for Human Behavioural Evolution
Schloss Monrepos
56567 Neuwied, Germany
+49 (0) 2631 9772-243
https://www.researchgate.net/profile/Ivan_Calandra
On 04/03/2020 7:55, PIKAL Petr wrote:
#
Hm, as I wrote, something is weird with your installation or machine.
_                                                 
platform       x86_64-w64-mingw32                                
arch           x86_64                                            
os             mingw32                                           
system         x86_64, mingw32                                   
status         Under development (unstable)                      
major          3                                                 
minor          7.0                                               
year           2019                                              
month          07                                                
day            21                                                
svn rev        76854                                             
language       R                                                 
version.string R Under development (unstable) (2019-07-21 r76854)
nickname       Unsuffered Consequences
I do not have other version available so mayby others could adivice what is
going on.
[1] "O:/Dokumenty/STAT/pracovni"
Error in file.choose() : file choice cancelled
[1] "$RECYCLE.BIN"              "archiv"                   
 [3] "Cecko"                     "COzak.csv"                
 ...
13] "vypal"                     "Windows Burn Temp Files"
[1] "D:/archiv"
[1] "CC"                     "disketa z mereni barvy" "jine"

[4] "prezentace"             "TEMP"                   "zdroje"

[7] "zpravy"
Cheers
Petr
loaded via a
root of
http://www.R-project.org/posting-guide.html
#
Well, actually it doesn't matter much because I almost never use this
function, and when I do, I use RStudio projects anyway with relative paths.

It was just out of curiosity and trying to understand why it behaves in
this (unexpected) way.

Thanks,
Ivan

--
Dr. Ivan Calandra
TraCEr, laboratory for Traceology and Controlled Experiments
MONREPOS Archaeological Research Centre and
Museum for Human Behavioural Evolution
Schloss Monrepos
56567 Neuwied, Germany
+49 (0) 2631 9772-243
https://www.researchgate.net/profile/Ivan_Calandra
On 04/03/2020 9:47, PIKAL Petr wrote:
#
Hello,

This is off-topic, it's been a while since I update my version of Windows.

Running R 3.6.3, RGui on Windows 7, sessionInfo() below.

The unexpected behavior I got is different from those posted so far.
In all cases the dialog window was closed without choosing a file.
Note: I don't have a directory C:\data so I changed to the home dir.

old_dir <- getwd()
old_dir
#[1] "C:/Programas/R/R-3.6.3/bin/x64"

file.choose()    # works, current dir was not changed

setwd('~/')
getwd()
#[1] "C:/Users/Rui/Documents"

file.choose()    # doesn't work, still in "C:/Programas/R/R-3.6.3/bin/x64"
file.choose()    # works at the second time

setwd('C:/')
getwd()
#[1] "C:/"

file.choose()    # doens't work, still in "C:/Users/Rui/Documents"
file.choose()    # works at the second time

setwd(old_dir)
getwd()
#[1] "C:/Programas/R/R-3.6.3/bin/x64"

file.choose()    # doesn't work, still in "C:/"
file.choose()    # works at the second time


sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=Portuguese_Portugal.1252 
LC_CTYPE=Portuguese_Portugal.1252
[3] LC_MONETARY=Portuguese_Portugal.1252 LC_NUMERIC=C 

[5] LC_TIME=Portuguese_Portugal.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] compiler_3.6.3


Hope this helps,

Rui Barradas



?s 08:05 de 04/03/20, Ivan Calandra escreveu:
#
Hi Rui,

I hadn't checked it, but indeed, calling file.choose() a second time
works as expected when it doesn't the first time already.

Best,
Ivan

--
Dr. Ivan Calandra
TraCEr, laboratory for Traceology and Controlled Experiments
MONREPOS Archaeological Research Centre and
Museum for Human Behavioural Evolution
Schloss Monrepos
56567 Neuwied, Germany
+49 (0) 2631 9772-243
https://www.researchgate.net/profile/Ivan_Calandra
On 04/03/2020 10:45, Rui Barradas wrote:
#
You can see the source code for this in

https://svn.r-project.org/R/trunk/src/extra/graphapp/dialogs.c

The function handling the dialog is askfilenames, starting at line 229. 
I'd guess the weird behaviour where file.choose() acts differently on 
the first and second calls in Rui's example is because the directory is 
saved before the peekevent() calls on line 274.  However, I can't build 
R on Windows any more, so I can't test this.

Duncan Murdoch
On 04/03/2020 4:48 a.m., Ivan Calandra wrote: