How to run Julia and R in LyX via knitr
Shuvomoy Das Gupta
May 6, 2025
Abstract
In this blog, we will discuss how to install and run Julia and R from LyX using knitr.
1 What is knitr?
Relevant resources.
Yihui Xie. Dynamic Documents with R
and knitr, Second Edition. Chapman and
Hall/CRC, 2017.
Yihui Xie, Christophe Dervieux, and Emily
Riederer. R markdown cookbook. Chapman
and Hall/CRC, 2020. Available at:

A LyX demo by Yihui Xie available at:

A knit-manual for LyX by Yihui Xie available
at: 

The knitr package is a general-purpose literate programming tool that allows us to create document formats including LA-
TEX, HTML, and Markdown while running programming languages such as R, Python, Julia, C++, more in the same doc-
ument. Not only that, we can use knitr as a literate programming tool for running code such as Julia code in a LyX document.
It is actually written in R, so for it to work properly, we will go through the following steps.
2 Install R, RStudio, and Julia
First, install Julia from  and install R from 
After installing Julia and R, add the location of the R folder (could be something like   󱎻󱎹󱎷󱎸󱎳󱎹󱎷)
to your path. For example, if the operating system is windows, then go to ‘edit the system environment variables‘ then on
‘system variables‘ select ‘Path‘ ‘Edit‘ ‘New‘ and paste the R folder location (e.g.,   󱎻󱎹󱎷󱎸󱎳󱎹󱎷).
You may need to restart the computer for it to take effect.
Okay, great. Now open Julia REPL, and from there run (note that  will be different for different OS etc,
ensure that it is associated with the same R folder location added to path in the earlier step, with the location of the R binary
e.g., 󱎹󱎷 removed)
 󰙐   󱎻󱎹󱎷󱎸󱎳
 

 
    
 
    
Next, install RStudio from 
Now in RStudio, we will run the folloing lines one by one in the console:
  󰙐 


  󰙐 
󱕏󱕐


After running all these, it is time for testing if knitr is working as expected in RStudio. For that, in RStudio, go to 
    and paste the following content in the file and test if you can test each code chunks by pressing the
green run button.

    
 
 󱎵󱎳󱎵󱎸󱎳󱎷󱎴󱎵

 
 

󰮎󰮎󰮎  󰙐
󱕏󱕐󰏰 󰙐 
󰮎󰮎󰮎
     
󰮎󰮎󰮎

 󰙐 󱎷󱎵

󰮎󰮎󰮎
    
       󰮎󰮎     
 󰮎 󰮎       
󰮎󰮎󰮎
 󰙐  󰙈 󱎴󱎵

󰮎󰮎󰮎
   
󰮎󰮎󰮎
 󰙐 󱎵     
󰮎󰮎󰮎
   
󰮎󰮎󰮎
 󰙐 󱎵
󰮎󰮎󰮎
󰮎󰮎󰮎
 
 󰙐 󱎳 󱎴󱎳 󰙐󱎴󱎳󱎳
 󰙐 
 
󰮎󰮎󰮎
3 Install LyX and set it up to run knitr
I will assume that a tex distribution such as TeXlive is installed. Now, first, install LyX from . Now go
through the following steps:
1. Open a new L
Y
X document;
2. Go to    and insert the module named Rnw (knitr);
3. Add code highlighting via 
at the very beginning of the LyX document (before title etc) and paste the fol-
lowing code
󰙙󰙙 󰙐 󰙐󰙕󰙕󰙐

           
󰏰
 󰙐 
 󰙐 
󰙐
        
 󰙙   
 󰙙 󰏰
 󰙙   󰙐 
󱎳
󰙐  
 

󰏰
 󰙐 
 󰙐 
󰄈
4. Then insert the following Julia code in the document with either Insert T
E
X Code or the Custom Insets Chunk
environment:
󰙙󰙙󰙕󰙕󰙐
    
 
 󰙐 󰙈
 

󰄈
which after compilation, will look like the following:
# This is Julia code
function 
󰙐 󰙈
return
end
## my_add (generic function with 1 method)
Let us test the chunk called ‘my_add‘ as follows. We do so by adding the following chunk (with either Insert T
E
X Code or
the Custom Insets Chunk environment again)
󰙙󰙙󰙕󰙕󰙐
    
 󰙐 󱎺
 󰙐 󱎻
 󰙐  
󰄈
,which, on the compiled pdf, will look like:
# This is Julia code
 󰙐 󱎺
 󰙐 󱎻
 󰙐  
## 15