[GRASS-stats] Load data multiple data from grass to R

10 messages Options
Embed this post
Permalink
Jarosław Jasiewicz

[GRASS-stats] Load data multiple data from grass to R

Reply Threaded More More options
Print post
Permalink
I'd like to write R sript to read more rasters from grass to R and
writeh them to objects with unique

what I try to do:

system("g.mlist type=rast pattern=a_* >tmp_file")
k=c(scan("temp_file, what="string")) //maybe is simpler way

for (i in k) {
here_I_need_obect_with_unique_name=readRAST6(i, paste(i))
}
operations_on_obiects

I expect it could be more complicated than one line, but I complelty
cannot find solution. It seems very unintuitive

I know that I can use:

obiect <-readRAST6(k)

but how to split it on separate obiects
maybe this is better solution?

Jarek
_______________________________________________
grass-stats mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-stats
Roger Bivand

Re: [GRASS-stats] Load data multiple data from grass to R

Reply Threaded More More options
Print post
Permalink
On Wed, 21 Nov 2007, Jarek Jasiewicz wrote:

> I'd like to write R sript to read more rasters from grass to R and writeh
> them to objects with unique
>
> what I try to do:
>
> system("g.mlist type=rast pattern=a_* >tmp_file")
> k=c(scan("temp_file, what="string")) //maybe is simpler way
>
> for (i in k) {
> here_I_need_obect_with_unique_name=readRAST6(i, paste(i))
> }
> operations_on_obiects

Could you use the fact that the first argument to readRAST6() can be a
character vector of raster layer names? If k is such a character vector,
you could try readRAST6(k). The second argument is a logical vector, not
vname= as you seem to think - that is in writeRAST6().

Hope this helps,

Roger

>
> I expect it could be more complicated than one line, but I complelty cannot
> find solution. It seems very unintuitive
>
> I know that I can use:
>
> obiect <-readRAST6(k)
>
> but how to split it on separate obiects
> maybe this is better solution?
>
> Jarek
> _______________________________________________
> grass-stats mailing list
> [hidden email]
> http://lists.osgeo.org/mailman/listinfo/grass-stats
>
>

--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: [hidden email]
_______________________________________________
grass-stats mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-stats
Roger Bivand
Economic Geography Section
Department of Economics
Norwegian School of Economics and Business Administration
Helleveien 30
N-5045 Bergen, Norway
Jarosław Jasiewicz

Re: [GRASS-stats] Load data multiple data from grass to R

Reply Threaded More More options
Print post
Permalink
Roger Bivand wrote:

> On Wed, 21 Nov 2007, Jarek Jasiewicz wrote:
>
>> I'd like to write R sript to read more rasters from grass to R and
>> writeh them to objects with unique
>>
>> what I try to do:
>>
>> system("g.mlist type=rast pattern=a_* >tmp_file")
>> k=c(scan("temp_file, what="string")) //maybe is simpler way
>>
>> for (i in k) {
>> here_I_need_obect_with_unique_name=readRAST6(i, paste(i))
>> }
>> operations_on_obiects
>
> Could you use the fact that the first argument to readRAST6() can be a
> character vector of raster layer names? If k is such a character
> vector, you could try readRAST6(k). The second argument is a logical
> vector, not vname= as you seem to think - that is in writeRAST6().
>
> Hope this helps,
>
> Roger
>
> obiect <-readRAST6(k)
well maybe presented problem not enough clear
First - there is en mistake in code i typed
shuoud be:

for (i in k) {
here_I_need_obect_with_unique_name=readRAST6(i)
}

but in that way I recive obiect which every iteration will be
overwritten by nex raster map. At the end I will have obiect with last
raster map in string vector

How to preserve obiect created in the loop and control its name?
I think about something like this (for example) in the loop ??

ob_a=readRAST6("a")
ob_b=readRAST6("b")
ob_c=readRAST6("c")
...
ob_z=readRAST6("z")

if I use this

obiect <-readRAST6(k)

there will be the problem with converting every layer (I mean:
"obiect@data$raster_name" i SpGridDataFrame) in multilayer
SpGridDataFrame to "im" class obiect

In general it is the problem how to control names of obiect if in the
loop we need to create more than one obiect

Hope I presented it clear

greetings
Jarek
_______________________________________________
grass-stats mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-stats
HamishB

Re: [GRASS-stats] Load data multiple data from grass to R

Reply Threaded More More options
Print post
Permalink
Jarek Jasiewicz wrote:

> >> I'd like to write R sript to read more rasters from grass to R and
> >> writeh them to objects with unique
> >>
> >> what I try to do:
> >>
> >> system("g.mlist type=rast pattern=a_* >tmp_file")
> >> k=c(scan("temp_file, what="string")) //maybe is simpler way
> >>
> >> for (i in k) {
> >> here_I_need_obect_with_unique_name=readRAST6(i, paste(i))
> >> }
> >> operations_on_obiects


does R have an "eval" operator?


Hamish



      ____________________________________________________________________________________
Get easy, one-click access to your favorites.
Make Yahoo! your homepage.
http://www.yahoo.com/r/hs 
_______________________________________________
grass-stats mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-stats
Roger Bivand

Re: [GRASS-stats] Load data multiple data from grass to R

Reply Threaded More More options
Print post
Permalink
On Thu, 22 Nov 2007, Hamish wrote:

> Jarek Jasiewicz wrote:
>
>>>> I'd like to write R sript to read more rasters from grass to R and
>>>> writeh them to objects with unique
>>>>
>>>> what I try to do:
>>>>
>>>> system("g.mlist type=rast pattern=a_* >tmp_file")
>>>> k=c(scan("temp_file, what="string")) //maybe is simpler way
>>>>
>>>> for (i in k) {
>>>> here_I_need_obect_with_unique_name=readRAST6(i, paste(i))
>>>> }
>>>> operations_on_obiects
>
>
> does R have an "eval" operator?

Yes, but it isn't often needed at the user level, and is considered poor
practice.

Roger

>
>
> Hamish
>
>
>
>      ____________________________________________________________________________________
> Get easy, one-click access to your favorites.
> Make Yahoo! your homepage.
> http://www.yahoo.com/r/hs
> _______________________________________________
> grass-stats mailing list
> [hidden email]
> http://lists.osgeo.org/mailman/listinfo/grass-stats
>

--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: [hidden email]
_______________________________________________
grass-stats mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-stats
Roger Bivand
Economic Geography Section
Department of Economics
Norwegian School of Economics and Business Administration
Helleveien 30
N-5045 Bergen, Norway
Roger Bivand

Re: [GRASS-stats] Load data multiple data from grass to R

Reply Threaded More More options
Print post
Permalink
In reply to this post by Jarosław Jasiewicz
On Thu, 22 Nov 2007, Jarek Jasiewicz wrote:

> Roger Bivand wrote:
>>  On Wed, 21 Nov 2007, Jarek Jasiewicz wrote:
>>
>> >  I'd like to write R sript to read more rasters from grass to R and
>> >  writeh them to objects with unique
>> >
>> >  what I try to do:
>> >
>> >  system("g.mlist type=rast pattern=a_* >tmp_file")
>> >  k=c(scan("temp_file, what="string")) //maybe is simpler way
>> >
>> >  for (i in k) {
>> >  here_I_need_obect_with_unique_name=readRAST6(i, paste(i))
>>> }
>> >  operations_on_obiects
>>
>>  Could you use the fact that the first argument to readRAST6() can be a
>>  character vector of raster layer names? If k is such a character vector,
>>  you could try readRAST6(k). The second argument is a logical vector, not
>>  vname= as you seem to think - that is in writeRAST6().
>>
>>  Hope this helps,
>>
>>  Roger
>>
>>  obiect <-readRAST6(k)
> well maybe presented problem not enough clear
> First - there is en mistake in code i typed
> shuoud be:
>
> for (i in k) {
> here_I_need_obect_with_unique_name=readRAST6(i)
> }
>
> but in that way I recive obiect which every iteration will be overwritten by
> nex raster map. At the end I will have obiect with last raster map in string
> vector
>
> How to preserve obiect created in the loop and control its name?
> I think about something like this (for example) in the loop ??

What I suggested was putting all the rasters into the same object - their
names become the column names of the object. Since the current region is
respected, they will be conformal. If you want multiple objects, use
assign() which takes a character string as the name if the object being
assigned to, but watch the scoping if you are doing this in a function.

Roger

>
> ob_a=readRAST6("a")
> ob_b=readRAST6("b")
> ob_c=readRAST6("c")
> ...
> ob_z=readRAST6("z")
>
> if I use this
>
> obiect <-readRAST6(k)
>
> there will be the problem with converting every layer (I mean:
> "obiect@data$raster_name" i SpGridDataFrame) in multilayer SpGridDataFrame to
> "im" class obiect
>
> In general it is the problem how to control names of obiect if in the loop we
> need to create more than one obiect
>
> Hope I presented it clear
>
> greetings
> Jarek
>
>

--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: [hidden email]
_______________________________________________
grass-stats mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-stats
Roger Bivand
Economic Geography Section
Department of Economics
Norwegian School of Economics and Business Administration
Helleveien 30
N-5045 Bergen, Norway
Jarosław Jasiewicz

Re: [GRASS-stats] Load data multiple data from grass to R

Reply Threaded More More options
Print post
Permalink
In reply to this post by HamishB
Hamish wrote:

> Jarek Jasiewicz wrote:
>
>  
>>>> I'd like to write R sript to read more rasters from grass to R and
>>>> writeh them to objects with unique
>>>>
>>>> what I try to do:
>>>>
>>>> system("g.mlist type=rast pattern=a_* >tmp_file")
>>>> k=c(scan("temp_file, what="string")) //maybe is simpler way
>>>>
>>>> for (i in k) {
>>>> here_I_need_obect_with_unique_name=readRAST6(i, paste(i))
>>>> }
>>>> operations_on_obiects
>>>>        
>
>
> does R have an "eval" operator?
>
>
> Hamish
>
>
>
>       ____________________________________________________________________________________
> Get easy, one-click access to your favorites.
> Make Yahoo! your homepage.
> http://www.yahoo.com/r/hs 
>  
yas, but it not work with "paste" as we can expected base on other languages
Jarek
_______________________________________________
grass-stats mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-stats
Jarosław Jasiewicz

Re: [GRASS-stats] Load data multiple data from grass to R

Reply Threaded More More options
Print post
Permalink
In reply to this post by Roger Bivand
Roger Bivand wrote:

> On Thu, 22 Nov 2007, Jarek Jasiewicz wrote:
>
>> Roger Bivand wrote:
>>>  On Wed, 21 Nov 2007, Jarek Jasiewicz wrote:
>>>
>>> >  I'd like to write R sript to read more rasters from grass to R
>>> and >  writeh them to objects with unique
>>> > >  what I try to do:
>>> > >  system("g.mlist type=rast pattern=a_* >tmp_file")
>>> >  k=c(scan("temp_file, what="string")) //maybe is simpler way
>>> > >  for (i in k) {
>>> >  here_I_need_obect_with_unique_name=readRAST6(i, paste(i))
>>>> }
>>> >  operations_on_obiects
>>>
>>>  Could you use the fact that the first argument to readRAST6() can be a
>>>  character vector of raster layer names? If k is such a character
>>> vector,
>>>  you could try readRAST6(k). The second argument is a logical
>>> vector, not
>>>  vname= as you seem to think - that is in writeRAST6().
>>>
>>>  Hope this helps,
>>>
>>>  Roger
>>>
>>>  obiect <-readRAST6(k)
>> well maybe presented problem not enough clear
>> First - there is en mistake in code i typed
>> shuoud be:
>>
>> for (i in k) {
>> here_I_need_obect_with_unique_name=readRAST6(i)
>> }
>>
>> but in that way I recive obiect which every iteration will be
>> overwritten by nex raster map. At the end I will have obiect with
>> last raster map in string vector
>>
>> How to preserve obiect created in the loop and control its name?
>> I think about something like this (for example) in the loop ??
>
> What I suggested was putting all the rasters into the same object -
> their names become the column names of the object. Since the current
> region is respected, they will be conformal. If you want multiple
> objects, use assign() which takes a character string as the name if
> the object being assigned to, but watch the scoping if you are doing
> this in a function.
>
> Roger
>
>>
>> ob_a=readRAST6("a")
>> ob_b=readRAST6("b")
>> ob_c=readRAST6("c")
>> ...
>> ob_z=readRAST6("z")
>>
>> if I use this
>>
>> obiect <-readRAST6(k)
>>
>> there will be the problem with converting every layer (I mean:
>> "obiect@data$raster_name" i SpGridDataFrame) in multilayer
>> SpGridDataFrame to "im" class obiect
>>
>> In general it is the problem how to control names of obiect if in the
>> loop we need to create more than one obiect
>>
>> Hope I presented it clear
>>
>> greetings
>> Jarek
>>
>>
>
that is solution!!

many thanks Roger!
Jarek
_______________________________________________
grass-stats mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-stats
Jarosław Jasiewicz

Re: [GRASS-stats] Load data multiple data from grass to R

Reply Threaded More More options
Print post
Permalink
This another wuestion which probably has very simple solution:

I try to create single object wich is list of list (from the result of
density() function

it is very simple:
raster_dens=list(dens_1, dens_2...,dens_78)

except that I must to do it manually

I try to do it with script:

raster_dens=list()

for (i in seq(1:78)) {
append(raster_dens,
density(na.omit(raster[[i]]),bw=(max(na.omit(raster[[i]]))-min(na.omit(raster[[i]]))/50)))
}
density() returns list of 7, but I cannot ad it to raster_dens

If I do it with SpGridDataFrame, all go OK

So why I cannot do this there?

Jarek
_______________________________________________
grass-stats mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-stats
Roger Bivand

Re: [GRASS-stats] Load data multiple data from grass to R

Reply Threaded More More options
Print post
Permalink
On Fri, 23 Nov 2007, Jarek Jasiewicz wrote:

> This another wuestion which probably has very simple solution:
>
> I try to create single object wich is list of list (from the result of
> density() function
>
> it is very simple:
> raster_dens=list(dens_1, dens_2...,dens_78)
>
> except that I must to do it manually
>
> I try to do it with script:
>
> raster_dens=list()
>
> for (i in seq(1:78)) {
> append(raster_dens,
> density(na.omit(raster[[i]]),bw=(max(na.omit(raster[[i]]))-min(na.omit(raster[[i]]))/50)))
> }
> density() returns list of 7, but I cannot ad it to raster_dens
>
> If I do it with SpGridDataFrame, all go OK
>
> So why I cannot do this there?

In general, create a list of the right length first, and do

raster_dens[[i]] <- density(na.omit(raster[[i]]), ...

to avoid object copying on appending or concatenation. Then you'll get a
list of lists.

Hope this helps,

Roger

>
> Jarek
> _______________________________________________
> grass-stats mailing list
> [hidden email]
> http://lists.osgeo.org/mailman/listinfo/grass-stats
>

--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: [hidden email]
_______________________________________________
grass-stats mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-stats
Roger Bivand
Economic Geography Section
Department of Economics
Norwegian School of Economics and Business Administration
Helleveien 30
N-5045 Bergen, Norway