Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Visual Studio Code to (auto-)format R-Code

Working on Windows 10, no Formatter for the R-Language seems to do its job in Visual Studio Code. I've tried R support for Visual Studio Code and R-Tools, which seem to be the most popular extensions out there.

I've also checked that the path to the R binary file is correct. Nevertheless, when I hit SHIFT + ALT + F I get the "There is no formatter for 'r'-files installed." message.

Moreover I tried to enable the Settings Editor: Format On Type and Editor: Format On Save, but without luck.

Haven't been able to find other posts about this problem online. Any ideas? Big thanks

like image 541
Comfort Eagle Avatar asked Aug 07 '19 13:08

Comfort Eagle


People also ask

How do I enable auto format in VS code?

VS Code Auto Format On SaveOpen Visual Studio Code editor. Click the “Settings” gear icon in the bottom-left corner. Search “Formatter” and click the “Editor: Default Formatter” option. From the drop-down menu, select whichever code formatter you want to use.

How do I fix code formatting in Visual Studio?

The code formatting is available in Visual Studio Code through the following shortcuts: On Windows Shift + Alt + F. On Mac Shift + Option + F. On Linux Ctrl + Shift + I.

How do I get auto suggestion codes in Visual Studio?

You can trigger IntelliSense in any editor window by typing Ctrl+Space or by typing a trigger character (such as the dot character (.) in JavaScript).


2 Answers

Installing R Language Server and R LSP Client should do what you want

  1. Open R Studio and install languageserver, tying this in the console window: install.packages("languageserver")

  2. Install R LSP Client Extension in VS Code

  3. Toggle Format on Type, Format On Save, and Format On Paste settings in VS Code editor preferences, depending on your needs.
like image 158
user3451039 Avatar answered Sep 25 '22 11:09

user3451039


Answer works as per user3451039, but if you are using another formatter in editor.defaultFormatter (such as Prettier), you may need to add the following to your settings.json in VSCode:

  "[r]": {
    "editor.defaultFormatter": "REditorSupport.r-lsp"
  },
like image 20
Ctrl-Zed Avatar answered Sep 25 '22 11:09

Ctrl-Zed