Written and maintained by CASRAI Editorial Board
Last updated
Who this is for: researchers and analysts who need to see which datasets ship with R and its packages, load one into a session, and, if they write packages, ship their own. After reading you should be able to list datasets from any installed package, load them without overwriting your own objects, and know where a package author should put data files.
Short answer: run data() to list datasets in the packages on your search path, data(package = "rpart") to list one package’s datasets, and data(mtcars) to load one by name. Read its documentation with help(mtcars). Behaviour below is taken from the R documentation (R 4.6.0 pages, read 2026-11-12).
What datasets in R packages are used for in research
R ships a base set of example datasets in the datasets package, and many contributed packages bundle their own. The titles in the datasets package index show the intended use: small, documented tables for teaching and for testing code, for example mtcars (“Motor Trend Car Road Tests”), iris (“Edgar Anderson’s Iris Data”), airquality (“New York Air Quality Measurements”), ToothGrowth (“The Effect of Vitamin C on Tooth Growth in Guinea Pigs”) and PlantGrowth (“Results from an Experiment on Plant Growth”).
Because they are bundled and documented, they make good reproducible examples: anyone with the package installed can run your code. They are not a substitute for the data in your own study, and each has its own provenance, which you should read before citing results. Python users looking for the equivalent should see scikit-learn datasets.
Install or access (official source only)
The data() function belongs to the utils package and the datasets package is part of R itself, so neither needs a separate install. Datasets from contributed packages become available once the package is installed and, for lazy-loaded data, attached with library(). Official documentation:
- The
data()help page in the R manual (utils package). - The “Data in packages” section of Writing R Extensions on CRAN, for package authors.
- The datasets package index.
Step-by-step: list, load and document a dataset
- List what is available. Run
data()with no arguments. It lists the datasets in the packages on the search path. - List one package’s datasets. Run
data(package = "rpart")(substitute any installed package name). The result is an object of classpackageIQR. - Load a dataset by name. Run
data(USArrests), or several at once withdata(USArrests, "VADeaths"). To load names held in a character vector, useds <- c("USArrests", "VADeaths")thendata(list = ds). - Restrict the search to one package. Use the
packageargument. Withpackageset, loaded packages are searched first, then the default libraries. Withpackage = NULL(the default), all packages on the search path are searched, then the data folder of the working directory. Withpackage = character(0), only the working directory’s data folder is searched. - Read the documentation. Run
help(USArrests)to see the description, variables and source.
The full signature is data(..., list = character(), package = NULL, lib.loc = NULL, verbose = getOption("verbose"), envir = .GlobalEnv, overwrite = TRUE).
Which file types does data() read?
For data files in a data folder, the data() help page lists: .R/.r files are sourced; .RData/.rda files are loaded; .txt/.tab files are read as tables with a header; and .csv files are read with ; as the separator.
Output and how to read it
data() returns a packageIQR object, displayed as a list grouped by package. Some entries look like beaver1 (beavers). That means the object beaver1 is created when you load the file named beavers, so you call data(beavers), not data(beaver1).
Troubleshooting
A loaded dataset replaced my own object
By default overwrite = TRUE and envir = .GlobalEnv, so data() writes into your global environment and can silently replace an existing object of the same name. Rename your own object before loading, set overwrite = FALSE, or inside a function load into the function’s own environment with envir = environment().
Dataset not found
Check the name against the data() listing, remember the alias case above (beaver1 versus beavers), and name the package with package = if it is not on the search path.
A .csv file loads as one column
data() reads .csv files with ; as the separator, so a comma-separated file will not split into columns. For comma-separated files use a reading function with the right separator on a file path instead.
For package authors: where data files go
From Writing R Extensions, “Data in packages” (read 2026-11-12):
- Files in the package’s
datasubdirectory are made available either by lazy loading or withdata(). TheLazyDatafield inDESCRIPTIONchooses; the default is not to lazy-load. - Other files belong in
inst/extdata. - Accepted data file types are R code (
.R/.r), tables (.tab/.txt/.csv, where the.csvformat is not standard CSV) andsave()images (.RData/.rda). Tables may be gzip, bzip2 or xz compressed. R CMD buildadds adatalistfile automatically for data directories over 1 MB.- The
LazyDataCompressionfield accepts gzip (the default), bzip2, xz or none.R CMD checkwarns whendata/Rdata.rdbis over 5 MB and the field is unset. The toolstools::checkRdaFilesandtools::resaveRdaFileshelp with this. Lazy loading is not supported above 2 GB of serialized data.
Reproducibility and citation
A bundled dataset is still data: record the package name, the R version and, from help(), the stated source. Treat the package as software you cite (see the FORCE11 software citation principles), and report it under your reproducible research practices. For data you publish yourself, the FAIR data principles and a datasheet for datasets describe what documentation to provide.
Other ways to find datasets and read their documentation
| Method | What it gives you | Source |
|---|---|---|
data() |
Datasets in packages on the search path | R data() help page |
data(package = "name") |
One package’s datasets as a packageIQR object |
R data() help page |
help(dataset) |
Description, variables and source of one dataset | R data() help page examples |
| datasets package index (web) | Titles of the core datasets without starting R | R manual, datasets 4.6.0 |
FAQ
How do I see all datasets available in R?
Run data(). It lists datasets from the packages on the search path; add package = "name" to list a single package.
How do I load a dataset from a specific package?
Use data(name, package = "pkg"). When package is set, loaded packages are searched first, then the default libraries.
Why did data() change my variable?
Its defaults are overwrite = TRUE and envir = .GlobalEnv, so an object with the same name is replaced. Use overwrite = FALSE or load into another environment.
Do package datasets load automatically?
Only if the package sets LazyData in DESCRIPTION; the default is not to lazy-load, so authors may need users to call data().
Where should I put raw files in my own package?
Processed data in data, other files in inst/extdata.







