Running R Code in VS Code

MKT 566 · Decision Making Using Marketing Analytics · Fall 2026

In the VS Code setup guide you installed VS Code and an AI assistant. This guide adds the last ingredient: R, the language we use for all the analysis in this course.

Three pieces have to talk to each other, and this guide installs them in order:

The whole thing takes about 15 minutes, most of it waiting for downloads. If you get stuck, see If something goes wrong at the bottom.

The shortcut: ask your AI assistant to do it

You installed an AI assistant in the previous guide, and setup chores like this one are exactly what it is good at. Instead of doing Steps 1 to 4 by hand, you can ask it to do them for you:

  1. Open your assistant’s chat: the spark icon (Claude Code), the Codex icon, or the chat panel (Copilot). If you use Copilot, switch the dropdown at the bottom of the chat from Ask to Agent, so it is allowed to run commands.

  2. Paste this request:

    Set up my computer so I can run R scripts in VS Code. Check whether R is installed and install it if it is not. Install the VS Code R extension by REditorSupport. Then install these R packages: languageserver, httpgd, ggplot2, dplyr, tidyr, forcats, scales, data.table, ggthemes, patchwork, treemapify, RColorBrewer, sf, and rnaturalearth. If a package fails to install, do not install anything from source; skip it and tell me at the end. Next, enable the httpgd plot viewer in the VS Code settings. Then apply the known workaround for REditorSupport/vscode-R issue #1696 (R 4.5+ no longer runs the extension’s .First.sys startup hook, so plots open in a separate window instead of a VS Code tab): append to my ~/.Rprofile a .First function that looks up globalenv()$.First.sys, and if it is a function, removes .First from globalenv and calls the hook. Finally, tell me how to check that everything works.

  3. The assistant will suggest commands and ask your permission before running each one. Read what it plans to do and click Allow. Expect a few of these; the whole thing still takes 10 to 15 minutes because the downloads are the slow part.

  4. When it says it is done, jump to Step 5 and try running a script. If something does not work, tell the assistant exactly what you saw, or fall back to the manual steps below.

If you would rather see what is actually being installed (a good instinct), do Steps 1 to 4 yourself:

Step 1: Install R

R is free and comes from a website called CRAN (the official R repository).

On a Mac

  1. First, check which chip your Mac has: click the Apple menu (top-left corner) → About This Mac. Look at the Chip line: it says either Apple M1/M2/M3/M4 (Apple silicon) or Intel.
  2. Go to https://cran.r-project.org and click Download R for macOS.
  3. Download the installer that matches your chip:
    • Apple silicon (M1 and later): the file ending in arm64.pkg.
    • Intel: the file ending in x86_64.pkg.
  4. Double-click the downloaded .pkg file and click through the installer: Continue, Agree, Install. You may need to type your Mac password.

On Windows

  1. Go to https://cran.r-project.org and click Download R for Windows.
  2. Click base, then the big Download R-4.x.x for Windows link at the top.
  3. Double-click the downloaded .exe file and click through the installer, keeping all the default choices: Next, Next, …, Finish.

The exact version number changes over time; anything starting with 4 is fine.

Step 2: Install the R extension in VS Code

  1. Open VS Code. If it was already open, quit it completely and reopen it so it notices that R is now installed.
  2. Press Cmd+Shift+X (Mac) or Ctrl+Shift+X (Windows) to open the Extensions panel.
  3. In the search box, type R.
  4. Click the result named R, published by REditorSupport (it is usually the top result), then click the blue Install button.

Step 3: Install the course packages

Packages are add-ons to R, like apps on a phone. One command installs everything we need for the first weeks.

  1. In VS Code, press Cmd+Shift+P (Mac) or Ctrl+Shift+P (Windows), type R: Create R Terminal, and press Enter. A panel opens at the bottom of the window ending in a > symbol. That is the R console: the place where R code runs.
  2. Copy the line below, paste it into the console, and press Enter:
install.packages(c("languageserver", "httpgd", "ggplot2", "dplyr", "tidyr",
                   "forcats", "scales", "data.table", "ggthemes", "patchwork",
                   "treemapify", "RColorBrewer", "sf", "rnaturalearth"))
  1. A lot of text will scroll by for a few minutes. That is normal. Two questions might come up:
    • If R asks you to select a CRAN mirror, pick the first option (0-Cloud).
    • If R asks “Do you want to install from sources the package which needs compilation?”, type no and press Enter.
  2. You are done when the scrolling stops and the > prompt comes back.

For the curious: languageserver gives you autocomplete and help inside VS Code, httpgd makes charts show up in a VS Code tab, and the rest are the graphing and data packages our scripts use.

Step 4: Make charts appear inside VS Code

Two small things, so charts open in a VS Code tab instead of a separate floating window.

First, a checkbox:

  1. Press Cmd+, (Mac) or Ctrl+, (Windows) to open Settings.
  2. In the search box, type httpgd.
  3. Check the box for R > Plot: Use Httpgd.

Second, a one-time fix. Recent versions of R (4.5 and newer, which is what you just installed) changed how R starts up, and the R extension has a known bug with it (issue #1696): without the fix, charts still open in a separate window. Copy the block below, paste it into the R console from Step 3 (the panel ending in >), and press Enter:

cat('
# Show charts in a VS Code tab (fix for R 4.5+, vscode-R extension issue #1696)
.First <- function() {
  hook <- globalenv()$.First.sys
  if (is.function(hook)) {
    suppressWarnings(rm(".First", envir = globalenv()))
    hook()
  }
}
', file = "~/.Rprofile", append = TRUE)

You do not need to understand the code in the box; it simply reconnects R to VS Code each time R starts. It writes the fix into a file called .Rprofile, which R reads every time it starts, so you only do this once. Then close the R console (hover over R Interactive in the panel’s right-side list and click the trash-can icon): the fix takes effect the next time a console opens, which happens automatically when you run code in Step 5.

Step 5: Run your first real script

  1. Unzip the week 1 code folder you downloaded from the course site.
  2. In VS Code, choose File → Open Folder… and open that folder.
  3. In the left sidebar, click a script (a file ending in .R) to open it.
  4. Click anywhere on the first line of code and press Cmd+Enter (Mac) or Ctrl+Enter (Windows). Two things happen: an R console opens at the bottom (the first time takes a few seconds), and that line runs in it. The cursor then jumps to the next line.
  5. Keep pressing Cmd+Enter / Ctrl+Enter to walk through the script one line at a time, watching what each line prints. To run several lines at once, select them first, then press the same shortcut.
  6. When a line makes a chart, the chart appears in a new tab on the right.

That is the whole workflow for this course: open a script, run it line by line, and look at what each line does. If a line confuses you, select it and ask your AI assistant what it does. That is exactly what it is for.

If something goes wrong

  • Cmd+Enter / Ctrl+Enter does nothing. Make sure the file name ends in .R, then press Cmd+Shift+P / Ctrl+Shift+P, type “Reload Window”, and press Enter.
  • VS Code cannot find R, or the R console never opens. Make sure Step 1 actually finished, then quit VS Code completely and reopen it. Installing R after VS Code was already open is the most common cause.
  • A popup says the R language server is not installed and offers to install it. Click Install: it is the languageserver package from Step 3.
  • Running a line prints garbage like [200~ or an error ending in ~. Open Settings (Cmd+, / Ctrl+,), search for bracketed paste, and make sure R > Bracketed Paste is unchecked.
  • Charts open in a separate floating window (called “Quartz” on a Mac, “R Graphics” on Windows) instead of a VS Code tab. One of the two parts of Step 4 is missing: check the httpgd box in Settings and paste the one-time fix into the R console. Then close the R console (trash-can icon) and run a chart line again. The first chart after a restart can take a few seconds.
  • An error says there is no package called 'ggplot2' (or any other package name). The packages did not finish installing: redo Step 3 and let it run to the end.
  • Anything else. Paste the full error message into your AI assistant and ask what to do. This works surprisingly often, and building that habit is half the point of the course.
  • Still stuck? Bring your laptop to office hours: Davide (Tue 2 to 4 pm, HOH 332) or Raghav, our TA (Thu 2:30 to 4:30 pm, HOH 311). We will get you running in a few minutes.