![]() In RStudio, here() refers to the base directory of the current project, but VS Code does not have projects. I like to use the here package in R which avoids the need for hard-coding directory paths and makes it easier to share code with others. As with RStudio, you can send a single line of code from the editor to R ( cmd-return), or run the whole code ( cmd-shift-S). To save clicking back and forth, there are keyboard shortcuts to shift the focus from the code editor to the R console and back ( ctrl-` and cmd-1 on a Mac). With R opened in this way, you can send code to it from a VS Code editor, or interact with it directly by typing in the R console pane. Instead you need to click the down arrow next to the plus button used to open a new terminal, and select R Terminal. This will launch R, but it won’t be able to communicate with VS Code properly. However, as I learned the hard way, don’t launch R by typing R from a terminal prompt in VS Code. VS Code has an integrated terminal where the R session runs. Then you need the R extension for VS Code, which also requires the languageserver package to be installed in R. ![]() The first step is to obviously install VS Code. All of the layout is very customisable and you can also have a horizontal pane at the bottom of the screen if you wish. Then at the far right is the terminal pane which is showing an R console and can also be used as a command-line terminal. I often make charts as output so it’s nice to be able to preview these directly in VS Code. Next from the left is a code pane, and to the right of that is a chart that has been output as a PNG file by the code. You can however double-click a variable to open it in a viewer as you can in RStudio. Clicking the R button at the far left switches the file explorer to an R workspace explorer, which I have to say is not as useful as the similar feature in RStudio in that the information it shows about current variables in the workspace is quite limited. At the left is a file explorer of the current folder, and below that is an outline of the currently open code file which gives easy access to its sections. The screenshot above shows some of the basic features of VS Code. Let’s start with where I ended up after half a day of setup. Much of what follows I learned from Ren Kun’s excellent blog posts about VS Code and R. The setup process was a lot more involved than RStudio which works pretty much out of the box, but I’m quite happy with the end result. I’ve only used VS Code for a short time so am not an expert, but here is a quick summary of how I got it set up. VS Code is a popular development environment that now has very good support for R via some extensions. So at the suggestion of a few friends I decided to try VS Code instead. For example the pane layout is not very flexible and I find that the process of sending code from the editor to the R console slows down sometimes for some unknown reason. I’ve used RStudio for a long time and am mostly happy with but it does have a few things that annoy me. Instead of the Path Intellisense extension, I’ve found the Path Autocomplete extension works better. ![]() Recommended extensions and configuration.Using this URI with the vscode.openFolder command did the trick.Update June 2023: See also the following resources provided by the creators of the vscode-R extension: Instead of using the () method, I tried out the () method for the Windows path and it resulted in: file:///c:/data/vscode/test-project-folder. ![]() When checking this for a macOS path like /data/vscode/test-project-folder, the scheme is set to file. Looking to the documentation, this should be file. The scheme for this URI is C, which is something VS Code its file system provider does not understand. Whenever I parsed the above Windows path, I received the following path: C:/data/vscode/test-project-folder. To open a project folder, VS Code requires the following URL format: vscode://file// - example: vscode://file/c:/myProject/. This documentation led me to the solution of opening Windows-based folder paths. While searching through the VS Code issues and documentation, I found the following information on opening VS Code with URLs. When I opened the folder from the command, I spotted the following error in the debug console: No file system provider found for resource. The above code works fine on macOS and Linux but gives issues on Windows paths.įirst, I thought it had to do with the \ backslashes, but replacing these with / forward slashes did not change the behavior. The folder path needs to be parsed to a URI to use the command. executeCommand ( `vscode.openFolder`, folderUri ) Const folderPath = `C:\\data\\vscode\\test-project-folder` const folderPathParsed = folderPath.
0 Comments
Leave a Reply. |