Here’s a summary of the steps to configure the R kernel in VSCode on your MacBook Pro for easy reproducibility:

Summary of Steps#

  1. Install Prerequisites

    • Install Python 3 (if not already installed).

    • Install R from CRAN.

  2. Set Up a Virtual Environment

    python3 -m venv ~/documents/athena/myenv
    source ~/documents/athena/myenv/bin/activate
    
  3. Install Jupyter and IRKernel

    pip install jupyter jupyter-client
    R -e "install.packages('IRkernel'); IRkernel::installspec(user = TRUE)"
    
  4. Ensure R is in the PATH

    • Add R to your PATH in your ~/.zshrc:

    echo 'export PATH=/usr/local/bin:$PATH' >> ~/.zshrc
    source ~/.zshrc
    
  5. Verify Kernel Installation

    jupyter kernelspec list
    
  6. Install VSCode Extensions

  7. Configure VSCode to Use the Jupyter Server

    • Open VSCode.

    • Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P).

    • Run Jupyter: Select Interpreter to start Jupyter server and select the Python interpreter from your virtual environment.

  8. Open or Create a Jupyter Notebook

    • Open an existing .ipynb file or create a new one.

  9. Select the R Kernel

    • Click on the kernel name at the top right corner of the notebook interface.

    • Select the R kernel from the dropdown menu.

Step-by-Step Commands#

  1. Install Python 3 and R (if not already installed):

    • Follow the installation instructions for Python 3 and R on your MacBook Pro.

  2. Set Up Virtual Environment:

    python3 -m venv ~/documents/athena/myenv
    source ~/documents/athena/myenv/bin/activate
    
  3. Install Jupyter and IRKernel:

    pip install jupyter jupyter-client
    R -e "install.packages('IRkernel'); IRkernel::installspec(user = TRUE)"
    
  4. Add R to PATH:

    echo 'export PATH=/usr/local/bin:$PATH' >> ~/.zshrc
    source ~/.zshrc
    
  5. Verify Kernel Installation:

    jupyter kernelspec list
    
  6. Install VSCode Extensions:

    • Open the Extensions view in VSCode (Ctrl+Shift+X or Cmd+Shift+X).

    • Install the Jupyter Extension.

    • Optionally, install the R Extension.

  7. Configure Jupyter Server in VSCode:

    • Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P).

    • Run Jupyter: Select Interpreter to start Jupyter server and select the Python interpreter from your virtual environment.

  8. Open or Create a Jupyter Notebook:

    • Open an existing .ipynb file or create a new one in VSCode.

  9. Select the R Kernel:

    • Click on the kernel name at the top right corner of the notebook interface.

    • Select the R kernel from the dropdown menu.

By following these summarized steps, you should be able to replicate the setup on your MacBook Pro successfully. If you encounter any issues or need further assistance, please let me know.

.Rhistory#

When you save a session after q()

install.packages("IRkernel")
install.packages("IRkernel")
Sys.setenv(JUPYTER_PATH = "/Users/apollo/Documents/Athena/myenv/bin/jupyter")
Sys.setenv(PATH = paste(Sys.getenv("PATH"), "/Users/apollo/Documents/Athena/myenv/bin", sep=":"))
system2(Sys.getenv("JUPYTER_PATH"), c("kernelspec", "list"))
IRkernel::installspec()
install.packages("IRkernel")
q()
Sys.setenv(JUPYTER_PATH = "/Users/apollo/Documents/Athena/myenv/bin/jupyter")
Sys.setenv(PATH = paste(Sys.getenv("PATH"), "/Users/apollo/Documents/Athena/myenv/bin", sep=":"))
Sys.getenv("JUPYTER_PATH")
system2(Sys.getenv("JUPYTER_PATH"), c("kernelspec", "list"))
IRkernel::installspec()
q()
install.packages("IRkernel")
IRkernel::installspec(user = FALSE)  # Use user = FALSE to install system-wide if necessary
IRkernel::installspec(user = TRUE)  # Use user = FALSE to install system-wide if necessary
Sys.setenv(JUPYTER_PATH = "/Users/apollo/Documents/Athena/myenv/bin/jupyter")
Sys.setenv(PATH = paste(Sys.getenv("PATH"), "/Users/apollo/Documents/Athena/myenv/bin", sep=":"))
Sys.getenv("JUPYTER_PATH")
system2(Sys.getenv("JUPYTER_PATH"), c("kernelspec", "list"))
jupyter notebook
q()