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 SO, 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?



A minimal reproducible example consists of the following items:



Looking at the examples in the help files of the used functions is often helpful. In general, all the code given there fulfills the requirements of a minimal reproducible example: data is provided, minimal code is provided, and everything is runnable.



For most cases, this can be easily done by just providing a vector/data frame with some values. Or you can use one of the built-in datasets, which are provided with most packages.
A comprehensive list of built-in datasets can be seen with library(help = "datasets"). There is a short description to every dataset and more information can be obtained for example with ?mtcars where 'mtcars' is one of the datasets in the list. Other packages might contain additional datasets.



Making a vector is easy. Sometimes it is necessary to add some randomness to it, and there are a whole number of functions to make that. sample() can randomize a vector, or give a random vector with only a few values. letters is a useful vector containing the alphabet. This can be used for making factors.



A few examples :



For matrices, one can use matrix(), eg :



Making data frames can be done using data.frame(). One should pay attention to name the entries in the data frame, and to not make it overly complicated.



An example :



For some questions, specific formats can be needed. For these, one can use any of the provided as.someType functions : as.factor, as.Date, as.xts, ... These in combination with the vector and/or data frame tricks.



If you have some data that would be too difficult to construct using these tips, then you can always make a subset of your original data, using eg head(), subset() or the indices. Then use eg. dput() to give us something that can be put in R immediately :



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 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:



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 dplyr. In these cases you can convert back to a regular data frame before sharing, dput(as.data.frame(my_data)).



Worst case scenario, you can give a text representation that can be read in using the text parameter of read.table :



This should be the easy part but often isn't. What you should not do, is:



What you should do, is:



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 be helpful to report your RStudio version.



If you have a problem with a specific package you may want to provide the version of the package by giving the output of packageVersion("name of the package").



(Here's my advice from How to write a reproducible example . I've tried to make it short but sweet)



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.



There are four things you need to include 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 the mtcars dataset in R,
I'd perform the following steps:



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.



Personally, I prefer "one" liners. Something along the lines:



The data structure should mimic the idea of 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:



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



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.



If you're in need of 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).



Inspired by this very post, I now use a handy function
reproduce(<mydata>) when I need to post to StackOverflow.



If myData is the name of your object to reproduce, run the following in R:



This function is an intelligent wrapper to dput and does the following:



DF is about 100 x 102. I want to sample 10 rows, and a few specific columns



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 SO questions posts and also easier to copy+paste.



You can now specify how many lines of text output will take up (ie, what you will paste into StackOverflow). Use the lines.out=n argument for this. Example:



reproduce(DF, cols=c(1:3, 17, 23), lines.out=7) yields:



Here is a good guide:



http://www.r-bloggers.com/three-tips-for-posting-good-questions-to-r-help-and-stack-overflow/



But the most important is: Just make sure that you make a small piece of code that we can run to see what the problem is. A usefull function for this is dput(), but if you have very large data 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 you should be able to really identify where the problem is and reproduce a trivial example in which the same thing goes wrong.



The R-help mailing list has a posting guide which covers both asking and answering questions, including an example of generating data:



Examples: Sometimes it helps to
provide a small example that someone
can actually run. For example:



If I have a matrix x as follows:



how can I turn it into a dataframe
with 8 rows, and three columns named
'row', 'col', and 'value', which have
the dimension names as the values of 'row' and 'col', like this:



...
(To which the answer might be:



)



The word small is especially important. You should be aiming for a minimal reproducible example, which means that the data and the code should be as simple as possible to explain the problem.



EDIT: Pretty code is easier to read than ugly code. Use a style guide.



Since R.2.14 (I guess) you can feed your data text representation directly to read.table:



Sometimes the problem really isn't reproducible with a smaller piece of data, no matter how hard you try, and doesn't happen with synthetic data (although it's useful to show how you produced synthetic data sets that did not reproduce the problem, because it rules out some hypotheses).



If you can't do either of these then you probably need to hire a consultant to solve your problem ...



edit: Two useful SO questions for anonymization/scrambling:



The answers so far are obviously great for the reproducibility part. This is merely to clarify that a reproducible example cannot and should not be the sole component of a question. Don't forget to explain what you want it to look like and the contours of your problem, not just how you have attempted to get there so far. Code is not enough; you need words also.



Here's a reproducible example of what to avoid doing (drawn from a real example, names changed to protect the innocent):



The following is sample data and part of function I have trouble with.



How can I achieve this ?



To quickly create a dput of your data you can just copy (a piece of) the data to your clipboard and run the following in R:



for data in Excel:



for data in a txt file:



You can change the sep in the latter if necessary.
This will only work if your data is in the clipboard of course.



I have a very easy and efficient way to make a R example that has not been mentioned above.
You can define your structure firstly.For example,



when you execute 'fix' command,you will get this pop-up box



Then you can input your data manually.This is efficient for smaller examples rather than big ones.



Your main objective in crafting your questions should be to make it as easy as possible for readers to understand and reproduce your problem on their systems. To do so:



This does take some work but seems like a fair trade-off since you are asking others to do work for you.



The best option by far is to rely on built-in datasets. This makes it very easy for others to work on your problem. Type data() at the R prompt to see what data is available to you. Some classic examples:



See this SO QA for how to find data sets suitable for your problem.



If you are able to rephrase your problem to use the built-in datasets you are much more likely to get good answers (and upvotes).



If your problem is very specific to a type of data that is not represented in the existing data sets, then provide the R code that generates the smallest possible dataset that your problem manifests itself on. For example



Now someone trying to answer my question can copy/paste those two lines and start working on the problem immediately.



As a last resort, you can use dput to transform a data object to R code (e.g. dput(myData)). I say as a "last resort" because the output of dput is often fairly unwieldy, annoying to copy-paste, and obscures the rest of your question.



Someone once said:



A picture of expected output is worth 1000 words



-- a very wise person



If you can add something like "I expected to get this result":



to your question, people are much more likely to quickly understand what you are trying to do. If your expected result is large and unwieldy, then you probably haven't thought enough about how to simplify your problem (see next).



The main thing to do is to simplify your problem as much as possible before you ask your question. Re-framing the problem to work with the built-in datasets will help a lot in this regard. You will also often find that just by going through the process of simplification you will answer your own problem.



Here are some examples of good questions:



In both cases, the user's problems are almost certainly not with the simple examples they provide. Rather they abstracted the nature of their problem and applied it to a simple data set to ask their question.



This answer focuses on what I think is the best practice: use built-in data sets and provide what you expect as a result in a minimal form. The most prominent answers focus on other aspects. I don't expect this answer to rising to any prominence; this is here solely so that I can link to it in comments to newbie questions.



Reproducible code is key to get help. However, there are many users that might be skeptical of pasting even a chunk of their data. For instance, they could be working with sensitive data or on an original data collected to use in a research paper. For any reason, I thought it would be nice to have a handy function for "deforming" my data before pasting it publicly. The anonymize function from the package SciencesPo is very silly, but for me it works nicely with dput function.



Then I anonymize it:



One may also want to sample few variables instead of the whole data before apply anonymization and dput command.



Often you need some data for an example, however, you don't want to post your exact data. To use some existing data.frame in established library, use data command to import it.



e.g.,



and then do the problem



If you have large dataset which cannot be easily put to the script using dput(),
post your data to pastebin and load them using read.table:



Inspired by @Henrik.



I am developing the wakefield package to address this need to quickly share reproducible data, sometimes dput works fine for smaller data sets but many of the problems we deal with are much larger, sharing such a large data set via dput is impractical.



About:



wakefield allows the user to share minimal code to reproduce data. The user sets n (number of rows) and specifies any number of preset variable functions (there are currently 70) that mimic real if data (things like gender, age, income etc.)



Installation:



Currently (2015-06-11), wakefield is a GitHub package but will go to CRAN eventually after unit tests are written. To install quickly, use:



Example:



Here is an example:



This produces:



If you have one or more factor variable(s) in your data that you want to make reproducible with dput(head(mydata)), consider adding droplevels to it, so that levels of factors that are not present in the minimized data set are not included in your dput output, in order to make the example minimal:



I wonder if an http://www.r-fiddle.org/ link could be a very neat way of sharing a problem. It receives a unique ID like and one could even think about embedding it in SO.



http://www.r-fiddle.org/#/help



Please do not paste your console outputs like this :



We can not copy-paste it directly.



To make questions and answers properly reproducible, try to remove + & > before posting it and put # for outputs and comments like this:



One more thing, if you have used any function from certain package, mention that library.



Apart of all above answers which I found very interesting, it could sometimes be very easy as it is discussed here :- HOW TO MAKE A MINIMAL REPRODUCIBLE EXAMPLE TO GET HELP WITH R



There are many ways to make a random vector Create a 100 number vector with random values in R rounded to 2 decimals or random matrix in R



Note that sometimes it is very difficult to share a given data because of various reasons such as dimension etc. However, all above answers are great and very important to think and use when one wants to make a reproducible data example. But note that in order to make a data as representative as the original (in case the OP cannot share the original data), it is good to add some information with the data example as (if we call the data mydf1)



Moreover, one should know the type, length and attributes of a data which can be Data structures



Here are some of my suggestions:



Try to be concise,



All these are part of a reproducible example.



You can do this using reprex.



As mt1022 noted, "... good package for producing minimal, reproducible example is "reprex" from tidyverse".



According to Tidyverse:



The goal of "reprex" is to package your problematic code in such a way that other people can run it and feel your pain.



An example is given on tidyverse web site.



I think this is the simplest way to create a reproducible example.



It's a good idea to use functions from the testthat package to show what you expect to occur. Thus, other people can alter your code until it runs without error. This eases the burden of those who would like to help you, because it means they don't have to decode your textual description. For example



is clearer than "I think x would come out to be 1.23 for y equal to or exceeding 10, and 3.21 otherwise, but I got neither result". Even in this silly example, I think the code is clearer than the words. Using testthat lets your helper focus on the code, which saves time, and it provides a way for them to know they have solved your problem, before they post it




Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).


Would you like to answer one of these unanswered questions instead?

Popular posts from this blog

大跃进

马相伯