Skip to content

Instantly share code, notes, and snippets.

@strengejacke
Last active October 29, 2024 23:21
Show Gist options
  • 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"
}
@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