1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
<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 & and < and >.
</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>
|