Skip to main content
v2026.11,858 entries · CC-BY 4.0

Datasets in R Packages: List, Load and Add Your Own

List datasets in any installed R package, load them without overwriting your objects, and place data files correctly when building a package.

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

  1. List what is available. Run data() with no arguments. It lists the datasets in the packages on the search path.
  2. List one package’s datasets. Run data(package = "rpart") (substitute any installed package name). The result is an object of class packageIQR.
  3. Load a dataset by name. Run data(USArrests), or several at once with data(USArrests, "VADeaths"). To load names held in a character vector, use ds <- c("USArrests", "VADeaths") then data(list = ds).
  4. Restrict the search to one package. Use the package argument. With package set, loaded packages are searched first, then the default libraries. With package = NULL (the default), all packages on the search path are searched, then the data folder of the working directory. With package = character(0), only the working directory’s data folder is searched.
  5. 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 data subdirectory are made available either by lazy loading or with data(). The LazyData field in DESCRIPTION chooses; 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 .csv format is not standard CSV) and save() images (.RData/.rda). Tables may be gzip, bzip2 or xz compressed.
  • R CMD build adds a datalist file automatically for data directories over 1 MB.
  • The LazyDataCompression field accepts gzip (the default), bzip2, xz or none. R CMD check warns when data/Rdata.rdb is over 5 MB and the field is unset. The tools tools::checkRdaFiles and tools::resaveRdaFiles help 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.

Sources (official documentation, read 2026-11-12)

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Datasets in R Packages: List, Load and Add Your Own

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →