[R-sig-Geo] get() in raster ?

Robert J. Hijmans r.hijmans at gmail.com
Mon Nov 22 00:51:23 CET 2010


Hi Agus,

This is somewhat puzzling. The general approach works in this example:

r1 <- raster(nrow=10, ncol=10)
r1[] <- 1
r3 <- r2 <- r1
v <- list(r1, r2, r3)
do.call(stack, v)

# or simply:
s <- stack(v)

# or the more convoluted, if you have the object names rather than the
objects themselves:
v <- list(get("r1"), get("r2"), get("r3"))
s <- stack(v)

#And finally, if you want a brick (as you are dealing with large
files, I think it is more efficient to first make a stack and then
write the results to a brick):

b <- brick(s)

# Although in my example, this does also work:
b <- do.call(brick, v)



On Sun, Nov 21, 2010 at 10:22 AM, Agustin Lobo <alobolistas at gmail.com> wrote:
> Thanks Edzer, but neither work:
>> v = list("r1","r2","r3")
>> do.call(brick, v)
> Error in .rasterObjectFromFile(x, objecttype = "RasterBrick", ...) :
>  file: r1 does not exist
> Calls: do.call ... <Anonymous> -> <Anonymous> -> .local -> .rasterObjectFromFile

Makes sense, because you are using character arguments, which are
interpreted as filenames.

>> v = list(r1, r2, r3)
>> bN = do.call(brick, v)
> Error in .rasterObjectFromFile(x, objecttype = "RasterBrick", ...) :
>  file: r1r2r3 does not exist

Strange to have r1r2r3 concatenated.

> Calls: do.call ... <Anonymous> -> <Anonymous> -> .local -> .rasterObjectFromFile
> 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 (fileext %in% c(".GRD", ".GRI")) { :
>  the condition has length > 1 and only the first element will be used
> 3: In if (!file.exists(x)) { :
>  the condition has length > 1 and only the first element will be used
> 4: In if (file.exists(grdfile) & file.exists(grifile)) { :
>  the condition has length > 1 and only the first element will be used
>
> Also, in the real case the raster objects (i.e.,N1, N2 and N3) will
> not have values in memory, so I'm not
> positive that the list would be the way to go:
>

Should not matter

>> class(N1)
> [1] "RasterLayer"
> attr(,"package")
> [1] "raster"
>
>> inMemory(N1)
> [1] FALSE
>
>> v = list(N1, N2, N3)
>> bN = do.call(brick, v)
> Error in as.matrix(getValues(r)) :
>  error in evaluating the argument 'x' in selecting a method for
> function 'as.matrix'
> Calls: do.call ... <Anonymous> -> addLayer -> addLayer -> .local -> as.matrix

This may be a real bug. But perhaps you can do
sN = brick( do.call(stack, v) )

>
> By now I'm editing the output of
> paste("N",1:360,sep="")
> for a call to brick() including the 360 raster objects. But there has
> to be an elegant way of doing this. Not a critical issue though.
>
> Agus
>
> 2010/11/20 Edzer Pebesma <edzer.pebesma at uni-muenster.de>:
>> Just pushed "send" too fast -- try without quotes to pass the objects
>> instead of their names:
>>
>> v = list(r1, r2, r3)
>> do.call(brick, v)
>>
>>
>> On 11/20/2010 11:51 AM, Agustin Lobo wrote:
>>> ok, the problem is that, as noted by Carson,
>>> I must use get("v") and not get(v).
>>>
>>> But still have problems to use get() within stack() or brick():
>>>> r1 = raster(SM[,,1])
>>>> r2 = raster(SM[,,2])
>>>> r3 = raster(SM[,,3])
>>>> v = c("r1","r2","r3")
>>>> get("v")
>>> [1] "r1" "r2" "r3"
>>>> bSM = brick(get(v))
>>> Error in .rasterObjectFromFile(x, objecttype = "RasterBrick", ...) :
>>>   file: r1r2r3 does not exist
>>> Calls: brick -> brick -> .local -> .rasterObjectFromFile
>>>
>>> ( I'm trying to use get() because have to
>>> create a brick with 360 files that have been read as raster objects within
>>> a loop using assign() )
>>>
>>> Agus
>>>
>>>
>>> 2010/11/19 Robert J. Hijmans <r.hijmans at gmail.com>:
>>>> This suggest that you have an object "r1" that is a Raster* object.
>>>> Perhaps because of a saved session?
>>>> Have you looked at
>>>> ls()
>>>> Robert
>>>>
>>>> On Fri, Nov 19, 2010 at 1:24 PM, Agustin Lobo <alobolistas at gmail.com> wrote:
>>>>> Hi again!
>>>>>
>>>>> I'm having problems because cannot use get()  from base once package
>>>>> raster has been installed:
>>>>>
>>>>>> v = c("r1","r2","r3")
>>>>>> v
>>>>> [1] "r1" "r2" "r3"
>>>>>> get(v)
>>>>> Loading required package: raster
>>>>> Loading required package: sp
>>>>> raster version 1.6-22 (17-November-2010)
>>>>> class       : RasterLayer
>>>>> nrow        : 18
>>>>> ncol        : 36
>>>>> ncell       : 648
>>>>> projection  : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
>>>>> xmin        : -180
>>>>> xmax        : 180
>>>>> ymin        : -90
>>>>> ymax        : 90
>>>>> xres        : 10
>>>>> yres        : 10
>>>>>
>>>>> Even not having loaded raster, it gets automatically loaded once I
>>>>> call get(). Is this a bug?
>>>>> In general, is there a way to force that, in case of conflict, you run
>>>>> a particular function from a given package?
>>>>>
>>>>> Thanks!
>>>>>
>>>>> Agus
>>>>>
>>>>
>>>> _______________________________________________
>>>> R-sig-Geo mailing list
>>>> R-sig-Geo at stat.math.ethz.ch
>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>>
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at stat.math.ethz.ch
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>> --
>> Edzer Pebesma
>> Institute for Geoinformatics (ifgi), University of Münster
>> Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
>> 8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
>> http://www.52north.org/geostatistics      e.pebesma at wwu.de
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at stat.math.ethz.ch
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



More information about the R-sig-Geo mailing list