[R-SIG-Mac] problem with R in a bash script

Simon Urbanek simon.urbanek at r-project.org
Tue Nov 14 19:33:14 CET 2006


On Nov 14, 2006, at 12:37 PM, 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.
>

This question is not really about R. Please consider reading some  
documentation about shell programming - you have a few problems in  
the script.

When you intend to use double quotes (e.g. as parameter to echo), you  
have to escape them inside the string. You also have to escape `$' in  
cases where you mean it literally and don't want variable expansion.  
Use single quotes in shell scripts if you don't want to use shell  
expansion. Another alternative is to use heredoc notation. Also  
creating a temporary file via echo is superfluous since you can pipe  
it right into R. In addition, I can't find any shell variables at a  
first glance to be expanded, so you could as well put the script in a  
file saving you all the quoting trouble.
I have no idea about GRASS, so I can't really comment on the other  
part of the script file, but remove the echo command with the script  
and create a single text - that could be a beginning.

Cheers,
Simon

PS: this is entirely independent of Mac OS - you can safely ask unix  
users about this.


> [...]
> "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
> Esecuzione interrotta
> ============================================
>
> have you any suggestions about this ?
>
> regards..
> Massimo.
>
> _______________________________________________
> R-SIG-Mac mailing list
> R-SIG-Mac at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-mac
>
>



More information about the R-SIG-Mac mailing list