# How to read multiple images automatically in R?

#### Sunny Sun

##### New Member
Hello all,

I have 30 tiff images in the same folder. The name of files are btv_2000097, btv_2000105 ....until btv_2000297. I can read them one by one in R, and then stack all individual layers into a raster object with 30 layers. I have 15 year images. Totally more than 300 images. Therefore, I don't want to read them one by one. Is there any way (or command) to them automatically in R? If so,please tell me how to do this?

Thank you very much

#### trinker

##### ggplot2orBust
Sorry it's the end of the semester and I am swamped but I ar least want to give you a little direction though I can't take the time to provide a fuller solution.

I think the following functions will help:

1. dir
2. lapply

Again wish I had more time but I don't.

#### Lazar

something like:
Code:
tiffFiles <- list.files("/Users/me/mypictures", pattern="*tif$", full.name=TRUE) tiffList <- lapply(tiffFiles, readTIFF) #### Sunny Sun ##### New Member Thank you so much for your reply. I tried the script you supplied. But there is something wrong. R said warning with files. So I tried this. It seems to work well. Is there something wrong, please tell me raster_data <- list.files(path="F:/PhenologyMOD09/ImagesUsed/Condition/ndvi/Filtered_single_band",pattern="*tif$", full.name=TRUE)
ndvi2000<-stack(raster_data)

#### Lazar

##### Phineas Packard
no idea. What was the error message you got? Did you install the TIFF package? Does the raster_data object contain the tif file locations you expect it to contain? What is the lapply command you tried before stack?

#### Sunny Sun

##### New Member
Hi, Yes, I have installed "tiff" package. There is an error; "Error: cannot allocate vector of size 100.9 Mb
In addition: There were 50 or more warnings (use warnings() to see the first 50)". My files are very big. I think this error comes from big size of images.
I didn't use lapply command before stack. So would the result wrong?

#### rogojel

##### TS Contributor
Hi,
the pattern used in the code seems to me to have a problem . Would the script read all files that have tifsomething in their name? Like Motif1 ?

Of course, one cane argue that in the directory you only have tiff files, but this is only a matter of time and anyway in that case the pattern * would suffice.

regards
rogojel

#### Lazar

##### Phineas Packard
No because $at the end of tif$ means all files ending in tif so tifsomething would not match.

#### rogojel

##### TS Contributor
No because $at the end of tif$ means all files ending in tif so tifsomething would not match.
hi Lazar,
but then motif would still mach right? Tiff would not?

regards
rogojel

#### Lazar

##### Phineas Packard
Unlikely. I mean file names generally end in an extension .tif .jpg .txt right so motif.doc would not match. If you are really really love he word mofit and you use it in all you non-tiff files you could escape the "." if you like:

Code:
> test <- c("motif", "hello.tif", "andthis.tif")
> grep(".*tif$", test, value=TRUE) [1] "motif" "hello.tif" "andthis.tif" > grep(".*\\.tif$", test, value=TRUE)
[1] "hello.tif"   "andthis.tif"
but I hardly see the point.