First, if you want to use RStudio, there doesn't seem to be anything wrong with that. However, if you already use VS Code for C/C++, Javascript, Python, Ruby, Markdown, text files, and everything else, it makes sense to also use it for R.
This should basically work for both Win10 and macOS. Your projects (stored in a
version control system, of course) should be portable between the two. The use
of renv
keeps the package
dependencies (and their versions) in sync for your project between multiple
machines and users.
This also recommends (at least so far) the addition of
plotly to enhance ggplot
as it creates interactive
plots that can be shared with people not running R. (Plotly also works well with
Javascript and Python.)
Some references:
-
R for Data Science is a very good online book, especially if you are starting from zero in R.
-
This post and this post discussing some best practices for organizing your data, your projects, and your code.
-
The original reference used to put together these instructions, although the
languageserversetup
package did not seem to actually be necessary, so it is not used below. -
A post specific to R and VS Code that was also a useful reference to putting together the instructions below. (Linked from the previous bullet, and probably ultimately more useful.)
-
This post discusses more about package management and why it is important. It goes briefly into even using Docker. However, it also discusses
packrat
which is out of date.packrat
has been essentially replaced byrenv
. See also here.
Now the basic steps:
Download the installer package for R (here v4.0.0 is used) for your operating system (here either Win10 or macOS, but there's no reason Linux won't work), and install it.
On Windows, you will also need to install
RTools because some R packages
need to be natively compiled when you install them, and Windows does not readily
offer a command-line compiler (e.g. clang
, gcc
) like macOS and Linux do.
When you install RTools in Windows, please don't use the default install
location. It is no longer the 1980s, and you shouldn't clutter up your drive
root. Just install it in Program Files\R
(where R itself is installed) or
something. Then add an environment variable:
RTOOLS40_HOME: C:\Program Files\R\rtools40
and add C:\Program Files\R\rtools40\usr\bin
to your path.
For an improved command-line R experience, install
Radian. This does require Python to run,
but it improves the command-line experience (much like ipython
does for
Python), so it is worth it.
See this gist and this gist for Windows for general development system setup including Python (and terminal customization, etc.).
With Python and pip
set up, run:
pip install --user radian
Then you should be able to run radian
from a terminal prompt. If you cannot,
make sure the pip
install location is on your system path. For example, if
you're on Windows and you used the dev setup instructions linked above that
include pyenv
, make sure that C:\Users\<username>\.pyenv\pyenv-win\bin
is in
your path.
Before setting up VS Code and its R-related extensions, you need to install the
languageserver
package in
R. This provides the hooks that VS Code needs to to add help features, linting
and code analysis, code execution hooks, etc. to the user interface.
From a terminal, run R (not Radian in this case if you installed Radian) to open the basic R shell. Then:
install.packages("languageserver")
and follow the instructions to select a CRAN mirror and all that. This should
install languageserver
and all of its dependencies.
Important: If you are on Win10, run R from an elevated command prompt. That is, run PowerShell as Administrator to do this step. This isn't clearly documented anywhere I could find, but without doing this as admin, the VS Code connection was never able to find the installed package.
Install VS Code in the usual way. Then install the vscode-R and vscode-r-lsp extensions.
In terms of extension configuration, in the R extension I have:
- enabled "Bracketed Paste" (recommended when using Radian)
- set the R path in macOS to
/usr/bin/R
- set the Windows path to
C:\Users\<username>\.pyenv\pyenv-win\shims\radian.bat
(unfortunately I couldn't get this to work using a more portable replacement like$HOME\.pyenv\...
) - enabled the (currently experimental) Session Watcher which adds some nice things like being able to get plots and help right in the VS Code environment.
everything else is set to the default. In the LSP (language server protocol) extension, everything is set to the default.
Then reload your VS Code Window, and it should be all set.
renv, gitignore, data folder, plotly, liveserver, etc...
One thing I've noticed is that if you use ggplot
and the plots show up within
VS Code, the plot will not update if you "source" an entire script file
(Cmd/Ctrl+Shift+S) but it will if you only run the code line that generates the
plot (Cmd/Ctrl+Enter).
However, if you source a file so the source(...)
command appears in the
integrated R terminal, then go to the terminal, edit the command to include the
parameter print.eval = TRUE
, and execute, the plot will update.