Is there a way to use code sections similar to RStudio's # --- or MATLAB's %% in VSCode? I would like to divide my code to sections and selectively run those sections. Is that possible?
Thanks,
Yasir
I think there's a couple different things you're getting at. If you want to divide up your code and run chunks separately similar to jupyter notebooks, I think danlooo is correct in pointing you towards R Markdown (and/or Quarto) as these let you do this. More on Quarto in VS Code here.
I was more interested in the # ---- you mentioned as I don't often want or need to use R Markdown or Quarto. In RStudio, the shortcut to achieve this is with ctrl+shift+r on Windows or cmd+shift+r on Mac. I wanted to emulate this in VS Code and wrote the below snippet and put it as a keyboard shortcut. You can do this if you open up the command palette and look up "Preferences: Open Keyboard Shortcuts (JSON)".
{
"key": "cmd+shift+r",
"command": "editor.action.insertSnippet",
"when": "editorLangId == r && editorTextFocus",
"args": {
"snippet": "# ${TM_SELECTED_TEXT}$0 ----"
}
},
Now, whenever I use this command I can quickly enter a section divider in a regular R script. I can then navigate to different sections at the top of the script:
You can use the "Outline View" to divide your code into section and quickly navigate them. See VS Studio Code's documentation here. In fact, it is better than Rstudio's in some ways because you can define subsections. Note that VS Studio Code by default also shows other stuff in the outline too, but you can turn those off in the settings (such as Fields, Booleans, etc), but make sure you keep Strings enabled as that's what's needed to navigate via commented sections. See my example below:

There is a VSCode Extension for extended syntax highlighting. See also here for more documentation on how to use R in VS code.
Then you can see the sections in the breadcrumb of the editor, if you have a comment in a new line having at least 4 hyphens (#My title ----). This is the same syntax as used in RStudio.