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?
If you have one or more
factor
variable(s) in your data that you want to make reproducible withdput(head(mydata))
, consider addingdroplevels
to it, so that levels of factors that are not present in the minimized data set are not included in yourdput
output, in order to make the example minimal:I wonder if an http://old.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.
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 exampleis 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 itYou can do this using reprex.
As mt1022 noted, "... good package for producing minimal, reproducible example is "reprex" from tidyverse".
According to Tidyverse:
An example is given on tidyverse web site.
I think this is the simplest way to create a reproducible example.
Since R.2.14 (I guess) you can feed your data text representation directly to
read.table
:Here are some of my suggestions:
dput
, so others can help you more easilyinstall.package()
unless it is really necessary, people will understand if you just userequire
orlibrary
Try to be concise,
All these are part of a reproducible example.