<br><br><div><span class="gmail_quote">On 28/02/2008, <b class="gmail_sendername">Roger Bivand</b> &lt;<a href="mailto:Roger.Bivand@nhh.no">Roger.Bivand@nhh.no</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Thu, 28 Feb 2008, Rainer M Krug wrote:<br> <br> &gt; On 28/02/2008, Roger Bivand &lt;<a href="mailto:Roger.Bivand@nhh.no">Roger.Bivand@nhh.no</a>&gt; wrote:<br> &gt;&gt;<br> &gt;&gt; On Thu, 28 Feb 2008, Rainer M Krug wrote:<br>
 &gt;&gt;<br> &gt;&gt;<br> &gt;&gt;&gt; On 28/02/2008, Roger Bivand &lt;<a href="mailto:Roger.Bivand@nhh.no">Roger.Bivand@nhh.no</a>&gt; wrote:<br> &gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;<br>
 &gt;&gt;&gt;&gt; Glynn Clements wrote:<br> &gt;&gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;&gt; Rainer M Krug wrote:<br> &gt;&gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;&gt;&gt; Sorry for crossposting, but I think this can be of interest for GRASS<br>
 &gt;&gt;&gt;&gt; and<br> &gt;&gt;&gt;&gt;&gt;&gt; R<br> &gt;&gt;&gt;&gt;&gt;&gt; users.<br> &gt;&gt;&gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;&gt;&gt; I am planning to write a package to make the use of GRASS from R<br> &gt;&gt;&gt;&gt; easier.<br>
 &gt;&gt;&gt;&gt;&gt;&gt; The<br> &gt;&gt;&gt;&gt;&gt;&gt; idea is to wrap the system call to execute the GRASS command into an<br> &gt;&gt; R<br> &gt;&gt;&gt;&gt;&gt;&gt; command of the same name.<br> &gt;&gt;&gt;&gt;&gt;&gt; e.g:<br>
 &gt;&gt;&gt;&gt;&gt;&gt; r.to.vect &lt;- function(..., intern=TRUE, ignore.stderr=FALSE)<br> &gt;&gt;&gt;&gt;&gt;&gt;&nbsp;&nbsp; {<br> &gt;&gt;&gt;&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; comm &lt;- paste( &quot;r.to.vect &quot;, ..., sep=&quot;&quot; )<br>
 &gt;&gt;&gt;&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; print(comm)<br> &gt;&gt;&gt;&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; system( comm, intern=intern, ignore.stderr=ignore.stderr )<br> &gt;&gt;&gt;&gt;&gt;&gt;&nbsp;&nbsp; }<br> &gt;&gt;&gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;&gt;&gt; My questions are:<br>
 &gt;&gt;&gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;&gt;&gt; 1) Is this a good way of doing it, or is giving a named list to the<br> &gt;&gt;&gt;&gt;&gt;&gt; function<br> &gt;&gt;&gt;&gt;&gt;&gt; more usefull?<br> &gt;&gt;&gt;&gt;&gt;<br>
 &gt;&gt;&gt;&gt;&gt; If R provides a way to pass individual arguments (argc/argv), use that<br> &gt;&gt;&gt;&gt;&gt; instead of contructing a shell command as a string. Otherwise, you are<br> &gt;&gt;&gt;&gt;&gt; likely to run into problems with shell metacharacters in argument<br>
 &gt;&gt;&gt;&gt;&gt; values.<br> &gt;&gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;&gt; (Replying in Nabble because Rainer&#39;s posting has been swallowed by<br> &gt;&gt;&gt;&gt;&gt; threading everywhere else)<br>
 &gt;&gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;&gt; This is the key sympton, seen recently in a similar interface to SAGA,<br> &gt;&gt;&gt;&gt;&gt; where the package author carelessly converted all &quot;/&quot; to &quot;\&quot; (it is<br>
 &gt;&gt; only<br> &gt;&gt;&gt;&gt;&gt; on Windows), thus destroying a valid r.mapcalc-like operation.<br> &gt;&gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;&gt; It is made worse by differences between Unixen and Windows in handling<br>
 &gt;&gt;&gt;&gt;&gt; stdout and stderr, and by differences between CygWin and MSYS in what<br> &gt;&gt;&gt;&gt; the<br> &gt;&gt;&gt;&gt;&gt; GRASS commands are called in system() - MSYS wants an *.exe.<br> &gt;&gt;&gt;<br>
 &gt;&gt;&gt;<br> &gt;&gt;&gt; R has a mechanism to detect the OS and, and that could be used to deal<br> &gt;&gt; with<br> &gt;&gt;&gt; &quot;/&quot; or &quot;\&quot; issues as well as other differences (one thing I haven&#39;t<br>
 &gt;&gt;&gt; considered yet as) .<br> &gt;&gt;&gt; One way of dealing with the different OS concerning stderr and stdout<br> &gt;&gt; would<br> &gt;&gt;&gt; be using the command system() in R as it already adresses these issues<br>
 &gt;&gt; (as<br> &gt;&gt;&gt; far as I know) and I managed to write usable wrappers around a few of<br> &gt;&gt; the<br> &gt;&gt;&gt; grass commands, including r.mapcalc. But especially r.mapcalc required<br> &gt;&gt;&gt; manual tweaking...<br>
 &gt;&gt;<br> &gt;&gt;<br> &gt;&gt; It is precisely manual tweaking that gets broken ...<br> &gt;<br> &gt;<br> &gt; That&#39;s why I would like to avoid it...<br> &gt;<br> &gt;&gt;<br> &gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;&gt;&gt; 2) Is there a way to obtain easily all commands from GRASS and the<br>
 &gt;&gt;&gt;&gt;&gt;&gt; parameters possible and required?<br> &gt;&gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;&gt; You can obtain a list of commands by enumerating the $GISBASE/bin and<br> &gt;&gt;&gt;&gt;&gt; $GISBASE/scripts directories. Nearly all commands support the<br>
 &gt;&gt;&gt;&gt;&gt; --interface-description switch, which outputs details of all of the<br> &gt;&gt;&gt;&gt;&gt; options in XML. There are several other switches which output the same<br> &gt;&gt;&gt;&gt;&gt; information in different formats; see the g.parser manpage for<br>
 &gt;&gt;&gt;&gt;&gt; details.<br> &gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;&gt; ---<br> &gt;&gt;&gt;&gt;&gt; Actually, the interface problem for R is almost exactly the same as<br> &gt;&gt; for<br> &gt;&gt;&gt;&gt;&gt; all the other front ends - think of R as a CLI-UI. Using XML in R to<br>
 &gt;&gt; set<br> &gt;&gt;&gt;&gt;&gt; up commands going from R to GRASS might not be impossible, where there<br> &gt;&gt;&gt;&gt;&gt; would be only one command - do_GRASS(), taking the GRASS command as<br> &gt;&gt; its<br>
 &gt;&gt;&gt;&gt;&gt; required argument, and relying on the GRASS GUI to put up a dialogue.<br> &gt;&gt; If<br> &gt;&gt;&gt;&gt;&gt; there were further arguments, then they would be used in the context<br> &gt;&gt; of<br>
 &gt;&gt;&gt;&gt;&gt; the requested command.<br> &gt;&gt;&gt;<br> &gt;&gt;&gt;<br> &gt;&gt;&gt; That sounds like a good approach and as a starting point - as ultimately<br> &gt;&gt; I<br> &gt;&gt;&gt; would like to be able to call e.g. r.in.bin() from R instead of<br>
 &gt;&gt; do_GRASS(&quot;<br> &gt;&gt;&gt; r.in.bin&quot;, ...). I am using R a lot at the moment to write ecological<br> &gt;&gt;&gt; simulation models, and to use the commands directly makes the programs<br> &gt;&gt; much<br>
 &gt;&gt;&gt; more readable as emacs highlights the commands. I will look into xml<br> &gt;&gt; parsers<br> &gt;&gt;&gt; under R to geth the information out of the grass command.<br> &gt;&gt;<br> &gt;&gt;<br> &gt;&gt; The workhorse has to be a do_GRASS() talking to GRASS in a standardised<br>
 &gt;&gt; way (mirroring g.parser?). Whether you then generate aliases for it taking<br> &gt;&gt; the names of the first argument is a detail, but not a trivial one. You<br> &gt;<br> &gt; will certainly need a NAMESPACE in R, and do_GRASS() will be several<br>
 &gt;&gt; orders of magnitude easier to debug.<br> &gt;<br> &gt;<br> &gt; I agree completely. But for readability, I would still like to have these<br> &gt; wrappsers. But One could leave them for a later stage and to get do_GRASS()<br>
 &gt; working.<br> &gt;<br> &gt; The GRASS command names are not<br> &gt;<br> &gt; guaranteed to be unique seen from the R side - load a contributed package<br> &gt;&gt; into the R workspace with its own g.list(), say, and which one will you<br>
 &gt;&gt; get?<br> &gt;<br> &gt;<br> &gt; Isn&#39;t that a general problem with using packages? There are several packages<br> &gt; which overwrite other functions in other packages and they give a warning<br> &gt; message when loading.<br>
 <br> <br>But the others *are* still available through namespaces, if the<br> contributed package author used a NAMESPACE.</blockquote><div><br>Yes - that&#39;s true. I haven&#39;t used namespaces so far, but I think I should. <br>
</div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> &gt; But it would be nice to avoid these conflicts - one<br> &gt; could introduce these wrappers as g.list.GRASS().<br>
 &gt; As an emacs user, I would actually prefer using do.GRASS() instead of<br> &gt; do_GRASS() - but that is a minor point. As far as I know, there are no<br> &gt; conventions in R concerning this?<br> <br> <br>Please note that generic methods dispatch on the .* of names for S3<br>
 (old-style) classes. I don&#39;t think that &quot;GRASS&quot; is used as an S3 class<br> name, but if it was, you&#39;d get badly hurt by that. You need to look at the<br> class/method system too.<br> <br> For example, summary.lm() is a different method from summary.default(),<br>
 and both are used as summary() dispatching on the class of the first<br> argument. I used to use &quot;.&quot; in function names but now largely avoid it for<br> this reason. do.GRASS would be a &quot;do&quot; method for an object of S3 class<br>
 &quot;GRASS&quot;. Visual editor concerns are minor by comparison. This will also<br> (as you can see) impact the use of regular GRASS command names as R<br> functions.</blockquote><div><br>I think you convinced me. But the _ is so unhandy under EMACS... But I will stick with it as it makes sense.<br>
I looked into the XML package for R, and it looks promising for parsing the <span class="q" id="q_1185f44191570a8e_3">return value from --interface-description. I&#39;ll follow this line of ideas and let you know of any progress.<br>
But if the xml needs to be evaluated each time do_GRASS() is called, it will slow it down considerably. I think the use of a cached version of the interface description (in an Rdata file probably or in a</span><span class="q" id="q_1185f44191570a8e_3">n additional XML structure</span><span class="q" id="q_1185f44191570a8e_3">?) will be the most efficient way of doing it. It could be updated at the first call of a GRASS command with do_GRASS() or manually whenever GRASS is updated.<br>
<br>Rainer<br></span></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> Roger<br> <br><br> &gt;<br> &gt; Rainer<br> &gt;<br> &gt; Roger<br>
 &gt;&gt;<br> &gt;&gt;<br> &gt;&gt;&gt;<br> &gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;&gt; I haven&#39;t been following the GUI discussion, so wonder whether the<br> &gt;&gt;&gt;&gt;&gt; --interface-description output is cached anywhere? If not, do_GRASS()<br>
 &gt;&gt;&gt;&gt;&gt; would retrieve that first, and then try to insert given arguments,<br> &gt;&gt; using<br> &gt;&gt;&gt;&gt;&gt; platform specific &quot;glue&quot; to move forward. Has the (excellent, by the<br> &gt;&gt;&gt;&gt; way)<br>
 &gt;&gt;&gt;&gt;&gt; progress on GUI provided a way of passing a &quot;bubble&quot; (say in XML) to<br> &gt;&gt;&gt;&gt; GRASS<br> &gt;&gt;&gt;&gt;&gt; commands, to avoid having to step around shell metacharacters? The<br>
 &gt;&gt; same<br> &gt;&gt;&gt;&gt;&gt; for error handling? It feels as though g.parser will help a lot.<br> &gt;&gt;&gt;<br> &gt;&gt;&gt;<br> &gt;&gt;&gt; If the grass commands are created automatically and included&nbsp;&nbsp;into a<br>
 &gt;&gt; package<br> &gt;&gt;&gt; (in addition to the flexible do_GRASS()), this would not be that<br> &gt;&gt; relevant,<br> &gt;&gt;&gt; as the creation of the code only happens once (either at coding time of<br> &gt;&gt; the<br>
 &gt;&gt;&gt; package or at compilation time - that would make the system much more<br> &gt;&gt;&gt; flexible as only GRASS commands / scripts installed be available).<br> &gt;&gt;&gt;<br> &gt;&gt;&gt; Rainer<br> &gt;&gt;&gt;<br>
 &gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;&gt; Roger<br> &gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;&gt; --<br> &gt;&gt;&gt;&gt;&gt; Glynn Clements &lt;<a href="mailto:glynn@gclements.plus.com">glynn@gclements.plus.com</a>&gt;<br> &gt;&gt;&gt;&gt;<br>
 &gt;&gt;&gt;&gt;&gt; _______________________________________________<br> &gt;&gt;&gt;&gt;&gt; grass-dev mailing list<br> &gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;&gt; <a href="mailto:grass-dev@lists.osgeo.org">grass-dev@lists.osgeo.org</a><br>
 &gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;&gt; <a href="http://lists.osgeo.org/mailman/listinfo/grass-dev">http://lists.osgeo.org/mailman/listinfo/grass-dev</a><br> &gt;&gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt;<br>
 &gt;&gt;&gt;&gt; --<br> &gt;&gt;&gt;&gt; View this message in context:<br> &gt;&gt;&gt;&gt;<br> &gt;&gt; <a href="http://www.nabble.com/Plan-to-build-Package-to-use-GRASS-from-R-tp15712877p15731306.html">http://www.nabble.com/Plan-to-build-Package-to-use-GRASS-from-R-tp15712877p15731306.html</a><br>
 &gt;&gt;&gt;&gt; Sent from the Grass - Dev mailing list archive at <a href="http://Nabble.com">Nabble.com</a>.<br> &gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt; _______________________________________________<br> &gt;&gt;&gt;&gt; grass-dev mailing list<br>
 &gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt; <a href="mailto:grass-dev@lists.osgeo.org">grass-dev@lists.osgeo.org</a><br> &gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;&gt; <a href="http://lists.osgeo.org/mailman/listinfo/grass-dev">http://lists.osgeo.org/mailman/listinfo/grass-dev</a><br>
 &gt;&gt;&gt;&gt;<br> &gt;&gt;&gt;<br> &gt;&gt;&gt;<br> &gt;&gt;&gt;<br> &gt;&gt;&gt;<br> &gt;&gt;<br> &gt;&gt; --<br> &gt;&gt;<br> &gt;&gt; Roger Bivand<br> &gt;&gt; Economic Geography Section, Department of Economics, Norwegian School of<br>
 &gt;&gt; Economics and Business Administration, Helleveien 30, N-5045 Bergen,<br> &gt;&gt; Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43<br> &gt;&gt; e-mail: <a href="mailto:Roger.Bivand@nhh.no">Roger.Bivand@nhh.no</a><br>
 &gt;&gt;<br> &gt;&gt;<br> &gt;<br> &gt;<br> &gt;<br> <br> --<br> Roger Bivand<br> Economic Geography Section, Department of Economics, Norwegian School of<br> Economics and Business Administration, Helleveien 30, N-5045 Bergen,<br>
 Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43<br> e-mail: <a href="mailto:Roger.Bivand@nhh.no">Roger.Bivand@nhh.no</a><br> <br> </blockquote></div><br><br clear="all"><br>-- <br><br>-- <br>Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation Biology (UCT)<br>
<br>Plant Conservation Unit Department of Botany<br>University of Cape Town<br>Rondebosch 7701<br>South Africa