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

Use Octave in KnitR

Octave Code in KnitR Documents

edit

The following section explains how to perform calculation with Octave and generation of figure in R with KnitR package in R. The knitr4octave.Rmd[1] 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

edit

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

edit

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

The which command provides the path for starting octave, e.g.

/usr/bin/octave
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.
  • (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).

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/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)

Path to Octave

edit

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.

References

edit
  1. knitr4octave.Rmd (2024) R-Markdown document in the knitr4education package on GitHub - URL: https://github.com/niebert/knitr4education/tree/main/en