KnitR/Octave

In general KnitR was designed to combine a report document with R code ("Knit" a document with "R" code$$\Rightarrow $$ "KnitR"). The follow learning modul show how to integrate Octave (Open Source software numeric calculations) in KnitR code for Dynamic Document Generation.

Octave Code in KnitR Documents
The following section explains how to perform calculation with Octave and generation of figure in R with KnitR package in R. The knitr4octave.Rmd is an example that can be used in the learning resource. Keep in mind to adapt the path to Octave into the head of the Octave code chunk so that Octave can be executed in R.

Working Octave Code
Assume we have the following Octave code, that plots a function in Octave and saves the generated code in a JPG file myfigure.jpg.

Embed the Octave Code in the KnitR document
The code chunk in R-Markdown looks a bit different than the code chunk in the statistical language R. The which command provides the path for starting octave, e.g.
 * (Path to Octave) The header must contain the location/path to the Octave interpreter that executes the code. Here Octave is located at /usr/local/bin/octave/ on a Linux machine. You can identify the path to Octave on Linux and MacOSX by
 * Use this path as engine path in the code chunk for KnitR.
 * On Windows add the path to the octave.exe to your PATH variable.
 * On Windows add the path to the octave.exe to your PATH variable.

Now we will use the Octave-Code in the R-Markdown document with the 3 backticks. The next code chunk is integrated in KnitR markdown with preceeding text and an import in the document for the generated figure.
 * (Echo-Boolean) echo=TRUE prints the code in the document. This is a standard feature of code chunks that can be used e.g. for code chunks in R. With echo=FALSE the code will disappear in the generated document by KnitR. The code will be executed even if the code does not appear in the document. So figures based on updated data will be included in the KnitR generated output after a KnitR run.
 * (Figures) Octave generates figures as usual similar to calls in Graphical User Interface of Octave. Save figures on the harddisk an import the generated figures with the corresponding filename in the R-Markdown document. and executes the code (i.e. generates the figure).

Path to Octave
The engine path defines, where The import of the generated figure is not necessary with R code. With an R code chunk a plot command in the code chunk will be visible in the output document. After the lass3 backticks there is some more text in the R-Markdown document. The generate figure will be imported with a standard image import syntax in R-Markdown.