Skip to content

Instantly share code, notes, and snippets.

@strengejacke
Last active May 8, 2024 17:39
Show Gist options
  • Star 51 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save strengejacke/82e8d7b869bd9f961d12b4091c145b88 to your computer and use it in GitHub Desktop.
Save strengejacke/82e8d7b869bd9f961d12b4091c145b88 to your computer and use it in GitHub Desktop.
VS Code setup for R
// save to windows-user directory
linters: with_defaults(object_name_linter = NULL,
object_length_linter(50),
commented_code_linter = NULL,
object_usage_linter = NULL,
line_length_linter(120),
cyclocomp_linter = cyclocomp_linter(50))
[
/*
Keybindings for R Language ------------------------------------------------
*/
// Build RD files for R functions / packages
{
"description": "R Build Docs",
"key": "ctrl+shift+d",
// "command": "r.document",
"command": "r.runCommand",
"args": "devtools::document(roclets = c('rd', 'collate', 'namespace'))",
"when": "editorTextFocus || terminalFocus"
},
// Insert pipe-operator
{
"description": "R Pipe Operator",
"key": "ctrl+shift+m",
"command": "type",
"args": {"text": " |> "},
"when": "editorTextFocus || terminalFocus",
},
// Insert <- assignment
{
"description": "R Assignment Arrow",
"key": "alt+-",
"command": "type",
"args": {"text": " <- "},
"when": "editorTextFocus || terminalFocus",
},
// Build R *source* package (e.g. for submission to win-builder)
{
"description": "R Build Source Package",
"key": "ctrl+shift+b",
// "command": "r.build",
"command": "r.runCommand",
"args": "devtools::build()",
"when": "editorTextFocus || terminalFocus",
},
// Install R package (for developers)
{
"description": "R Install Package",
"key": "ctrl+shift+i",
// "command": "r.install",
"command": "r.runCommand",
"args": "devtools::install(quick=TRUE,upgrade=FALSE)",
"when": "resourceLangId == 'r'"
},
// Restart R session, to clean workspace and namespace
{
"description": "R Restart Session",
"key": "ctrl+shift+F10",
"command": "r.runCommand",
"when": "editorLangId == 'r'",
"args": "startup::restart()"
},
// Loads current package (for developers), local install
{
"description": "R Load Package",
"key": "ctrl+alt+l",
"command": "r.loadAll",
"when": "editorLangId == 'r' || editorTextFocus && editorLangId == 'rmd'"
},
// Update R packages
{
"description": "R Update Packages",
"key": "ctrl+shift+u",
"command": "r.runCommand",
"args": "vsCodeSnippets::update_R_packages()",
"when": "resourceLangId == 'r' || terminalFocus"
},
// Create reprex (must be in clipboard)
{
"description": "R Create Reprex",
"key": "ctrl+shift+x",
"command": "r.runCommand",
"args": "reprex::reprex()",
"when": "resourceLangId == 'r'"
},
// Compile report (knit R Script)
{
"description": "R Compile Report",
"key": "ctrl+shift+t",
"command": "r.runCommand",
"args": "rmarkdown::render(input=rstudioapi::getSourceEditorContext()$path)",
"when": "resourceLangId == 'r'"
},
// Show help for function at cursor position
{
"description": "R Show Documentation",
"key": "f1",
"command": "r.helpPanel.openForSelection",
"when": "editorLangId == 'r' || editorTextFocus && editorLangId == 'rmd'"
},
/*
Editor-specific keybindings -----------------------------------------------
*/
// View R workspace
{
"description": "View: Show R Workspace",
"key": "ctrl+shift+w",
"command": "workbench.view.extension.workspaceViewer"
},
// Format Code
{
"key": "ctrl+shift+a",
"command": "editor.action.formatSelection",
"when": "editorHasDocumentSelectionFormattingProvider && editorTextFocus && !editorReadonly"
},
// Show all symbols (objects, ...)
{
"key": "ctrl+oem_period",
"command": "workbench.action.showAllSymbols"
},
// new R file
{
"key": "ctrl+n",
"command": "r.newFileDocument"
},
// new file
{
"key": "ctrl+shift+n",
"command": "welcome.showNewFileEntries"
},
// switch position of panel
{
"key": "shift+alt+7",
"command": "workbench.action.positionPanelRight"
},
{
"key": "shift+alt+8",
"command": "workbench.action.positionPanelBottom"
},
// other stuff
{
"key": "ctrl+alt+l",
"command": "quarto.previewShortcut",
"when": "editorLangId == 'dot' || editorLangId == 'mermaid' || editorLangId == 'quarto'"
},
{
"key": "ctrl+l",
"command": "workbench.action.terminal.clear",
"when": "terminalFocus"
},
// requires extension "multi-command"
{
"key": "ctrl+shift+j",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"workbench.action.toggleMaximizedPanel",
"workbench.action.terminal.focus"
]
},
"when": "editorTextFocus || terminalFocus"
},
// lock scrolling, to synch scrolling across editor windows
{
"key": "ctrl+t",
"command": "workbench.action.toggleLockedScrolling"
},
/*
Switch default keybindings for these commands -----------------------
*/
{
"key": "shift+f12",
"command": "references-view.findReferences",
"when": "editorHasReferenceProvider"
},
{
"key": "shift+alt+f12",
"command": "editor.action.goToReferences",
"when": "editorHasReferenceProvider && editorTextFocus && !inReferenceSearchEditor && !isInEmbeddedEditor"
},
{
"key": "ctrl+oem_5",
"command": "editor.action.jumpToBracket",
"when": "editorTextFocus"
},
{
"key": "ctrl+shift+oem_5",
"command": "workbench.action.splitEditor"
},
{
"key": "alt+oem_5",
"command": "workbench.action.splitEditorDown"
},
/*
Unbinding pre-defined shortcuts ----------------------------------------
*/
{
"key": "ctrl+k ctrl+f",
"command": "-editor.action.formatSelection",
"when": "editorHasDocumentSelectionFormattingProvider && editorTextFocus && !editorReadonly"
},
{
"key": "ctrl+oem_period",
"command": "-editor.action.quickFix",
"when": "editorHasCodeActionsProvider && editorTextFocus && !editorReadonly"
},
{
"key": "ctrl+t",
"command": "-workbench.action.showAllSymbols"
},
{
"key": "ctrl+shift+d",
"command": "-workbench.view.debug",
"when": "viewContainer.workbench.view.debug.enabled"
},
{
"key": "ctrl+shift+x",
"command": "-workbench.view.extensions",
"when": "viewContainer.workbench.view.extensions.enabled"
},
{
"key": "ctrl+shift+u",
"command": "-workbench.action.output.toggleOutput",
"when": "workbench.panel.output.active"
},
{
"key": "ctrl+shift+w",
"command": "-workbench.action.closeWindow"
},
{
"key": "ctrl+shift+l",
"command": "-quarto.previewShortcut",
"when": "editorLangId == 'dot' || editorLangId == 'mermaid' || editorLangId == 'quarto'"
},
{
"key": "ctrl+shift+n",
"command": "-workbench.action.newWindow"
},
{
"key": "ctrl+n",
"command": "-workbench.action.files.newUntitledFile"
},
{
"key": "ctrl+alt+win+n",
"command": "-welcome.showNewFileEntries"
},
{
"key": "shift+alt+f12",
"command": "-references-view.findReferences",
"when": "editorHasReferenceProvider"
},
{
"key": "shift+f12",
"command": "-editor.action.goToReferences",
"when": "editorHasReferenceProvider && editorTextFocus && !inReferenceSearchEditor && !isInEmbeddedEditor"
},
{
"key": "shift+f12",
"command": "-goToPreviousReference",
"when": "inReferenceSearchEditor || referenceSearchVisible"
},
{
"key": "ctrl+oem_5",
"command": "-workbench.action.splitEditor"
},
{
"key": "ctrl+shift+oem_5",
"command": "-editor.action.jumpToBracket",
"when": "editorTextFocus"
},
{
"key": "ctrl+shift+b",
"command": "-workbench.action.tasks.build",
"when": "taskCommandsRegistered"
},
{
"key": "shift+f1",
"command": "workbench.action.showCommands"
},
{
"key": "f1",
"command": "-workbench.action.showCommands"
},
{
"key": "ctrl+shift+t",
"command": "-workbench.action.reopenClosedEditor"
}
]
{
// Place your snippets for r here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
"VS Code Debugging": {
"prefix": ["deb", "debugging"],
"body": ["vsCodeSnippets::load_debug_pkg()"],
"description": "Loads package for debugging and refreshes breakpoints"
},
"VS Code Debugging 2": {
"prefix": ["fixit"],
"body": [
"ret <- pkgload::load_all()",
"exports <- as.environment(paste0(\"package:\", environmentName(ret$env)))",
"vscDebugger::.vsc.refreshBreakpoints(list(ret$env, exports))"
],
"description": "Loads package for debugging and refreshes breakpoints (no need for vcCodeSnippets)"
}
}
{
// workspace, workbench and window settings
"workbench.startupEditor": "none",
"workbench.panel.defaultLocation": "right",
"workbench.colorTheme": "Default Dark+",
"security.workspace.trust.untrustedFiles": "open",
"window.commandCenter": true,
"explorer.confirmDelete": false,
"diffEditor.renderSideBySide": false,
"diffEditor.ignoreTrimWhitespace": false,
"diffEditor.experimental.showMoves": true,
// terminal settings
"terminal.integrated.defaultProfile.windows": "R Terminal",
"terminal.integrated.fontWeightBold": "normal",
"terminal.integrated.enableMultiLinePasteWarning": "never",
"terminal.integrated.cursorStyle": "underline",
"terminal.integrated.cursorBlinking": true,
// editor settings
"editor.stickyScroll.enabled": true,
"editor.minimap.enabled": false,
"editor.formatOnPaste": true,
"editor.unicodeHighlight.nonBasicASCII": true,
"editor.tabSize": 2,
"editor.detectIndentation": false,
"editor.defaultFormatter": "REditorSupport.r",
"editor.guides.indentation": false,
"editor.fontFamily": "'Fira Code', Hasklig, Consolas, 'Courier New', monospace",
"editor.fontLigatures": true,
"editor.inlineSuggest.enabled": true,
"editor.guides.bracketPairs": "active", // adds guides to matching brackets
"editor.accessibilitySupport": "off",
"editor.rulers": [
{"column": 80, "color": "#5a5a5a80"}, // right rule
{"column": 95, "color": "#5a5a5a30"}, // right rule
{"column": 120, "color": "#5a5a5a40"} // extra right rule
],
// remember (and restore) unsaved files even when switching workspaces
"files.hotExit": "onExitAndWindowClose",
// make sure ".rmd" files are treated as R Markdown
"files.associations": {
"*.rmd": "rmd"
},
// other extension settings
// allows PathAutocomplete to start completion after typing "."
// use "./<filename>" for correct file paths
"path-autocomplete.pathMappings": {
".": "${workspace}"
},
// git settings
"git.autofetch": true,
"git.confirmSync": false,
"git.suggestSmartCommit": false,
// r settings
"r.alwaysUseActiveTerminal": true,
"r.bracketedPaste": true,
"r.plot.useHttpgd": true,
"r.plot.defaults.fullWindowMode": true,
"r.rpath.windows": "C:\\Program Files\\R\\R-4.3.3\\bin\\R.exe",
"r.rterm.windows": "C:\\Program Files\\R\\R-4.3.3\\bin\\x64\\Rterm.exe",
// "r.rterm.windows": "C:\\Users\\mail\\AppData\\Local\\Programs\\Python\\Python311\\Scripts\\radian.exe",
"r.removeLeadingComments": true,
"r.session.levelOfObjectDetail": "Normal",
"quarto.mathjax.theme": "dark",
// github settings
"githubPullRequests.fileListLayout": "tree",
"githubPullRequests.defaultMergeMethod": "squash",
"githubPullRequests.pullBranch": "never",
"github-actions.org-features": true,
// TODO highlighter
"todohighlight.include": [
"**/*.r"
],
"todohighlight.keywords": [
{
"text": "TODO",
// "color": "#1ab7ea",
"color": "#fffc00",
"backgroundColor": "rgba(245, 145, 0, .66)",
}
],
// StatusBar Debugger
"statusbarDebugger.actions": [
"step_over",
"step_into",
"step_out",
"restart",
"stop"
],
"statusbarDebugger.actionsIcons": [
"$(debug-step-over)",
"$(debug-step-into)",
"$(debug-step-out)",
"$(debug-step-back)",
"$(debug-stop)"
],
"debug.toolBarLocation": "docked",
// Codesnap
"codesnap.boxShadow": "rgba(0, 0, 0, 0.55) 0px 20px 60px",
"codesnap.backgroundColor": "#6090a0",
"codesnap.containerPadding": "1em",
"codesnap.showWindowControls": false,
"codesnap.shutterAction": "copy",
// todo tree
"todo-tree.highlights.customHighlight": {
"TODO": {
"background": "#F5910044",
"foreground": "#fffc00FF"
},
"BUG": {
"icon": "bug",
"background": "#D3262655",
"foreground": "#FF3300FF"
},
"HACK": {
"icon": "tools"
},
"FIXME": {
"icon": "flame",
"background": "#00996844",
"foreground": "#25D366FF"
}
},
// GitHub Copilot
"github.copilot.enable": {
"files.associations": {
"*.R": "r"
},
"languages": [{
"id": "r",
"extensions": [ ".r", ".R", ".rmd", ".Rmd" ],
"aliases": [ "R" ]
}],
"quarto": false,
"*": true
},
"chatgpt.lang": "en"
}
@strengejacke
Copy link
Author

strengejacke commented Aug 8, 2022

Setting up VS Code for R

Basics

The very basic things you need to run R from VS Code are some packages and extension.

  1. Install R package "languageserver" - this is required.
  2. For better looking plots, install R package "httpgd" - this is optional.
install.packages(c("languageserver", "httpgd"), dependencies = TRUE)

Don't forget to activate httpgd in the settings:

image

  1. In VS Code, install the REditorSupport extension - this is required.

That's it. Of course, you may want to add some more extension and tweak some settings or keybindings, but these are the minimum steps you need to do. You may want to look at the useful resouces I posted below on how to setup VS Code, or my list if recommended extensions.

Some people suggest installing Python and radian as better terminal - I did this, but un-installed it again because I didn't want to install Python just for a "better" terminal. If you have installed Python anyway, I would definitely recommend radian. Another reason for not using radian: I'm working on three different computers, all with different user paths, and my VS Code settings and extensions sync across computers - however, the path to radian seems to be hard-coded only, so this wouldn't work (I'm not sure if I have missed something here, let me know!).

Workspace

I think one important and useful concept are "Workspaces", which are roughly comparable to RStudio projects. If you're a package developer, each package should have an own workspace file (like an RStudio project file .RProj).

If you want to add existing folders to a (new) workspace, I would:

  1. Close currently opened workspace, if any
  2. Goto menu File -> Add Folder to Workspace, and select the folder of your package/project/...
  3. Goto menu File -> Save Workspace as, and save the workspace file in that folder - and you're done.

Many useful functions, and the scope of find-functionality, can be extended (or limited) to the workspace, which is what you most often want.

Keybindings

Most of the keybindings and settings shown above in the settings.json and keybindngs.json are automatically generated when you open/edit settings or keybindings in VS Code. I just saved them here to remember what I changed from the defaults, in case I have to setup VS Code from scratch.

Pipe and Arrow

The most important keybindings are probably <- and |> (or %>%). See keybindings above - you need to add following chunks to your keybindings.json file:

    // Insert pipe-operator
    {
      "description": "R Pipe Operator",
      "key": "ctrl+shift+m",
      "command": "type",
      "args": {"text": " |> "},
      "when": "editorTextFocus",
    },
    // Insert <- assignment
    {
      "description": "R Assignment Arrow",
      "key": "alt+-",
      "command": "type",
      "args": {"text": " <- "},
      "when": "editorTextFocus",
    }

New R file

As I only work with R and I rarely use the terminal for small R code chunks (instead I use a temporary R file for all junk chunks), I have assigned ctrl+N to open an R file directly:

    // new R file
    {
      "key": "ctrl+n",
      "command": "r.newFileDocument"
    }

Other keybindings

It's really a matter of personal taste and whether you develop R packages or not if you need more keybindings (see again keybindings.json above). I highly recommend looking at the keybindings cheat sheet, in particular section "Navigation" (how to navigate to files/symbols/functions). Note the different terminology!

Not that if you have German keyboard layout for instance, some keybindings from the cheat sheet need to be "translated", like Ctrl+Shift+[ becomes Ctrl+Shift+ß, and Ctrl+Shift+] becomes Ctrl+Shift+´.

Settings

Well, there are probably too many settings to discuss here. One thing I found useful was "files.hotExit": "onExitAndWindowClose", which restores unsaved files on exit and when you switch workspaces and doesn't require to save them (to mimic same behaviour as in RStudio).

More

(soon?)

@strengejacke
Copy link
Author

strengejacke commented Aug 8, 2022

Recommended Extensions and Packages

R packages (required, suggested)

  • install.packages(c("languageserver", "httpgd"), dependencies = TRUE)

Extensions to work with R

Extensions for R developers

Other useful Extensions

  • vscode-pdf (PDF Plugin, view PDF from inside VS Code)
  • Todo Tree (list all TODO, FIXME, BUG, ... comments, and also highlight them; an extension with fewer, but similar functionality is TODO Highlight, which only highlights tokens in comments, but adds no overview-panel to the sidebar)
  • Liveshare (collaborative working in real time)
  • Codesnap (beautiful screenshots of code snippets)
  • Excel Viewer (edit and view CSV and Excel files)

Themes

Dark

I personally use the default Dark+ theme. None of the other external themes have convinced me so far.

Light

I haven't used light themes yet, I don't like any of the built-in themes very much. Due to the thin (or no) window borders, the app window is very bright for light themes. While I prefer light themes in RStudio, I use the dark default theme in VS Code.

@strengejacke
Copy link
Author

strengejacke commented Aug 8, 2022

Setting up debugger for R packages

Installation

Required packages

  • install.packages(c("R6", "jsonlite"), dependencies = TRUE)
  • remotes::install_github("ManuelHentschel/vscDebugger")

Required extension

Usage

See also https://www.youtube.com/watch?v=i4Q3SWV9pfo&ab_channel=Daniel (a bit outdated, though)

Steps to make debugging R packages with "breakpoints" in VS Code work:

  1. Install R Debugger extension and vscDebugger package (https://github.com/ManuelHentschel/vscDebugger)
  2. Open VS Code and the workspace file from your R package. Select the "R debugger" pane from the left panel. Click on "Show all automatic debug configuration".
  3. Click "Add config (workspace)", select "R: Launch workspace"
  4. This is what your workspace file could look like:
{
  "folders": [
    {
      "path": "."
    }
  ],
  "launch": {
    "version": "0.2.0",
    "configurations": [
    {
      "type": "R-Debugger",
      "name": "Launch R-Workspace",
      "request": "launch",
      "debugMode": "workspace",
      "workingDirectory": ""
    }
    ]
  }
}
  1. Either:
  • use browser(): add the browser() command into the code where you want to stop. Thus, you must remember to remove the browser() command later ;-) Open the function / file you want to debug, enter browser() into the respective code line.
  • use breakpoints: in the editor, click on the left side of the line-number where you want to set a breakpoint to enter debugging.
  1. In the "Debugger Panel" (left side), start "Launch and Debug workspace". Your status bar at the bottom turns into orange.
  2. Now create an empty R file, or run your commands from the R terminal.
  • If you use browser(): Run pkgload::load_all() (you may want to add a shortcut to r.loadAll - see keybindings.json above).
  • If you use breakpoints: Run following code, which loads your package and refreshes breakpoints you set (I have defined a snippet for this, see above):
ret <- pkgload::load_all()
exports <- as.environment(paste0("package:", environmentName(retenv)))
vscDebugger::.vsc.refreshBreakpoints(list(retenv, exports))
  1. Run the sample code that calls your functions you want to debug. Your browser() statement or breakpoints don't need to be in that function, they can also be in any sub-function.
  2. Navigate with the debugger menu bar or F10/F11/etc. through the code. You see all relevant objects in the debugger panel on the left. The debugger stops in your function at that position, where you inserted browser() or added a breakpoint. Now you can go through your code step-by-step.
  3. To inspect objects/symbols/variabels/..., just add them to the Watchlist in the left Debugger Panel, or type something like print(object) into the Debugger Console.

If there's any more convenient way, let me know 🙂

@strengejacke
Copy link
Author

strengejacke commented Aug 8, 2022

FAQ

How do I update packages?

In the R panel, under Help pages, look for Install CRAN packages and find the update button:

image

Alternatively, you could install the vsCodeSnipptes package from GitHub and add a shortcut to the update_R_packages() function (see keybindings.json above)

RStudio or VS Code?

IMHO, both are good IDEs. It doesn't really matter much which one you take. From my point of view, for both "R users" and "R developers", it makes no big difference which IDE you use, and I could recommend both for either use case. Only: whenever you switch from one tool to the other, take some time (1-2 weeks) to change your most common habits, and after another few weeks you're probably comfortable with either tool.

It probably boils down to: are you more a keyboard person (VS Code) or mouse person (RStudio)?

Pro VS Code:

  • Fast, definitely way better GitHub integration (see here), more editor features, many extensions, much more customization

Pro RStudio:

  • Less customization (can be overwhelming in VS Code), important features are more accessible to "non-programmers" and less tech-affine people, easier configuration (works faster out of the box), better integration of devtools and RStudio R packages (like importing data sets, building/testing packages, creating new package skeletons)

Do you miss something in VS Code you had in RStudio?

  • Yes, I don't know how/if I can open a HTML page from the webview in a browser. In RStudio, you can easily open the viewer pane in a browser by clicking on an icon.
  • IntelliSense auto-completion doesn't seem to suggest method arguments based on the correct method dispatch, see following video from RStudio, which, depending on the supplied argument, will provide the accurate argument list, if there are different methods:
autocomplete.mov

Any downsides you found so far?

  • The default "install package" (Ctrl+Shift+I, mostly for developers) does much more than the equivalent command in RStudio, so it takes longer to install a local package. I have been too lazy yet to change that shortcut.
  • The default R Terminal (which I use), feels a bit less smooth than the terminal integrated in RStudio.
  • Updating a few packages like cli or rlang don't work, since the session watcher seems to use them. Haven't found a good way how to resolve this issue, beside starting good old R and updating those two packages there.

@strengejacke
Copy link
Author

strengejacke commented Aug 8, 2022

Placeholder

@strengejacke
Copy link
Author

strengejacke commented Aug 8, 2022

Miscellaneous

Installed extensions

image
image

Layout

I prefer the "3 column layout":

image

This gives you enough space in the terminal to also see output with longer lines without line breaks:

image

You can easily hide the terminal via shortcut (Ctrl + J) when you have other panes open, like plots. This allows to quickly switch between "terminal" and "plot" view:

image

@strengejacke
Copy link
Author

placeholder

@strengejacke
Copy link
Author

placeholder

@bwiernik
Copy link

bwiernik commented Sep 28, 2022

@strengejacke Is there a way to get VS Code to show the HTML help instead of the plain text help?

@strengejacke
Copy link
Author

I think it's already HTML, but not everything is correctly rendered (limitation: equations?).

@bwiernik
Copy link

Yeah, that’s what I’m realizing. VS Code has built-in katex support, so I think this is an REditorSupport issue

@bwiernik
Copy link

(REditorSupport has since fixed this)

@ymansiaux
Copy link

Hi @strengejacke , where are we supposed to put the "r.json" file ?
Thanks for your work, I just set up my VScode using your examples :)

@strengejacke
Copy link
Author

Not sure, I think you can try this, and the file will be saved there automatically
image

Else, the file path is:
image

@turbanisch
Copy link

Could you perhaps add some details on how to modify the .linter file? For example, I would like to ignore trailing whitespace and empty lines (at least as long as the cursor is on them) and set the maximum line width to infinity.
Do the linter settings work only for a specific language or for all of them?
Also, do you have any clue where the file would need to go on MacOS? I find it very hard to find information on that. Thanks!

@grcatlin
Copy link

@turbanisch the .lintr file has also been a bit of a nightmare for me to figure out, though it isn't too bad once you know where to put it.

You can have .lintr's multiple places and it evaluates in a hierarchy. For example, on my Windows machine, I could place a global .lintr in C:/Users/your_username/Documents/.lintr and also a .lintr in my project directory (running lintr::use_lintr() in the R console will place one in your current directory also).

The lintr package is set up to search your current directory first, then iteratively move up until it gets to the default linters it has (I don't know where to find this). So in the above example, since I have one in my current directory, it would stop there. But, if I didn't have one it would eventually find the one in my documents folder.

Once you have that figured out, then customizing is pretty easy. See the "Customizing Active Linters" section at (https://cran.r-project.org/web/packages/lintr/vignettes/lintr.html). Essentially, if there's something you want to turn off, you just say that_linter = NULL. For example, this is the one I'm using atm:

linters: linters_with_defaults(
    line_length_linter(120), 
    commented_code_linter = NULL,
    object_usage_linter = NULL,
    trailing_blank_lines_linter = NULL,
    trailing_whitespace_linter = NULL
  )
encoding: "UTF-8"

You can see I changed the line length to 120. I don't know if it would accept infinity but you could probably put something ungodly like 1000 there.

The .lintr file only applies to the R language but I assume other languages have linter customization through vs code (and probably much easier customization at that).

As for where to put your global .lintr, if desired, I think it should be something like $HOME/.lintr on Unix.

Hope this helps!

@gadepallivs
Copy link

gadepallivs commented Jan 25, 2024

Thank you for this detailed gist. It is quite helpful.
I would like to use VS code IDE for R development. My work revolves around, shiny, API's, developing workflow pipelines ( Nextflow, snakemake, WDL), connecting to remote DB's, and submitting jobs to remote servers. Any specific extensions you would recommend to make the development process a bit easier in VS code ?

The most used features for me on Rstudio are, "

publish button for shiny apps, Knitr, markdown preview or quarto preview feature, 'cmd+enter' to run each line of code.

Is there a way we can set-up some of these features to work in VS code ? I work on Mac OS, what aspects of the .json settings need to be changed specifically to Mac ? Do you have similar setup guidelines for IOS?

PS: I guess the publish button in Rstudio may be out-of-scope , and wish Rstudio would make it available. The tedious task with shiny apps is to host them ( if not using Rstudio products). Is there any simpler way to host shiny apps from VS code ?

@aminuldu07
Copy link

does the vscode has data viewer like in Rstudio?
I mean in rstudio, i can open a data frame and search within it ? does vs code has this capability? Thanks.

@strengejacke
Copy link
Author

knitr, preview of markdown/quarto files and Ctrl+enter are available in vscode. Not sure about shiny apps though, since I don't use it.
A data viewer is available, but probably less sophisticated. Click on the magnifier next to the data frame in the workbench, where you see all objects in your environment

@strengejacke
Copy link
Author

There's a ShinyUiEditor-Extension, that might be able to do this.

@grcatlin
Copy link

@aminuldu07 @strengejacke The data viewer in VS Code can also be accessed with View(). I.E.,

View(mtcars)
# or
mtcars |> View()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment