Understanding setup chunk behavior in Posit Cloud

Posit Cloud
R
RStudio IDE
Published

January 29, 2026

Abstract

Explains how code chunks labeled with setup are treated specially by the RStudio IDE when working with R Markdown or Quarto documents in Posit Cloud, and how to control their execution behavior.

Introduction

Users may encounter unexpected behavior when working with R Markdown or Quarto documents in Posit Cloud, particularly when executing code chunks labeled setup.

This behavior is driven by the RStudio IDE, which Posit Cloud uses, and may also be observed in other environments where the IDE is available.

Problem

When a code chunk is labeled setup, the RStudio IDE gives it special treatment. By default, setup chunks are executed first to ensure proper environment initialization, even if you click to run a different chunk interactively.

This can cause confusion when running chunks out of order, as code in the setup chunk may execute unexpectedly before other chunks.

Common symptoms include:

  • The setup chunk running before another selected chunk
  • Libraries or global options appearing to load unexpectedly
  • Chunk output appearing out of sequence during interactive execution

Solution

You have several options to control setup chunk behavior:

  1. Remove or rename the setup label
    If the chunk does not require special initialization behavior, remove the label or use a different name.

  2. Move the setup label to the intended first chunk
    Ensure the setup label is applied only to the chunk that should always execute first.

  3. Disable automatic setup chunk execution in the IDE
    In the RStudio IDE, go to:

    • Tools → Global Options → R Markdown
    • Uncheck “Execute setup chunk automatically in notebooks”

If you’re still having issues, you can reach out to Support by opening a ticket.