# KnitR/Octave

Use Octave in KnitR

In general KnitR was designed to combine a report document with R code ("Knit" a document with "R" code${\displaystyle \Rightarrow }$ "KnitR"). The follow learning modul show how to integrate Octace (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 in KnitR as an R-package.

### 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.

```  x = -10:0.1:10;
plot (x, sin (x));
print -djpg 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.

• (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 which octave. On Windows add the path to the octave.exe.
• (Echo-Boolean) echo=TRUE prints the code in the document and executes the code (i.e. generates the figure). With echo=FALSE the code will be executed but the code will not appear in the document.

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.

```  This is pure text before code chunk in Octave.
```{octave,engine.path='/usr/local/bin/octave/',results='asis',echo=TRUE}
x = -10:0.1:10;
plot (x, sin (x));
print -djpg myfigure.jpg
```
More text and then import the generated figure from Octave.
![My Plot of the sin-function with Octave](myfigure.jpg)
```

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.