I am a pretty competent beginner but can’t resolve the following.
list.files() seems to require setting the working directory using setwd().
I need to list.files() from different locations in the same R session. So I start out defining file paths (path1, path2, path3) to 3 different directory locations - which is how I normally work and it works well except for importing files.
For example, if I run
file_names <- list.files(path = “path1”, pattern = "\\.SCO$”)
then the vector will be empty. But if I first setwd(path1) and run the same line it works as expected.
Problem is that to import files from the other 2 locations, I need to setwd(path2) then run list.files, then setwd(path3) and run list.files.
This does get everything into the global environment the way I need, but seems convoluted. I have reviewed this well written similar issue (list.files() operates on knit directory, not setwd() set via Session menu or console · Issue #3843 · rstudio/rstudio · GitHub) and as per this advice, I do have Knitr Directory set to Document Directory (I work mostly in RNotebook but the problem is the same even if you are working from a script - just omit this note on Knitr)
Why don’t I just put all my files in one directory to satisfy list.files()? Because the data come from a source with extremely limited naming parameters and so I need to do a lot of renaming on import - can’t do it at file save time.
So to reiterate - how do I make list.files() point to a specific directory WITHOUT using setwd()?