We can use any languages in knitr, including but not limited to R. Here are some simple demos with Python, Awk, Ruby, Haskell, Bash, Perl, Graphviz, TikZ, SAS, Scala, and CoffeeScript, etc:
- python.Rmd (output)
- c.Rmd (output)
- fortran.Rmd (output)
- sql.Rmd (output)
- awk.Rmd (output)
- ruby.Rmd (output)
- haskell.Rmd (output)
- bash.Rmd (output)
- perl.Rmd (output)
- dot.Rmd (output)
- tikz.Rmd (output)
- sas.Rmd (output)
- coffeescript.Rmd (output)
- polyglot.Rmd (output)
- Scala, Python, and Bash
These languages are easy to deal with because they can be called by pure
command line, e.g.
python -c can execute the code passed in as a character
Rcpp engine is different, however. The C++ code is compiled through the
Rcpp package. See Rcpp.Rmd
for example. Similarly,
C code is compiled via
R CMD SHLIB
A special engine
cat can be used to save the content of a code chunk to a
file using the
cat() function; see the
for an application.
knit_engines stores a series of named
functions to handle code from different languages, and you are free to
define a function to deal with a custom language. See
examples on how I deal with Python and Awk.
I do not really know much about other languages, so please feel free to contribute more language engines to knitr.
Note these languages work not only in Markdown, but also in other formats like LaTeX.
engine='R' (default), all chunks are executed in separate sessions,
so the variables cannot be directly shared. If we want to make use of
objects created in previous chunks, we usually have to write them to files
(as side effects). For the
bash engine, we can use
export variables from R to bash (example).
Another approach is to use the (experimental) runr package.