This tool organizes an R script into its sections. R scripts grow organically — a library call here, a data load there, cleaning scattered through analysis — until nobody, including the author a month later, can say where anything lives. Section headers (comment lines like # — Libraries —) are the lightweight structure that fixes this, and this tool reads them: it splits the script at each header and reports every section with its code-line count, then shows the sections in order. The result is a map of the script, useful for refactoring, reviewing, and teaching.
How to use it
- Paste your R script.
- Make sure sections start with headers like # — Name —.
- Click Organize sections.
- Read the section table: which sections are bloated?
- Refactor the longest sections first.
Worked example
A student’s analysis script has 300 lines with no headers. Adding three headers — Libraries, Data, Analysis — and running the organizer shows Libraries with 8 lines, Data with 40, and Analysis with 250 lines, most of it data cleaning mixed with modelling. The map makes the refactoring obvious: split Analysis into Cleaning and Modelling, and the script becomes navigable. Without the map, the student would have kept scrolling; with it, the structure problem is visible in one table.
Honest limits
The tool only recognizes dash-style headers (# — Title —); scripts using other conventions (plain comments, RStudio section markers with four # or – variants beyond this pattern) may not split as expected, so standardize headers first. It does not execute code, check syntax, or reorder anything — reordering sections can break dependencies, since later code often relies on earlier objects. It also counts non-empty lines only loosely: comments inside sections count as lines in the block view, so the table and the block may differ slightly in what “lines” means. Treat the output as a map, not a refactorer.
Who it is for
This tool is for R users tidying coursework or analysis scripts, teachers showing students what script structure looks like, and reviewers getting oriented in someone else’s code. It is a first step toward readable code, not a substitute for functions, comments, and documentation.
Going further
A good section order follows the analysis pipeline: setup (libraries, options), data (load, validate), cleaning (fix, transform), analysis (models, tests), and output (tables, plots, exports) — in that order, with no step reaching backward for something defined later. If your organizer map shows analysis before cleaning, or plots interleaved with modelling, the script is telling you its story out of order, and a reader will struggle to follow it. Enforce the order once, and future edits get easier: every new line has an obvious home, and the next person to open the file finds a story instead of a pile. Once sections exist, consider turning repeated blocks into functions: a cleaning sequence used three times becomes one function called three times, and the script shrinks while becoming easier to test. Functions also make the section map more honest — a section titled Analysis that contains only function calls is a readable outline of the analysis, while one containing 200 lines of inline code is still a pile with a nice heading. Structure is a ladder: headers first, functions next, and a package or documented project when the work grows beyond a single script. Add a README comment block at the top of every script, above the first section: purpose, author, date, inputs, outputs, and how to run it. The section map shows structure, but the header answers the questions a stranger asks first — what is this, can I run it, what will it produce? Scripts without headers get abandoned because the activation energy to understand them exceeds the value of running them; a five-line header reverses that calculation. It is the smallest documentation with the largest return.
Related tools
Versión en español · Jupyter Notebook Code Stripper · All free online tools.
Embed this tool on your website — free
Copy the code below to add this working tool to your own page. A small credit link stays with it — that’s all we ask.
