The R Project SVN R-packages

Rev

Blame | Last modification | View Log | Download | RSS feed

<article xmlns:r="http://www.r-project.org">

<para>
<r:code>
xmlSourceFunctions(system.file("exampleData", "literateFunction.xml", package = "XML"))
fun
</r:code>
</para>

<para>
<r:function id="fun">
fun = 
function(x, y = 2, z = 3)
{
  <r:code ref="checkInputs"/>
  <r:code ref="doComputation"/>
  <r:code ref="createOutput"/>
}
</r:function>

</para>
<para>
The first part is 

<r:code id="checkInputs"><![CDATA[
 if(!(x < 0 || y < .5))
    stop("invalid input")
]]></r:code>
</para>
<para>
Now we do the heart of the computation.
This creates a derived local variable and then
simulates some values. We do the latter within a separate fragment.
<r:code id="doComputation">
  tmp = z * log(x)^y  
  <r:code ref="sim"/>
</r:code>
(Note that we cannot use a CDATA for this r:code as we refer to another 
<xml:tag>r:code</xml:tag> element. So we would have to use entities for
any special characters such as &amp; and &lt; and &gt;.
</para>
<para>
Now we just put a class on the result
<r:code id="createOutput"><![CDATA[
  structure(ans, class = "FooBar")
]]></r:code>

</para>
<para>
We now come to the simulation part.
<r:code id="sim"><![CDATA[
  ans = rnorm(floor(tmp))
]]></r:code>
Note that we have to agree on the local variables across the fragments,
e.g. <r:var>ans</r:var>.


</para>
</article>