How not to be lost with VSCode when coming from RStudio?

VSCode is popular these days in the #rstats community. I know how to find everything I need in RStudio and I have my small habits, then how to find my way with VSCode? Here, I present the answers to my first exploration in using VSCode as a R user.

You will see that there are a few missing functionnalities and shortcuts when you start using VSCode. I know I can add some more plugins and my own keybindings, but here I want to show what exists when you start, without too much configuration. Indeed, when there is too much personal configuration, the environment is hardly reproducible for other users to test it. But there are already many things possible with VSCode, so let’s explore this!

Also, I know that VSCode is way more interesting if you use other development tools like CSS, HTML, JavaScript, Docker, ssh connection: everything needed for R in production. But this is not the topic of this blog post.

You first need to install VSCode and appropriate plugins as detailed at the end of this blog post.

What bugged me with VSCode coming as an RStudio user?

It is when you do not have things anymore that you know they were important in your (development) life…

Can I open a RStudio project and be in the same conditions?

Yes. You need to open your directory directly.: File > Open folder.
The working directory is the project root.

Can I have multiple VSCode session in parallel?

Yes. Open a new window, then you can open a new project: File > New window.

I don’t understand how it opens files in tabs or not…

  • Double Click on file name: Open the file
  • SHIFT + Double Click: Open in a new tab
  • CTRL + Double Click: Open in a new tab with a parallel view

Where is my code auto-indentation ?

I Don’t know! But you can use {styler} Addin

  • Select your code
  • CTRL + SHIFT + P
  • Find “Rstudio Addin”
  • Execute “Style selection” from {styler}

What are all these numbers next to my file name?

{lintr} results. When you open a file, {lintr} parses your code to find problems and practices that do not follow classical R code writing style.

Problematic lines or words are underlined in the code. Hover with the mouse to get the information.

How to start and use a Rmarkdown file?

You may know, I am an huge fan of the ‘Rmd First’ method to develop my analyses and my packages. This is why I developed {fusen}: a package to develop a package from a single Rmarkdown file. Hence, being able to work with Rmarkdown file is VSCode is one of my main concerns.

I did not find how to add the default template as in RStudio. However, you can start one of the two templates included in {rmarkdown}:

  • rmarkdown::draft(file = "bonjour", template = "github_document", package = "rmarkdown", edit = FALSE)
  • rmarkdown::draft(file = "bonjour", template = "html_vignette", package = "rmarkdown", edit = FALSE)

Then open the file and write your code.

  • Knit with : CTRL + SHIFT + K
  • There is no shortcut by default to add a new chunk, but you can create keybindings (See below)

Note that you can open a live preview the file by clicking on the split icon with a magnifier on the top right of your file.

Where is my R Console ?

It is not opened by default when opening your R project, because VSCode is not specific to R users. But you are one click away from opening it.

Click on “R: (not attached)” in the footer bar.

Can I use VSCode to develop a R package ?

Yes.
Tips: Create a “dev/dev_history.R” file in your package to store all functions that you use to use during development, because all buttons of the RStudio are not there. You can get an example of this file in our {attachment} package there: https://github.com/ThinkR-open/attachment/blob/master/dev/dev_history.R

Also, using {fusen} may help you in the process of building a package without having to think too much about all steps you use to do, like document, add dependencies, check, …

Is there autocompletion of R functions and help?

Yes. There is autocompletion in the files with function help box appearing. There is autocompletion in the R Console. There is also autocompletion of roxygen tags when writing function documentation.

Is there autocompletion for file path?

Yes, if you install the Path Intellisense plugin : https://github.com/ChristianKohler/PathIntellisense

  • You will need to start your relative path with "./" for autocomplete to start.

How do I know my file is not saved yet?

There is a dot on the right of the tab of the file

How do I commit and push my modifications?

  • Go to the git menu

  • Click on the + along the name of the file you want to add to the next commit. Note letters similar to what you find on RStudio about the state of your files (M, U, A, …).

  • You can write your commit message

  • Press “CTRL + Enter” to commit

  • Click the 3 dots to push to the server

  • List of operations on git repository, like push or pull

In the git view menu, when you open one of the files listed, you will see the difference with the previous commited version.

Can I use the bash Terminal?

Yes. In the bottom panel, click the + to choose what kind of Terminal you want to open Choose Terminal in VSCode

Which keyboard shortcuts are the same?

  • CTRL + Enter: Run highlighted code
  • CTRL + F: Search a word in the current file
  • CTRL + SHIFT + F: Search a word in the entire repository
  • CTRL + SHIFT + K: Knit a RMarkdown file

Can I open a directory with multiple git repositories ?

Yes. In this way, you will be able to see the git state of all your repositories in this directory.

Tips: I have a directory with all my GitHub projects and I can know their state with a unique view.

Advantages: This is good because you can be sure that you do not hav remaining commit locally, never sent to the server

Disadvantages: Do not expect to use this kind of view to work with R on a specific project, because the working directory will not be the one you expect as coming from RStudio.

How to comment a bunch of lines with # ?

  • CTRL + SHIFT + :

How to explore the code of a function?

VSCode does not automatically open the code of a function as text file like with F2 and when using browser() or debugonce()

  • I can use View(function) manually

Where are my RStudio Addins?

  • Use CTRL + SHIFT + P to open the commands explorer

    • Type “RStudio Addin”

    • Find the Addin you want to use

Any other tips or interesting differences?

  • Use CTRL + SHIFT + P to open the commands explorer

  • In Ubuntu, the R Console is a Terminal. This means:

    • CTRL + C: Stop the current action (not copy)

    • SHIFT + Insert: Paste the text you copied

  • {rstudioapi} functions work as is in VSCode

What VSCode Plugins to install as a starting kit for development with R?

Where to find VSCode?

https://code.visualstudio.com/

How do I install a plugin?

Click on the four squares icon on the left of VSCode and start searching the available plugins

Required: Install vscode-r plugin

Git tools

  • GitLens - Git supercharged

    • Allows git history and git blame along the files opened

    • Git history view

Path Autocomplete

What keybindings should I add?

Find all existing keyboard shortcuts

CTRL + K, CTRL + S

How to add new keyboard shortcuts?

When in the keyboard shortcuts list, click on the top right button

Some interesting shortcuts to add to “keybindings.json” to start

As said before, I don’t propose too many modifications of the original features so that the environment is similar between users.

// Place your key bindings in this file to override the defaultsauto[]
[
    // assignment
    {
        "key": "alt+-",
        "command": "type",
        "when": "editorLangId == r || editorLangId == rmd && editorTextFocus",
        "args": {
            "text": " <- "
        }
    },
    // Pipe
    {
        "key": "ctrl+shift+m",
        "command": "type",
        "when": "editorLangId == r || editorLangId == rmd && editorTextFocus",
        "args": {
            "text": " |> "
        }
    },
    // Insert a chunk
    {
        "key": "ctrl+alt+i",
        "command": "type",
        "when": "editorLangId == r || editorLangId == rmd && editorTextFocus",
        "args": {
            "text": "```{r}\n\n```"
        }
    },
    // Roxygen skeleton
    {
        "key": "ctrl+alt+shift+r",
        "command": "type",
        "when": "editorLangId == r || editorLangId == rmd && editorTextFocus",
        "args": {
            "text": "#' Title\n#'\n#'Description\n#'\n#'@param $$\n#'\n#'@export\n#'\n#'@example\n#'"
        }
    }
]

To go further

Stay tuned with the development of “vscode-R”: https://github.com/REditorSupport/vscode-R

Note that the Wiki presents the different possibilities offered by the plugin: https://github.com/REditorSupport/vscode-R/wiki

The big question: will I quit RStudio for VSCode?

As a regular R user, I do not see why I would quit RStudio where I find everything I need to create my projects, write my RMarkdown file, do my analysis and share it with git on GitLab or GitHub. Navigation menu for section titles of my Rmarkown or R script files is really comfortable. I would also say that the Visual Editor is a game changer in RStudio, in particular to paste screenshots as I did in this blog post.

As a R package developer, development and debugging tools included in RStudio make life really easy. Like button to test a single test file, open file when debugging, run checks in a separate session, verify package version installed, …

As a co-developer for R in production, I think that plugins available in VSCode will change my life like when I work with Docker, distant server connection, developing in other web and programming languages. I find the folder / file exploration list with file toggle much more comfortable than the “Files” Pane in RStudio. I find the exploration of git branches, git history, … very useful. And Live Sharing would probably allow more interesting pair programming sessions.

As a R instructor, I will stay in RStudio. There are already many things to learn when you start a new language. Whatever the level I teach, beginners, package development, Shiny for production, … I think that as RStudio is centered on R, there is less noise around, and we can focus on R and not to much on understanding the IDE. We can think that Live Sharing could probably be something missing with RStudio for remote teaching, but I personally prefer to explain what to do than writing it myself in the code of attendees, so that they better understand what they do.

What about you? Did you try?
What are your prefered plugins and keybindings?



Citation:

For attribution, please cite this work as:
Rochette Sébastien. (2021, Oct. 09). "How not to be lost with VSCode when coming from RStudio?". Retrieved from https://statnmap.com/2021-10-09-how-not-to-be-lost-with-vscode-when-coming-from-rstudio/.


BibTex citation:
@misc{Roche2021Howno,
    author = {Rochette Sébastien},
    title = {How not to be lost with VSCode when coming from RStudio?},
    url = {https://statnmap.com/2021-10-09-how-not-to-be-lost-with-vscode-when-coming-from-rstudio/},
    year = {2021}
  }