How to Run R Stats Directly in Jupyter Notebook Locally

Jupyter and R

Motivation

Around 2010, I begun my data science journey with R, after Python. Now days I spend more time in Python, but I am thinking to change my workflow. So, I am trying a new approach.

Regarding data analysis, people spend most of their time running R R-Studio and Python in Jupyter (jupyter-lab or jupyter-notebook).

Note: For programming and any other job, as type a text, notes, etc, I work in Emacs.

As I said, for data science, Jupyter and Python are an amazing match. Furthermore, I really enjoy Jupyter-Lab. However, for teaching data science and just do data analysis (fit models, plots, ETL), I guess that R is better.

Thus, I was thinking: if I return to R, Do I have to switch to Quarto? Researching about Quarto, I found this flow to reach a final report:

Figure 1: Quarto flow from https://bioinformatics.ccr.cancer.gov

At moment, I prefer the nbconverter, Pandoc and my own sty or css files format. So, just out of the blue, I remembered that in some day I run R in Jupyter and decide to try again.

R in Jupyter

Step 1

Make sure you have R in your machine.

Step 2

Open R in terminal and install the packages:

install.packages("languageserver")
install.packages('IRkernel')
install.packages("IRdisplay")

Now, in R (in terminal):

library(IRkernel)
IRkernel::installspec()

Note from https://github.com/IRkernel/IRkernel: Per default IRkernel::installspec() will install a kernel with the name ir and a display name of R. Multiple calls will overwrite the kernel with a kernel spec pointing to the last R interpreter you called that commands from. You can install kernels for multiple versions of R by supplying a name and displayname argument to the installspec() call (You still need to install these packages in all interpreters you want to run as a jupyter kernel!):

# in R 3.3
IRkernel::installspec(name = 'ir33', displayname = 'R 3.3')
# in R 3.2
IRkernel::installspec(name = 'ir32', displayname = 'R 3.2')

Step 3

Run you Jupyter and:

Figure 2: Jupyter-lab with Python and R kernels

Comments

I you use conda, maybe it may be useful: https://www.freecodecamp.org/news/how-to-run-r-programs-directly-in-jupyter-notebook-locally/#heading-run-r-in-jupyter-notebook .

Maybe in the future I will try Quarto, but for now I am happy with Jupyter.

That’s all folks! Regards!

References

 Share!

 
comments powered by Disqus