I tried it as follows, and it works fine for me:

setwd("~/Desktop/")
fnms <- dir(pattern = "tas_")
rlst <- lapply(fnms, function(x) raster(x))
plot(rlst[[1]])  # Works fine
rst <- stack(lapply(fnms, function(x) raster(x)))
plot(rst[[1]])  # Also fine

Looking at the files in a text editor, I see that they have a unix coding.
That works fine for me, since I am using a Mac, but if you are using
Windows, that is one possible place to look for an error.

Hope you can get this to work.

Cheers, Lyndon


On Thu, Mar 1, 2012 at 10:16 PM, Kanika Jhunjhnuwala <
JhunjhnuwalaK@landcareresearch.co.nz> wrote:

>  Hi****
>
> Attaching 2 files. ****
>
> Thanks****
>
> ** **
>
> Kanika****
>
> ** **
>
> *From:* Lyndon Estes [mailto:lyndon.estes@gmail.com]
> *Sent:* Friday, 2 March 2012 4:11 p.m.
>
> *To:* Kanika Jhunjhnuwala
> *Cc:* r-sig-geo@r-project.org
> *Subject:* Re: [R-sig-Geo] reading a list of text files as rasters****
>
> ** **
>
> Hi,
>
> Without the data in hand, it is hard to know why.  I would suggest the
> following:
>
> Try
> setwd("Original_data/AR4_climate_change_ASCII_grids/")
>
> And then enter the search pattern.  Presumably you have done so, but check
> to make sure that the string vector has the correct file names.
>
> If so, then repeat the lapply. Perhaps being in the working directory will
> help.
>
> Otherwise, if the files are small enough, send me two or three and I will
> try them on my system.
>
> Cheers, Lyndon ****
>
> On Thu, Mar 1, 2012 at 10:00 PM, Kanika Jhunjhnuwala <
> JhunjhnuwalaK@landcareresearch.co.nz> wrote:****
>
> Thanks but that did not work either. I get the same error****
>
>  ****
>
> > r.list <- lapply(fnms, function(x) raster(x))****
>
> Error in .rasterObjectFromFile(x, band = band, objecttype =
> "RasterLayer",  : ****
>
>   Cannot create a RasterLayer object from this file. (file does not exist)
> ****
>
>  ****
>
> And if I try to stack it instead I get****
>
>  ****
>
> r.st <- stack(lapply(fnms, function(x) raster(x)))****
>
>  Error in stack(lapply(fnms, function(x) raster(x))) : ****
>
>   error in evaluating the argument 'x' in selecting a method for function 'stack': Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",  : ****
>
>    Cannot create a RasterLayer object from this file. (file does not exist)****
>
>  ****
>
> KJ****
>
>  ****
>
> *From:* Lyndon Estes [mailto:lyndon.estes@gmail.com]
> *Sent:* Friday, 2 March 2012 3:55 p.m.
> *To:* Kanika Jhunjhnuwala
> *Cc:* r-sig-geo@r-project.org
> *Subject:* Re: [R-sig-Geo] reading a list of text files as rasters****
>
>  ****
>
> It looks like you have not put set up lapply incorrectly.
>
> hadcm3_40<-lapply(list.files(****
>
> path=
> "Original_data/AR4_climate_change_ASCII_grids/",pattern="^tas_2040.*a1b.*hadcm3"),raster,
> crs=prj_latlong)****
>
> Try this instead:
> fnms <- list.files(path= "Original_data/AR4_climate_change_ASCII_grids/",
>                           pattern="^tas_2040.*a1b.*hadcm3")
> r.list <- lapply(fnms, function(x) raster(x))
> #or make a stack
> r.st <- stack(lapply(fnms, function(x) raster(x)))
>
> That should work.
>
> Cheers, Lyndon
>
>
>
>
> ****
>
> On Thu, Mar 1, 2012 at 8:51 PM, Kanika Jhunjhnuwala <
> JhunjhnuwalaK@landcareresearch.co.nz> wrote:****
>
> Hi
>
> I have been all through google looking for an answer to this.
>
> I am trying to read a list of text files as rasters.
>
>
> >prj_latlong <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs "
> > list.files(path= "C:/Work/Climate
> stuff/Original_data/AR4_climate_change_ASCII_grids",pattern="^tas_2040.*a1b.*hadcm3")
> > b<-raster(a,crs=prj_latlong)
> Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",
>  :
>  Cannot create a RasterLayer object from this file. (file does not exist)
> In addition: Warning messages:
> 1: In if (x == "" | x == ".") { :
>  the condition has length > 1 and only the first element will be used
> 2: In if (start != "http" & start != "ftp") { :
>  the condition has length > 1 and only the first element will be used
> 3: In if (fileext %in% c(".GRD", ".GRI")) { :
>  the condition has length > 1 and only the first element will be used
> 4: In if (!file.exists(x)) { :
>  the condition has length > 1 and only the first element will be used
> 5: In if (extension(x) == "") { :
>  the condition has length > 1 and only the first element will be used
> 6: In if (fileext %in% c(".NC", ".NCDF", ".NETCDF")) { :
>  the condition has length > 1 and only the first element will be used
> 7: In if (fileext == ".GRD") { :
>  the condition has length > 1 and only the first element will be used
> 8: In if (fileext %in% c(".SGRD", ".SDAT")) { :
>  the condition has length > 1 and only the first element will be used
> 9: In if (nchar(filename) == 0) stop("empty file name") :
>  the condition has length > 1 and only the first element will be used
> 10: In if (!file.exists(x)) { :
>  the condition has length > 1 and only the first element will be used
>
> also if I try this instead I get an error
>
> > hadcm3_40<-lapply(list.files(path=
> "Original_data/AR4_climate_change_ASCII_grids/",pattern="^tas_2040.*a1b.*hadcm3"),raster,
> crs=prj_latlong)
>
> Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",
>  :
>
>  Cannot create a RasterLayer object from this file. (file does not exist)
>
>
> But if I do select a single file instead of the list and do this it works
> File<-
> raster("Original_data/AR4_climate_change_ASCII_grids/tas_2040a1b_apr_ukmo_hadcm3.txt",
> crs=prj_latlong)
>
> Somehow, it just cant read a list of text files as a list of rasters but
> can read a single one. I am not sure what to do. If I try different
> functions in lapply I get a list of 12 which is what I want (there are 12
> files I want to read in and make rasters from).
>
> Any help would be great
>
> Thanks
> KJ
>
>
> ________________________________
> Please consider the environment before printing this email
> Warning: This electronic message together with any attachments is
> confidential. If you receive it in error: (i) you must not read, use,
> disclose, copy or retain it; (ii) please contact the sender immediately by
> reply email and then delete the emails.
> The views expressed in this email may not be those of Landcare Research
> New Zealand Limited. http://www.landcareresearch.co.nz
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo****
>
>  ****
>
> ** **
>  ------------------------------
>
> Please consider the environment before printing this email
> Warning: This electronic message together with any attachments is
> confidential. If you receive it in error: (i) you must not read, use,
> disclose, copy or retain it; (ii) please contact the sender immediately by
> reply email and then delete the emails.
> The views expressed in this email may not be those of Landcare Research
> New Zealand Limited. http://www.landcareresearch.co.nz****
>
> ** **
>
> ------------------------------
> Please consider the environment before printing this email
> Warning: This electronic message together with any attachments is
> confidential. If you receive it in error: (i) you must not read, use,
> disclose, copy or retain it; (ii) please contact the sender immediately by
> reply email and then delete the emails.
> The views expressed in this email may not be those of Landcare Research
> New Zealand Limited. http://www.landcareresearch.co.nz
>

	[[alternative HTML version deleted]]

