0

I have a directory "space" containing 300 CSV files, and its path is "C://rstuff//space".

And I have a function:

myfunction <- function(my_dir, x, y){

      }

I want to open some of the csv files, so I want to get the location of these files,and I use the argument 'my_dir' to indicate the location of the CSV files. I want to use setwd(paste0("C://rstuff//", my_dir)) (thanks for Batanichek's comment), but I think my way is not good to set the path, if I don't know the path exactly, what should I do? Is there any good methods?

Vansiee
  • 11
  • 2
  • 1
    see `?paste0` to concate string like `paste0("C://rstuff//",your_var)` – Batanichek Feb 20 '16 at 13:12
  • Thank you very much! And I changed my question to focus on how to get into a directory. – Vansiee Feb 20 '16 at 13:28
  • what means "if I don't know the path exactly"?( how are you going to know path?) you want to choose path?(see `choose.dir`) – Batanichek Feb 20 '16 at 13:33
  • There is a directory named 'space', but I don't know its path. Because my files are in this directory, I have to get the location, and set it in my function in order to deal my files. If I change the location of this directory, I have to change the path in the paste0(), I don't want to change it every time. – Vansiee Feb 20 '16 at 13:44
  • hmm.. I think its not good idea .. you can try to search everywhere `list.dirs(recursive = T)` or try write function which use `list.dirs(recursive = F)` while not find your `dir` , but there may be more than one dir with name `space` – Batanichek Feb 20 '16 at 13:50

1 Answers1

0

You can use list.files

setwd("C://rstuff//space")
my_files<-list.files(pattern = ".csv", 
                     full.names = TRUE, recursive = TRUE, ignore.case = TRUE)

This finds all csv files in your working directory and give you the path starting from your working directory.

 [1] "./csvs2/data_1-10.csv"         
 [2] "./csvs2/old/data_1001-1010.csv"
 [3] "./overview/results.csv"

Then you can specify the ones you want to use. I for example give the important csv files a number after an "_" e.g "data_23". So you can exclude all non-important files with:

my_files<-my_files[-(which(grepl("_", my_files)==FALSE))]
JBGruber
  • 11,727
  • 1
  • 23
  • 45