Minyi Han
Minyi Han

Reputation: 827

How to use jupyter notebook with R kernel in VS Code?

Now I can use JupyterLab with an R kernel in a web browser and use VS Code to edit Jupyter notebook code with a Python kernel. Is it possible to edit R code in VS Code using Jupyter notebook mode? Thanks for the comments and solutions.

Upvotes: 8

Views: 5081

Answers (3)

Ali Balubaid
Ali Balubaid

Reputation: 1

these notes in addition to the response by another response were very helpful. if you are using this with R versions in conda environments, first create and activate the conda environment:

mamba activate myenv
mamba install -c conda-forge r-irkernel 
mamba install jupyter

Run R while the environment is active:

IRkernel::installspec(name='r44_myenv', displayname='R 4.4.1 (myenv)')

Make sure you choose a unique name/displayname to avoid overlapping registrations.

You then follow the same steps above:

  1. restart VSCode
  2. Type Ctrl + Shift + P to search for "Jupyter: Create New Blank Notebook"
  3. Select Kernel in the top right.
  4. Select Another Kernel >> Jupyter Kernel >> R (myenv)

Hope that helps.

Upvotes: 0

Ichsan
Ichsan

Reputation: 828

@testing_22 it works with me too

just add some note from my experience

It will failed If you run IRkernel::installspec() from RStudio or from Jupyter Conda environment failed way

Please run this syntax with VSCode terminal

install.packages('IRkernel')
IRkernel::installspec()

enter image description here

The rest is same, please restart VSCode and select "R" kernel from VSCode

Upvotes: 1

testing_22
testing_22

Reputation: 2585

Yes, it is possible. You just have to install IRkernel (R kernel) first.

According to the docs, run both lines

install.packages('IRkernel')
IRkernel::installspec()  # to register the kernel in the current R installation

Then, restart your VSCode and:

  • Type Ctrl + Shift + P to search for "Jupyter: Create New Blank Notebook" enter image description here

  • Click on the button right below ellipsis in upper right corner to choose kernel

    enter image description here

  • Switch to the desired kernel, in this case R's enter image description here

That's it!

Upvotes: 5

Related Questions