Using some dummy data, I would go for something like this. Would love to see
other approaches, though!

r1 <- raster()
r2 <- raster()
r3 <- raster()
r1[] <- runif(100)
r2[] <- runif(100)
r3[] <- runif(100)
r.stack <- stack(list(r1, r2, r3))
fn <- c("r1.grd", "r2.grd", "r3.grd")
r.unstack <- unstack(r.stack) #unstack your object and you get a list

# hide a loop :)
# elements in r.unstack should correspond to a file name from fn
lapply(X = seq_len(length(r.unstack)), FUN = function(x, my.unstack, fn) {
    writeRaster(my.unstack[[x]], fn[x])
}, my.unstack = r.unstack, fn = fn)



Cheers,
Roman



On Wed, Aug 24, 2011 at 12:44 PM, Martin Brandt
<martin.brandt@univie.ac.at>wrote:

> Hi Roman,
>
> if I want seperate raster files, I use:
>
> for (f in list.files(pattern='*.tif')){
> assign(f, raster(f))
> }
>
> this imports all .tiffs of the working directory.
> A much better way is:
>
> s = stack(list.files(pattern='*.tif'))
>
> then I have all the tiffs in a stack...the problem here is that I lose the
> original names if I export it again....
>
> cheers,
> Martin
>
>
>
>
> 2011/8/24 Roman Luštrik <roman.lustrik@gmail.com>
>
>> How do you import multiple files?
>>
>>
>>
>>
>> On Wed, Aug 24, 2011 at 11:07 AM, Martin <martin_brandt@gmx.net> wrote:
>>
>>> Hi,
>>>
>>> I have a little problem. I want to import a bunch of rasters to R, do
>>> some
>>> modifications, and export them again.
>>>
>>> I import them, create a stack or brick, and write everything to a
>>> multiband
>>> file which can be opened by GRASS with writeRaster. However, doing this,
>>> all
>>> original filenames are lost (I tried all possible formats).
>>>
>>> Is there a way to save a stack to a multiband file and keep the original
>>> names, or do I have to work with loops and seperate raster files?
>>>
>>> like that:
>>>
>>> for (f in list.files(pattern='1999_*')){
>>> assign(f, raster(f))
>>> }
>>>
>>> that works, but isn't very neat, and I struggle using loops with
>>> writeRaster...
>>>
>>>
>>>
>>>
>>>
>>> --
>>> View this message in context:
>>> http://r-sig-geo.2731867.n2.nabble.com/importing-and-exporting-multiple-rasters-and-keeping-original-filename-tp6719381p6719381.html
>>> Sent from the R-sig-geo mailing list archive at Nabble.com.
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo@r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>
>>
>>
>> --
>> In God we trust, all others bring data.
>>
>
>
>
> --
>
> Department of Geography and
>
> Regional Research
>
> UZA II, Althanstr. 14
>
> 1090 Wien, AUSTRIA
>
> http://geooekologie.univie.ac.at
>
>


-- 
In God we trust, all others bring data.

	[[alternative HTML version deleted]]

