I am trying to convert all my 'projects' to use here() rather than setwd() but am having random problems with the here() function (and others) not loving the .here files. I'm not able to use rStudio etc so don't have any sort of .project files. I am just trying to use a text editor along with a .here file which I can see in windows explorer but here() is sometimes not recognising them. Here's a short script that I have used to reproduce what I am seeing:
setwd("C:/!Rprojects/heretest/src")
library(here)
packageVersion("here")
packageVersion("rprojroot")
sessionInfo()
getwd()
list.files(".")
list.files("..")
set_here("..")
set_here("..")
list.files(".")
list.files("..")
here()
dr_here()
Before running the test script, the main project directory just contains the following:
data - an empty directory
doco - an empty directory
output - an empty directory
src - a directory containing a single R script with the above code
I have opened a fresh R session to run the commands above
setwd("C:/!Rprojects/heretest/src")
library(here)
here() starts at C:/!Rprojects/heretest/src
packageVersion("here")
[1] ‘0.1’
packageVersion("rprojroot")
[1] ‘1.3.2’
sessionInfo()
R version 3.4.3 (2017-11-30)
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=English_Australia.1252 LC_CTYPE=English_Australia.1252
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C
[5] LC_TIME=English_Australia.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] here_0.1
loaded via a namespace (and not attached):
[1] compiler_3.4.3 backports_1.1.2 rprojroot_1.3-2
getwd()
[1] "C:/!Rprojects/heretest/src"
list.files(".")
[1] "heretest.R"
list.files("..")
[1] "data" "doco" "output" "src"
set_here("..")
Created file .here in C:!Rprojects\heretest
set_here("..")
File .here already exists in C:!Rprojects\heretest
list.files(".")
[1] "heretest.R"
list.files("..")
[1] "data" "doco" "output" "src"
here()
[1] "C:/!Rprojects/heretest/src"
dr_here()
here() starts at C:/!Rprojects/heretest/src, because none of the following criteria apply for this directory or any of its parents:
- contains a file
.here
- contains a file matching
[.]Rproj$
with contents matching ^Version:
in the first line
- contains a file
DESCRIPTION
with contents matching ^Package:
- contains a file
remake.yml
- contains a file
.projectile
- contains a directory
.git
- contains a file
.git
with contents matching ^gitdir:
- contains a directory
.svn
Use set_here() to create a .here
file
So, why does the second set_here() call say that the .here file exists and yet the subsequent here() function and dr_here() do not see it there? It's interesting that the second list.files("..") call also does not show the .here file, even though it's after the set_here("..") calls and the .here file certainly exists at that point.
Now, exiting R and starting a fresh session then running the same commands gives a different result.
setwd("C:/!Rprojects/heretest/src")
library(here)
here() starts at C:/!Rprojects/heretest
packageVersion("here")
[1] ‘0.1’
packageVersion("rprojroot")
[1] ‘1.3.2’
sessionInfo()
R version 3.4.3 (2017-11-30)
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=English_Australia.1252 LC_CTYPE=English_Australia.1252
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C
[5] LC_TIME=English_Australia.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] here_0.1
loaded via a namespace (and not attached):
[1] compiler_3.4.3 backports_1.1.2 rprojroot_1.3-2
getwd()
[1] "C:/!Rprojects/heretest/src"
list.files(".")
[1] "heretest.R"
list.files("..")
[1] "data" "doco" "output" "src"
set_here("..")
File .here already exists in C:!Rprojects\heretest
set_here("..")
File .here already exists in C:!Rprojects\heretest
list.files(".")
[1] "heretest.R"
list.files("..")
[1] "data" "doco" "output" "src"
here()
[1] "C:/!Rprojects/heretest"
dr_here()
here() starts at C:/!Rprojects/heretest, because it contains a file .here
this time through, note the following:
- library(here) has detected the .here file
- set_here("..") fails both times, as expected, because the file exists
- list.files("..") still doesn't detect the file (it should shouldn't it?)
- here() works correctly
- dr_here() works correctly
Is there perhaps some sort of cached directory information that's causing the .here file to be not detected by here() after it's been created by set_here() on the first time through? Maybe? Just guessing.
list.files doesn't seem to like the file either, but that's a different issue, though possibly related.
here() looks really promising and I'm loving the idea. just getting confused in trying to actually use it on windows. thanks for any help.