Using R from VS Code

Published

Thu, 12 of September, 2024

Modified

Thu, 12 of September, 2024

Caution

Web page construction in progress…

(as of 25/5/2022) # Reference

Install VS Code

Install R ext on VS Code

Install radian ext on

One of the first suggestions is to use the radian terminal. That’s not a VS Code extension but an application written in Python—which means your system needs Python installed in order for radian to run. I already have Python and the conda package manager installed on my Mac, so I used the following installation command for radian:

See the How to set up VS Code for R video tutorial for easy instructions on how to install Python for use with R and RStudio.

install Python

brew install python

# Python has been installed as
#   /usr/local/bin/python3
# 
# Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
# `python3`, `python3-config`, `pip3` etc., respectively, have been installed into
#   /usr/local/opt/python@3.12/libexec/bin
  • If you do not need a specific version of Python, and always want Homebrew’s python3 in your PATH:
brew install python3

OK, but which Python version am I running?

  • Version: Python 3.12.5 for macOS ….

Ensure Python and pip are installed:

python3 --version # Python 3.12.5
pip3 --version  # PROBLEM! # pip 21.2.4 (python 3.9)
# pip 21.2.4 from
# /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/site-packages/pip (python 3.9)

Gotcha (part 1/2)!!!

Problem: mismatch tra pip (3.9) e python 3.12)

The error externally-managed-environment typically occurs when trying to install or upgrade packages in a Python environment that is managed by the system or another package manager (like Homebrew on macOS). To resolve this, you can use a virtual environment to isolate your Python installation and manage packages independently.

Here are the steps to create a virtual environment and install radian within it:

Create a virtual environment:

# Create a virtual environment
python3.12 -m venv myenv
# Activate the virtual environment
source myenv/bin/activate
# Upgrade pip within the virtual environment
pip install --upgrade pip
# Install radian within the virtual environment
pip install radian

Where is this new Python 3.12?

/usr/local/Cellar/python@3.12/3.12.5/Frameworks/Python.framework/Versions/3.12/bin/python3.12

looks like Homebrew puts Python in /usr/local/Cellar/

Where is this new pip/ (pip3 install radian)

/usr/local/lib/python3.12/site-packages/pip

… now install radian

#  Now, try installing radian again:
pip3 install radian

Where is this radian (Gotcha (part 2/2)!!!)

OKKIO: Move it from virtual environemnt a real environment

which radian # /Users/luisamimmi/myenv/bin/radian  OLD!!!
source myenv/bin/activate
cp /Users/luisamimmi/myenv/bin/radian /usr/local/bin/
chmod +x /usr/local/bin/radian

# check 
which radian # /usr/local/bin/radian   NEW !!!!

In sum:

  • Python 3.12.5 is in: /usr/local/Cellar/python@3.12/3.12.5/Frameworks/Python.framework/Versions/3.12/bin/python3.12
  • Pip ?? is in: /usr/local/lib/python3.12/site-packages/pip
  • radian is in: /usr/local/bin/radian

____ qui ______

After radian installed

Add to PATH: If radian is installed but not in your PATH, you can add it manually. For example, if radian is installed in /usr/local/bin, you can add this to your PATH in your .zshrc file:

echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

Python

3 ways to work with python

— 1) Launch Idle shell + execute

(it comes installed with the new python3)

— 2) Execute program in shell

python3 Python3_9_test.py  
# it prompts for name etc... 

— 3) Execute program in VS code

Just look for the arrow from the top of the .py file


Tweak Visual Studio Code settings for R

The easiest way to change a VS Code setting is to open up settings in the user interface:

  • [from Finder] Go to /Users/luisamimmi/Library/Application Support/Code/User/settings.json
  • [from VS code] Open by ctr+shift+P and then type Preferences: Open User Settings (JSON))

Then edit that setting file. Here is a lits of possible settings for R users GH link

Open in finder

From shell

open -a Finder /Library/Frameworks/R.framework/Resources

modify R Extension settings

I need to specify the path to R R > Rpath: mac

# here are the commands to figure it out 
R 
R.home("bin")
# [1] "/Library/Frameworks/R.framework/Resources/bin"

To color the console I need to have radian path in R settings –> R> Rterm:MAC
R > Rterm: Mac

"r.rterm.mac": "/usr/local/bin/radian"

Sending R code to terminal

  • Ctrl + Shift + P and choose “R: Create R terminal” command
  • oppure lancio R interactive dalla tendina in Terminal

Path to keybindings.json

~/Library/Application Support/Code/User/keybindings.json

Info on Keybinding

https://code.visualstudio.com/docs/getstarted/keybindings

F&$%ing lintr in VSCode

https://github.com/r-lib/lintr#continuous-integration

You can tone it down by creating your own `~/.lintr/ file like so

linters: with_defaults(
  line_length_linter = NULL,
  open_curly_linter = NULL, 
  commented_code_linter = NULL,
  trailing_whitespace_linter = NULL)