Scripts
Python and R on the canvas — read your tables as data frames, compute anything, and emit new tables and tiles.
A script is a Python or R program that lives on the canvas: it reads the document's tables and tiles, computes whatever the language can compute, and emits new tables and tiles as canvas objects. Scripts never run automatically — you click Run.
A script's language is chosen when you create it and doesn't change afterwards. Both languages get the same editor, the same lineage arrows, the same refresh behaviour, and the same runtime API — spelled in each language's own idiom.
Requirements
Scripts use your own interpreter — nothing is bundled:
| Language | Needs | Found as |
|---|---|---|
| Python | pandas and duckdb | python, py, python3 |
| Python, for statistical tests | also scipy and statsmodels | as above |
| R | duckdb and jsonlite (with DBI) | Rscript |
A language with no usable interpreter keeps its entry points hidden — if you have Python but not R, you'll simply never see R. Point Dreamsheets at a specific interpreter (a virtualenv, a non-PATH R) in Tools → Script Settings…. Each interpreter stays warm between runs, so repeated runs don't pay the import tax.
Writing a script
Right-click empty canvas and pick Add Python script here… or Add R script here… — only the languages you have installed are offered. Insert → Python Script… / R Script… does the same thing. Either opens the script editor: a syntax-highlighted source editor, a built-in API cheat-sheet, an optional Suggest with AI helper that drafts the script from a description, and a Run button.
The script environment, in both languages:
| Python | R | What it is |
|---|---|---|
sheet.MyTable | sheet$MyTable | The table named MyTable |
sheet["My Table"] | sheet[["My Table"]] | The same, for names with spaces |
.to_df() | (not needed) | Python's handle is lazy; R hands you the data frame directly |
sheet.sql("SELECT …") | sheet$sql("SELECT …") | Run DuckDB SQL across the document's tables |
tiles["My Tile"] | tiles[["My Tile"]] | A tile's value |
emit_table(df, name="…") | emit_table(df, name = "…") | Create/update a table on the canvas |
emit_tile(value, name="…") | emit_tile(value, name = "…") | Create/update a tile |
emit_chart(df, kind="bar", x="…", y="…", name="…") | emit_chart(df, kind = "bar", x = "…", y = "…", name = "…") | Create/update a native chart |
pd, duckdb pre-imported | base R attached |
orders = sheet.Orders.to_df()
by_month = (orders
.assign(month=pd.to_datetime(orders["Date"]).dt.to_period("M").astype(str))
.groupby("month", as_index=False)["Amount"].sum())
emit_table(by_month, name="MonthlyRevenue")
emit_tile(orders["Amount"].sum(), name="TotalRevenue")orders <- sheet$Orders
orders$month <- format(as.Date(orders$Date), "%Y-%m")
by_month <- aggregate(Amount ~ month, data = orders, FUN = sum)
emit_table(by_month, name = "MonthlyRevenue")
emit_tile(sum(orders$Amount), name = "TotalRevenue")If a script ends with a bare result value and emits nothing explicitly, Dreamsheets emits it for you — a data frame becomes a table, anything else becomes a tile.
For large tables, prefer sheet.sql(...) / sheet$sql(...) — the query is pushed into DuckDB over the table's storage instead of materializing everything into memory first. This matters more in R, where sheet$MyTable loads the whole table.
Python extras
Python's table handle is lazy, so it also offers .batches(n=100_000) to iterate the table in Arrow record batches (out-of-core for huge tables) and .schema for the table's schema as a DataFrame. Python scripts also get IDE-style autocomplete in the editor; R's editor is plain for now.
R notes
R scripts auto-print visible top-level results, like the R console — a bare head(df) shows its output. Base R is attached; anything else (dplyr, tidyr, a modelling package) must be installed in the R that Dreamsheets is using.
Refresh, lineage, and iteration
- Outputs are keyed by the script's own emit names: re-running the script updates the tables and tiles it created in place rather than creating duplicates. The match is on the script's recorded outputs, never on a document-wide name — so a script's first run that emits
name="Sales"into a document that already has aSalestable createsSales (2)rather than overwriting someone else's table. Script-produced cards get Refresh script / Edit script… in their menus, and a refresh always re-runs in the script's own language. - Column types follow the data frame, via DuckDB: integer dtypes land as integer columns, floats as numbers,
datetime.datevalues (e.g.df["d"].dt.date) as dates,datetime64as timestamps, strings as text. Convert before emitting if you want a date rather than a timestamp. - Dreamsheets records which tables and tiles a run read, and draws lineage arrows from the sources into the script card and out to its outputs.
- Errors show only your script's lines — library and runtime internals are stripped, so the message always points at something you wrote.
print()/cat()output is captured and shown.
Plots
A script that plots needs no extra code. In Python, the figures matplotlib has open when the script ends become pictures on the canvas (Figure 1, Figure 2, … in the order they were drawn); in R, anything drawn with base graphics or ggplot2 does. A re-run replaces each picture where it sits rather than adding more, and a picture the script stops drawing is removed. At most 20 figures per run.
Seaborn, `df.plot` and ggplot2 figures become native charts when the chart can draw exactly what the figure shows, in any script: interactive, themed, editable without code, and exported to Excel as real charts. The plotted data lands as a Figure N data table, and the chart takes the figure's place. A figure that maps only partly stays a picture: a second plot on the same axes, an extra mark (axhline, annotate), a twin axis, or an option the chart doesn't have.
What the plotting library computes, it computes: a seaborn bar or line plot's estimate and error bars come from seaborn's own aggregator (any errorbar=, any estimator, median included), and a ggplot2 stat_summary, geom_bar count or geom_smooth band comes from ggplot_build. So the chart shows the same numbers as the figure. (Seaborn bootstraps its confidence intervals; Dreamsheets seeds that bootstrap so a re-run draws the same chart.)
| Comes in native | Stays a picture | |
|---|---|---|
| seaborn | barplot, countplot, lineplot (with their error bars), scatterplot (hue, size, style), histplot (default bins), violinplot(inner=None), regplot/lmplot with ci=None, relplot/catplot faceted by col= | box, strip and swarm plots, heatmap, pairplot/jointplot, kdeplot, a regression's confidence band, row= facets, horizontal bars, custom bins= |
| pandas | df.plot line, bar, area, scatter and pie | df.plot.hist (its fixed 10 bins), barh, kde, box, subplots |
| ggplot2 | geom_point, geom_line, geom_col/geom_bar (stacked or dodged), geom_area, geom_violin, stat_summary (point range or bar), bars + geom_errorbar, geom_smooth (line and band), points + geom_smooth(method = "lm", se = FALSE) as a trend line, facet_wrap(~one), log and sqrt scales | geom_boxplot, geom_histogram, geom_density, loess fits, coord_flip/coord_polar, facet_grid, gradient colour scales, alpha/linetype/group aesthetics, annotate(), several plots on one page |
To make a chart directly, call emit_chart. data is a data frame, or the name of a table (one the run emitted, or one already in the document). kind is bar, line, point (or scatter), area, pie, histogram, density or violin. y may be a list for several series. The other channels are color, size, shape and facet, each a column name, plus y_low/y_high: two columns holding an interval's lower and upper ends (a confidence interval, mean ± SE), drawn around each mark (caps=True adds end caps). Also available: aggregate (sum, avg, count, min, max, none), stack (stack, dodge, none), normalize="percent", trendline (linear, poly…, with trend_order), x_scale/y_scale="log", title, x_label and y_label.
emit_chart(by_month, kind="line", x="month", y="Amount", name="Revenue trend")A re-run updates the chart in place. The script decides its data, kind, columns and aggregate; anything you restyle on the card (title, colors, axes, notes) stays.
Trust and safety
Scripts run with your interpreter's full capabilities (files, network — whatever your Python or R can do). That's the point — and the risk. So:
- Scripts never auto-run, ever. Opening a document executes nothing.
- The first time you run a script from a document you loaded, Dreamsheets asks whether you trust this document's scripts; the answer resets when you open a different document.
- A script imported from a notebook is reviewed on its own, even in a document you trust: its first Run shows the code and notes on what it does, and confirming trusts that one script (see Notebooks and scripts).
- Scripts read the open document's tables and tile values only, and write back only through
emit_table/emit_tile/emit_chart— each run's output is a normal undoable change. - A runaway script is stopped after two minutes rather than wedging the app. A script that legitimately needs longer (downloading and filtering large files) can be given up to 30 minutes: right-click its card → Time limit…. While a script runs, its card shows Cancel.
Scripts vs. the alternatives
- Reshaping data with joins and grouping → a query table is simpler and refreshable in place.
- A derived value per row → a calculated column.
- Iterative algorithms, calling libraries (stats, ML, requests), custom cleanup, simulation → a script. Reach for R when the analysis you want is a CRAN package; reach for Python when it's a pandas/scikit one.