Query a Parquet file with DuckDB from R¶
Draft: not yet validated
The code on this page was run inside MESA RStudio Geospatial on 2026-11-13 (R 4.6.2, duckdb 2.0.0), by the AI assistant that drafted it, and produced the output shown, one step at a time. No person has yet followed the steps, so a screen may differ. Tell us what you find.
DuckDB runs SQL straight against a Parquet file, so R can summarise data far too large
to read into memory. MESA RStudio Geospatial has the R duckdb package installed.
You will: have queried a Parquet file with SQL from R, and have the result as a data frame.
Time: 10 minutes. You need: the practice files in your working folder, from Load a file from your Data Store in RStudio.
Steps¶
-
Load the package and open a connection:
library(duckdb) con <- dbConnect(duckdb())You should see:
Loading required package: DBI, and then, on first use, the questionduckdb: create /home/rstudio/.duckdb? (Yes/no/cancel). Typeyesand press Enter. That folder holds DuckDB's settings; it is on the app's own disk and is removed when the app stops. -
Make a Parquet file to practise on, from the CSV you already have:
dbExecute(con, "COPY (SELECT * FROM 'readings.csv') TO 'readings.parquet' (FORMAT parquet)")You should see: the number of rows written.
[1] 152 -
Ask what the file holds, without reading its rows:
dbGetQuery(con, "DESCRIBE SELECT * FROM 'readings.parquet'")You should see: the four columns with their types. Unlike
read.csv(), DuckDB recogniseddateas a date.column_name column_type null key default extra 1 gauge_id VARCHAR YES <NA> <NA> <NA> 2 date DATE YES <NA> <NA> <NA> 3 value DOUBLE YES <NA> <NA> <NA> 4 flag VARCHAR YES <NA> <NA> <NA> -
Summarise it with SQL:
dbGetQuery(con, " SELECT gauge_id, count(*) AS n_rows, max(value) AS largest FROM 'readings.parquet' GROUP BY gauge_id ORDER BY gauge_id ")You should see:
gauge_id n_rows largest 1 G01 27 24.90 2 G02 31 2.92 3 G03 32 32.50 4 G04 31 26.40 5 G05 31 180.00 -
Keep a result as a data frame:
flagged <- dbGetQuery(con, "SELECT * FROM 'readings.parquet' WHERE flag = 'S'") flaggedYou should see: two rows, in an ordinary data frame you can pass to any R function.
gauge_id date value flag 1 G03 2026-07-18 0 S 2 G05 2026-07-09 180 S -
Close the connection when you have finished:
dbDisconnect(con, shutdown = TRUE)You should see: no output.
Check it worked¶
The n_rows column adds up to 152, the number of rows dim(readings) reported when R
read the CSV. Two tools reading the same data should agree before you trust either.
If it didn't¶
IO Error: No files found that match the pattern "readings.parquet". DuckDB looks
in R's working folder. Either step 2 did not run, or R is somewhere else: run
getwd(), and setwd() to your mesa-practice folder.
Next¶
- The same task in Python: Query a Parquet file with DuckDB from a notebook.
- Map point data with sf in MESA RStudio Geospatial
- Reference: R Client in the DuckDB
documentation, which also covers
dplyrthroughdbplyr.
Not yet validated by a person in MESA RStudio Geospatial. Drafted with AI assistance on 2026-11-13; the code was run there that day (R 4.6.2, duckdb 2.0.0).
Machine-readable versions of this page: Markdown twin · raw source on GitHub · llms.txt · llms-full.txt (whole site). See For AI agents.