How to make a great R reproducible example
When discussing performance with colleagues, teaching, sending a bug report or searching for guidance on mailing lists and here on Stack Overflow, a reproducible example is often asked and always helpful.
What are your tips for creating an excellent example? How do you paste data structures from r in a text format? What other information should you include?
Are there other tricks in addition to using dput()
, dump()
or structure()
? When should you include library()
or require()
statements? Which reserved words should one avoid, in addition to c
, df
, data
, etc.?
How does one make a great r reproducible example?
Solution 1:
Basically, a minimal reproducible example (MRE) should enable others to exactly reproduce your issue on their machines.
A MRE consists of the following items:
- a minimal dataset, necessary to demonstrate the problem
- the minimal runnable code necessary to reproduce the error, which can be run on the given dataset
- all necessary information on the used packages, the R version, and the OS it is run on.
- in the case of random processes, a seed (set by
set.seed()
) for reproducibility
For examples of good MREs, see section "Examples" at the bottom of help files on the function you are using. Simply type e.g. help(mean)
, or short ?mean
into your R console.
Providing a minimal dataset
Usually, sharing huge data sets is not necessary and may rather discourage others from reading your question. Therefore, it is better to use built-in datasets or create a small "toy" example that resembles your original data, which is actually what is meant by minimal. If for some reason you really need to share your original data, you should use a method, such as dput()
, that allows others to get an exact copy of your data.
Built-in datasets
You can use one of the built-in datasets. A comprehensive list of built-in datasets can be seen with data()
. There is a short description of every data set, and more information can be obtained, e.g. with ?iris
, for the 'iris' data set that comes with R. Installed packages might contain additional datasets.
Creating example data sets
Preliminary note: Sometimes you may need special formats (i.e. classes), such as factors, dates, or time series. For these, make use of functions like: as.factor
, as.Date
, as.xts
, ... Example:
d <- as.Date("2020-12-30")
where
class(d)
# [1] "Date"
Vectors
x <- rnorm(10) ## random vector normal distributed
x <- runif(10) ## random vector uniformly distributed
x <- sample(1:100, 10) ## 10 random draws out of 1, 2, ..., 100
x <- sample(LETTERS, 10) ## 10 random draws out of built-in latin alphabet
Matrices
m <- matrix(1:12, 3, 4, dimnames=list(LETTERS[1:3], LETTERS[1:4]))
m
# A B C D
# A 1 4 7 10
# B 2 5 8 11
# C 3 6 9 12
Data frames
set.seed(42) ## for sake of reproducibility
n <- 6
dat <- data.frame(id=1:n,
date=seq.Date(as.Date("2020-12-26"), as.Date("2020-12-31"), "day"),
group=rep(LETTERS[1:2], n/2),
age=sample(18:30, n, replace=TRUE),
type=factor(paste("type", 1:n)),
x=rnorm(n))
dat
# id date group age type x
# 1 1 2020-12-26 A 27 type 1 0.0356312
# 2 2 2020-12-27 B 19 type 2 1.3149588
# 3 3 2020-12-28 A 20 type 3 0.9781675
# 4 4 2020-12-29 B 26 type 4 0.8817912
# 5 5 2020-12-30 A 26 type 5 0.4822047
# 6 6 2020-12-31 B 28 type 6 0.9657529
Note: Although it is widely used, better do not name your data frame df
, because df()
is an R function for the density (i.e. height of the curve at point x
) of the F distribution and you might get a clash with it.
Copying original data
If you have a specific reason, or data that would be too difficult to construct an example from, you could provide a small subset of your original data, best by using dput
.
Why use dput()
?
dput
throws all information needed to exactly reproduce your data on your console. You may simply copy the output and paste it into your question.
Calling dat
(from above) produces output that still lacks information about variable classes and other features if you share it in your question. Furthermore, the spaces in the type
column make it difficult to do anything with it. Even when we set out to use the data, we won't manage to get important features of your data right.
id date group age type x
1 1 2020-12-26 A 27 type 1 0.0356312
2 2 2020-12-27 B 19 type 2 1.3149588
3 3 2020-12-28 A 20 type 3 0.9781675
Subset your data
Tho share a subset, use head()
, subset()
or the indices iris[1:4, ]
. Then wrap it into dput()
to give others something that can be put in R immediately. Example
dput(iris[1:4, ]) # first four rows of the iris data set
Console output to share in your question:
structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6), Sepal.Width = c(3.5,
3, 3.2, 3.1), Petal.Length = c(1.4, 1.4, 1.3, 1.5), Petal.Width = c(0.2,
0.2, 0.2, 0.2), Species = structure(c(1L, 1L, 1L, 1L), .Label = c("setosa",
"versicolor", "virginica"), class = "factor")), row.names = c(NA,
4L), class = "data.frame")
When using dput
, you may also want to include only relevant columns, e.g. dput(mtcars[1:3, c(2, 5, 6)])
Note: If your data frame has a factor with many levels, the dput
output can be unwieldy because it will still list all the possible factor levels even if they aren't present in the subset of your data. To solve this issue, you can use the droplevels()
function. Notice below how species is a factor with only one level, e.g. dput(droplevels(iris[1:4, ]))
. One other caveat for dput
is that it will not work for keyed data.table
objects or for grouped tbl_df
(class grouped_df
) from the tidyverse
. In these cases you can convert back to a regular data frame before sharing, dput(as.data.frame(my_data))
.
Producing minimal code
Combined with the minimal data (see above), your code should exactly reproduce the problem on another machine by simply copying and pasting it.
This should be the easy part but often isn't. What you should not do:
- showing all kinds of data conversions; make sure the provided data is already in the correct format (unless that is the problem, of course)
- copy-paste a whole script that gives an error somewhere. Try to locate which lines exactly result in the error. More often than not, you'll find out what the problem is yourself.
What you should do:
- add which packages you use if you use any (using
library()
) - test run your code in a fresh R session to ensure the code is runnable. People should be able to copy-paste your data and your code in the console and get the same as you have.
- if you open connections or create files, add some code to close them or delete the files (using
unlink()
) - if you change options, make sure the code contains a statement to revert them back to the original ones. (eg
op <- par(mfrow=c(1,2)) ...some code... par(op)
)
Providing necessary information
In most cases, just the R version and the operating system will suffice. When conflicts arise with packages, giving the output of sessionInfo()
can really help. When talking about connections to other applications (be it through ODBC or anything else), one should also provide version numbers for those, and if possible, also the necessary information on the setup.
If you are running R in R Studio, using rstudioapi::versionInfo()
can help report your RStudio version.
If you have a problem with a specific package, you may want to provide the package version by giving the output of packageVersion("name of the package")
.
Seed
Using set.seed()
you may specify a seed1, i.e. the specific state, R's random number generator is fixed. This makes it possible for random functions, such as sample()
, rnorm()
, runif()
and lots of others, to always return the same result, Example:
set.seed(42)
rnorm(3)
# [1] 1.3709584 -0.5646982 0.3631284
set.seed(42)
rnorm(3)
# [1] 1.3709584 -0.5646982 0.3631284
1Note: The output of set.seed()
differs between R >3.6.0 and previous versions. Specify which R version you used for the random process, and don't be surprised if you get slightly different results when following old questions. To get the same result in such cases, you can use the RNGversion()
-function before set.seed()
(e.g.: RNGversion("3.5.2")
).
Solution 2:
(Here's my advice from How to write a reproducible example. I've tried to make it short but sweet).
How to write a reproducible example
You are most likely to get good help with your R problem if you provide a reproducible example. A reproducible example allows someone else to recreate your problem by just copying and pasting R code.
You need to include four things to make your example reproducible: required packages, data, code, and a description of your R environment.
-
Packages should be loaded at the top of the script, so it's easy to see which ones the example needs.
-
The easiest way to include data in an email or Stack Overflow question is to use
dput()
to generate the R code to recreate it. For example, to recreate themtcars
dataset in R, I'd perform the following steps:- Run
dput(mtcars)
in R - Copy the output
- In my reproducible script, type
mtcars <-
then paste.
- Run
-
Spend a little bit of time ensuring that your code is easy for others to read:
-
Make sure you've used spaces and your variable names are concise, but informative
-
Use comments to indicate where your problem lies
-
Do your best to remove everything that is not related to the problem.
The shorter your code is, the easier it is to understand.
-
-
Include the output of
sessionInfo()
in a comment in your code. This summarises your R environment and makes it easy to check if you're using an out-of-date package.
You can check you have actually made a reproducible example by starting up a fresh R session and pasting your script in.
Before putting all of your code in an email, consider putting it on Gist github. It will give your code nice syntax highlighting, and you don't have to worry about anything getting mangled by the email system.
Solution 3:
Personally, I prefer "one" liners. Something along the lines:
my.df <- data.frame(col1 = sample(c(1,2), 10, replace = TRUE),
col2 = as.factor(sample(10)), col3 = letters[1:10],
col4 = sample(c(TRUE, FALSE), 10, replace = TRUE))
my.list <- list(list1 = my.df, list2 = my.df[3], list3 = letters)
The data structure should mimic the idea of the writer's problem and not the exact verbatim structure. I really appreciate it when variables don't overwrite my own variables or god forbid, functions (like df
).
Alternatively, one could cut a few corners and point to a pre-existing data set, something like:
library(vegan)
data(varespec)
ord <- metaMDS(varespec)
Don't forget to mention any special packages you might be using.
If you're trying to demonstrate something on larger objects, you can try
my.df2 <- data.frame(a = sample(10e6), b = sample(letters, 10e6, replace = TRUE))
If you're working with spatial data via the raster
package, you can generate some random data. A lot of examples can be found in the package vignette, but here's a small nugget.
library(raster)
r1 <- r2 <- r3 <- raster(nrow=10, ncol=10)
values(r1) <- runif(ncell(r1))
values(r2) <- runif(ncell(r2))
values(r3) <- runif(ncell(r3))
s <- stack(r1, r2, r3)
If you need some spatial object as implemented in sp
, you can get some datasets via external files (like ESRI shapefile) in "spatial" packages (see the Spatial view in Task Views).
library(rgdal)
ogrDrivers()
dsn <- system.file("vectors", package = "rgdal")[1]
ogrListLayers(dsn)
ogrInfo(dsn=dsn, layer="cities")
cities <- readOGR(dsn=dsn, layer="cities")
Solution 4:
Inspired by this very post, I now use a handy function,
reproduce(<mydata>)
when I need to post to Stack Overflow.
Quick instructions
If myData
is the name of your object to reproduce, run the following in R:
install.packages("devtools")
library(devtools)
source_url("https://raw.github.com/rsaporta/pubR/gitbranch/reproduce.R")
reproduce(myData)
Details:
This function is an intelligent wrapper to dput
and does the following:
- Automatically samples a large data set (based on size and class. Sample size can be adjusted)
- Creates a
dput
output - Allows you to specify which columns to export
- Appends to the front of it
objName <- ...
, so that it can be easily copy+pasted, but... - If working on a Mac, the output is automagically copied to the clipboard, so that you can simply run it and then paste it to your question.
The source is available here:
- GitHub - pubR/reproduce.R
Example:
# sample data
DF <- data.frame(id=rep(LETTERS, each=4)[1:100], replicate(100, sample(1001, 100)), Class=sample(c("Yes", "No"), 100, TRUE))
DF is about 100 x 102. I want to sample 10 rows and a few specific columns
reproduce(DF, cols=c("id", "X1", "X73", "Class")) # I could also specify the column number.
Gives the following output:
This is what the sample looks like:
id X1 X73 Class
1 A 266 960 Yes
2 A 373 315 No Notice the selection split
3 A 573 208 No (which can be turned off)
4 A 907 850 Yes
5 B 202 46 Yes
6 B 895 969 Yes <~~~ 70 % of selection is from the top rows
7 B 940 928 No
98 Y 371 171 Yes
99 Y 733 364 Yes <~~~ 30 % of selection is from the bottom rows.
100 Y 546 641 No
==X==============================================================X==
Copy+Paste this part. (If on a Mac, it is already copied!)
==X==============================================================X==
DF <- structure(list(id = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 25L, 25L, 25L), .Label = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y"), class = "factor"), X1 = c(266L, 373L, 573L, 907L, 202L, 895L, 940L, 371L, 733L, 546L), X73 = c(960L, 315L, 208L, 850L, 46L, 969L, 928L, 171L, 364L, 641L), Class = structure(c(2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L), .Label = c("No", "Yes"), class = "factor")), .Names = c("id", "X1", "X73", "Class"), class = "data.frame", row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 98L, 99L, 100L))
==X==============================================================X==
Notice also that the entirety of the output is in a nice single, long line, not a tall paragraph of chopped up lines. This makes it easier to read on Stack Overflow questions posts and also easier to copy+paste.
Update Oct 2013:
You can now specify how many lines of text output will take up (i.e., what you will paste into Stack Overflow). Use the lines.out=n
argument for this. Example:
reproduce(DF, cols=c(1:3, 17, 23), lines.out=7)
yields:
==X==============================================================X==
Copy+Paste this part. (If on a Mac, it is already copied!)
==X==============================================================X==
DF <- structure(list(id = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 25L,25L, 25L), .Label
= c("A", "B", "C", "D", "E", "F", "G", "H","I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U","V", "W", "X", "Y"), class = "factor"),
X1 = c(809L, 81L, 862L,747L, 224L, 721L, 310L, 53L, 853L, 642L),
X2 = c(926L, 409L,825L, 702L, 803L, 63L, 319L, 941L, 598L, 830L),
X16 = c(447L,164L, 8L, 775L, 471L, 196L, 30L, 420L, 47L, 327L),
X22 = c(335L,164L, 503L, 407L, 662L, 139L, 111L, 721L, 340L, 178L)), .Names = c("id","X1",
"X2", "X16", "X22"), class = "data.frame", row.names = c(1L,2L, 3L, 4L, 5L, 6L, 7L, 98L, 99L, 100L))
==X==============================================================X==
Solution 5:
Here is a good guide.
The most important point is: Make a small piece of code that we can run to see what the problem is. A useful function for this is dput()
, but if you have very large data, then you might want to make a small sample dataset or only use the first 10 lines or so.
EDIT:
Also, make sure that you identified where the problem is yourself. The example should not be an entire R script with "On line 200 there is an error". If you use the debugging tools in R (I love browser()
) and Google, then you should be able to really identify where the problem is and reproduce a trivial example in which the same thing goes wrong.