Chapter 14 Other Languages

Besides the R language, many other languages are supported in R Markdown through the knitr package. The language name is indicated by the first word in the curly braces after the three opening backticks. For example, the little r in ```{r} indicates that the code chunk contains R code, and ```{python} is a Python code chunk.

In knitr, each language is supported through a language engine. Language engines are essentially functions that takes the source code and options of a chunk as the input, and returns a character string as the output. They are managed through the object knitr::knit_engines. You may check the existing engines via:

names(knitr::knit_engines$get())
##  [1] "awk"         "bash"        "coffee"     
##  [4] "gawk"        "groovy"      "haskell"    
##  [7] "lein"        "mysql"       "node"       
## [10] "octave"      "perl"        "psql"       
## [13] "Rscript"     "ruby"        "sas"        
## [16] "scala"       "sed"         "sh"         
## [19] "stata"       "zsh"         "highlight"  
## [22] "Rcpp"        "tikz"        "dot"        
## [25] "c"           "fortran"     "fortran95"  
## [28] "asy"         "cat"         "asis"       
## [31] "stan"        "block"       "block2"     
## [34] "js"          "css"         "sql"        
## [37] "go"          "python"      "julia"      
## [40] "sass"        "scss"        "theorem"    
## [43] "lemma"       "corollary"   "proposition"
## [46] "conjecture"  "definition"  "example"    
## [49] "exercise"    "proof"       "remark"     
## [52] "solution"

Section 2.7 of the R Markdown Definitive Guide (Xie, Allaire, and Grolemund 2018) has shown examples of using Python, Shell, SQL, Rcpp, Stan, JavaScript, CSS, Julia, C, and Fortran code in R Markdown. In this chapter, we will show more language engines. First, let’s reveal how a language engine works by registering a custom language engine.

References

Xie, Yihui, J. J. Allaire, and Garrett Grolemund. 2018. R Markdown: The Definitive Guide. Boca Raton, Florida: Chapman; Hall/CRC. https://bookdown.org/yihui/rmarkdown.