Why to learn R/Python? How to integrate them in vs code?
Python or R is an open-source programming language. Python is one of the most widely used general programming language and R is one of the most widely used programming language for research and data analysis.
Code sharing, customizing, reusability of codes, and literally endless possibility of expansion of data science ideas are some of the benefits of command-driven programs over menu-driven programs.
R seems to know anything that a data analyst needs to analyze. The code is highly customizable. One problem can be solved in different ways. Output formatting can be styled using tidyverse and kableExtra. However, I think R has attracted many data analyst for its amazing publication ready data-based figures usually done with ggplot2.
Installation of R and integration with Jupyter Notebook in Visual Studio Code
Install R Download R
Install Python Download Python
Install pip (package installer for python) using cmd (command prompt):
python -m pip install --upgrade pipFor python3, update pip if needed:
python3 -m pip install --upgrade pipInstall Jupyter using cmd:
python -m pip install jupyterYou can also install Jupyter Lab using cmd:
python -m pip install jupyterlabYou can launch Jupyter Notebook or Lab using cmd:
jupyter notebookorjupyter lab
Run jupyter notebook in browser
- In cmd: jupyter notebook
Run R with jupyter notebook in VS Code (Visual Studio code)
Install VS Code Download VS Code
Install R extension in VS code: R Extension for Visual Studio Code (REditorSupport)
Install radian in cmd for syntax highlighting:
pip3 install -U radianOpen R from Start Menu
Install packages in R (not RStudio) without compilation:
install.packages(c('languageserver', 'httpgd', 'repr', 'IRdisplay', 'evaluate', 'crayon', 'pbdZMQ', 'devtools', 'uuid', 'digest'))Install R kernel for jupyter in R (not RStudio):
devtools::install_github('IRkernel/IRkernel')Link R kernel to jupyter notebook in R (not RStudio):
IRkernel::installspec(user = FALSE)Open a file in VS code with .ipynb extension and select kernel (jupyer kernel > R kernel)
Now your jupyter notebook is ready for both python and R. You can also create and work with R file with .R extension in vs code. At the end, you can export the .ipynb (jupyter notebook) file as html by pressing ctrl+shift+P and search for ‘jupyter: export to html’. Open this html file in RStudio to publish.
In addition, you can read some tips on working with markdown in Markdown cheat sheet.
A few important VS Code extensions for R and Python: Code Runner (Jun Han), Document Viewer (Syncfusion), GitHub Copilot Chat (GitHub), LaTex Shortcuts (Axel Delaval), Latex Workshop (James Yu), Markdown All in One (Yu Zhang), Markdown for Humans: WYSISIWYG Editor (concret.io), Markdown PDF (Yzane), Markdown PDF Plus (Tom Lathan), Markdown Preview Github Styling (Matt Bierner), PDF Viewer (Mathematic Inc), Markdown (Official) (Quarkdown), Quarkdown Preview (holooooo), Quarto (Quarto), Quarto Helpers (EmilHvitfeldt), R (REditorSupport), R Debugger (RDebugger), R Syntax (ReditorSupport), R LSP Client (REditorSupport), R Tools (REditorSupport), C/C++ (Microsoft), Pylance (Microsoft), Jupyter (Microsoft), Python Docstring Generator (Nils Werner), Black Formatter (Josip Medved).
For Latex, you can install small MiKTeX Download MiKTeX or large TeXstudio Download TeXstudio.
VS Code may show further error regarding ‘R Not Attached’ or failing to initiate R terminals. To solve this you may need to change some settings. Insert the following settings in the settings.json. Maintain its position in the existing settings’ curly braces and rename R versions (if needed). VS Code environment can fire on all cylinders: Radian for the terminal, httpgd for the plots, a direct R.exe path for the help pages, and a quiet Language Server providing documentation without the “linting” clutter.
{
"editor.minimap.enabled": false,
"editor.unicodeHighlight.nonBasicASCII": false,
"editor.wordWrap": "on",
"workbench.editorAssociations": { ".pdf": "pdf.view", ".xlsx": "syncfusion-excel-viewer" },
"editor.unicodeHighlight.invisibleCharacters": false,
"security.workspace.trust.enabled": false,
"security.workspace.trust.untrustedFiles": "open",
"workbench.colorTheme": "Quiet Light",
"terminal.integrated.initialHint": false,
"security.workspace.trust.startupPrompt": "never",
"quarkdownPreview.quarkdownPath": "quarkdown",
"terminal.integrated.enableMultiLinePasteWarning": "never",
"r.bracketedPaste": true,
"r.rterm.option": [ ],
"r.alwaysUseActiveTerminal": true,
"r.plot.useHttpgd": true,
"r.lsp.diagnostics": false,
"breadcrumbs.enabled": false,
"r.rpath.windows": "C:\\Program Files\\R\\R-4.5.3\\bin\\x64\\R.exe",
"r.rterm.windows": "C:\\Users\\ruenr\\AppData\\Roaming\\Python\\Python314\\Scripts\\radian.exe",
"terminal.integrated.defaultProfile.windows": "R Terminal",
}
If VS Code does not find the existing R Package Library path, the installed packages in RStudio may not work in VS Code. To solve this, you will need to create some new environmental variables in system and user sections. Example:
```R_libs: C:Files.1
R_paths: C:Files.1
R_term: C:Files.1```
Note: To run Quarto Document in VS Code, install QuarkDown using this powershell command. Ignore error message, just wait. However, you may need to install npm.
QuarkDown Install Command:
irm https://raw.githubusercontent.com/quarkdown-labs/get-quarkdown/refs/heads/main/install.ps1 | iex
Download and install quarfrom from https://quarto.org/docs/download/
To automate the RPubs upload:
To automate publishing to RPubs in VS Code, you can create a simple wrapper script. Since VS Code lacks RStudio’s “Publish” button, we use the markdown package to handle the upload programmatically.
- The Automation Script Create a new R script (e.g., publish.R) and use this code. It will render your document and immediately upload it.
# Load necessary libraries
library(rmarkdown)
library(markdown)
# 1. Define your file and title
doc_path <- "your_file.Rmd"
doc_title <- "My Professional Report"
# 2. Render the document to HTML
# This creates the .html file needed for RPubs
render(doc_path, output_format = "html_document")
html_file <- gsub("\\.Rmd$", ".html", doc_path)
# 3. Upload to RPubs
# If it's a NEW post, leave id = NULL.
# If updating an EXISTING post, paste your RPubs ID string in 'id'
result <- rpubsUpload(title = doc_title, htmlFile = html_file, id = NULL)
# 4. Finalize
if (!is.null(result$continueUrl)) {
browseURL(result$continueUrl) # Opens browser to confirm
} else {
stop("Upload failed: ", result$error)
}