[R-sig-Geo] problem with R in a bash script

Roger Bivand Roger.Bivand at nhh.no
Tue Nov 14 19:06:37 CET 2006


On Tue, 14 Nov 2006, massimodisasha wrote:

> Hi,
> i'm tryng to write a script to use
>   R - Grass.
> 
> 
> i've found this tutorial :
> 
> if i execute the line directly in a bash shell it give me not problem,
> but if i try to use the same code in a bash script, i've an error  
> message.
> 
> "with this code there aren't problem":
> 
> The code :
> 
> ======================================================
> #
> # this  line to GRASS
> #
> 
> er_wind_size=9
> wind_size=15
> r.neighbors in=elev_meters out=avg size=$er_wind_size method=average
> r.neighbors in=elev_meters out=min size=$er_wind_size method=minimum
> r.neighbors in=elev_meters out=max size=$er_wind_size method=maximum
> r.mapcalc "er = (avg - min)/(max - min) "
> r.param.scale in=elev_meters size=$wind_size param=slope out=slope
> r.param.scale in=elev_meters size=$wind_size param=profc out=profc
> r.param.scale in=elev_meters size=$wind_size param=crosc out=crosc
> r.param.scale in=elev_meters size=$wind_size param=minic out=minic
> r.param.scale in=elev_meters size=$wind_size param=maxic out=maxic
> r.param.scale in=elev_meters size=$wind_size param=longc out=longc
> 
> #
> #This lines to R
> #
> 
> require(spgrass6)
> require(cluster)
> gmeta6 <- gmeta6()
> x <- readFLOAT6sp(c 
> ("er","crosc","longc","slope","profc","minic","maxic"))
> morph <- data.frame(cbind(x$er, x$crosc, x$longc, x$slope, x$profc, x 
> $minic, x$maxic))
> morph <- data.frame(cbind(x$er, x$crosc, x$longc, x$slope/max(x 
> $slope), x$profc, x$minic, x$maxic))
> names(morph) <- c 
> ("er","crosc","longc","slope_n","profc","minic","maxic")
> morph.clara <- clara(morph, k=5, stand=F)
> x$morph_class <- morph.clara$clustering
> rast.put6(x,"morph", zcol="morph_class")
> ===================================================
> 
> after this,...
>   i quit R and see the result in Grass
> 
> it's all OK !
> 
> 
> but if itry to have the same result with a "bash script"
> like this :
> 
> ========================================
> echo "enter elevation data"
> read elevation
> er_wind_size=9
> wind_size=15
> g.region rast=$elevation -ap
> r.neighbors in=$elevation out=avg size=$er_wind_size method=average
> r.neighbors in=$elevation out=min size=$er_wind_size method=minimum
> r.neighbors in=$elevation out=max size=$er_wind_size method=maximum
> r.mapcalc "er = (avg - min)/(max - min) "
> r.param.scale in=$elevation size=$wind_size param=slope out=slope
> r.param.scale in=$elevation size=$wind_size param=profc out=profc
> r.param.scale in=$elevation size=$wind_size param=crosc out=crosc
> r.param.scale in=$elevation size=$wind_size param=minic out=minic
> r.param.scale in=$elevation size=$wind_size param=maxic out=maxic
> r.param.scale in=$elevation size=$wind_size param=longc out=longc
> echo "
> require(spgrass6)
> require(cluster)
> gmeta6 <- gmeta6()
> x <- readFLOAT6sp(c 
> ("er","crosc","longc","slope","profc","minic","maxic"))
> morph <- data.frame(cbind(x$er, x$crosc, x$longc, x$slope, x$profc, x 
> $minic, x$maxic))
> names(morph) <- c 
> ("er","crosc","longc","slope_n","profc","minic","maxic")
> morph.clara <- clara(morph, k=5, stand=F)
> x$morph_class <- morph.clara$clustering
> rast.put6(x,"morph", zcol="morph_class")
> " > R_temp
> echo 'source("R_temp")' | R --vanilla --slave
> g.region rast=morph -ap
> d.mon x0 ; d.rast morph
> 
> 
> ========================
> 
> the execution of this script give me this error :
> 
> GRASS 6.2.0RC3 (remote):~ > ./morfo.sh
> enter elevation data
> dtm_20m
> 
> Percent complete ...  100%
> Percent complete ...  100%
> Percent complete ...  100%
> 100%
> 100%
> 100%
> 100%
> 100%
> 100%
> 100%
> Carico il pacchetto richiesto: spgrass6
> Carico il pacchetto richiesto: sp
> Carico il pacchetto richiesto: maptools
> Carico il pacchetto richiesto: foreign
> Carico il pacchetto richiesto: rgdal
> Geospatial Data Abstraction Library extensions to R successfully loaded
> Carico il pacchetto richiesto: cluster
> Errore in seq(along = vname) : oggetto "er" non trovato
                                         ^^^^
echo "" is stripping out the internal ", so R get (er, ...
and there is no object "er". Look at R_temp - you'll see that the quotes 
are absent. Maybe say echo '':

$ echo "something"er"something else"
somethingersomething else
$ echo 'something"er"something else'
something"er"something else

since I don't see any single quotes in there.

Roger


> Esecuzione interrotta
> ============================================
> 
> have you any suggestions about this ?
> 
> regards..
> Massimo.
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 

-- 
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: Roger.Bivand at nhh.no




More information about the R-sig-Geo mailing list