FAQ: What's a reproducible example (`reprex`) and how do I do one?

faq

#1

Why reprex?

Getting unstuck is hard. Your first step here is usually to create a reprex, or reproducible example. The goal of a reprex is to package your code, and information about your problem so that others can run it and feel your pain. Then, hopefully, folks can more easily provide a solution.

What's in a Reproducible Example?

Parts of a reproducible example:

  1. background information - Describe what you are trying to do. What have you already done?
  2. complete set up - include any library() calls and data to reproduce your issue.
    data for a reprex: Here's a discussion on setting up data for a reprex
  3. make it run - include the minimal code required to reproduce your error on the data provided.
    People should be able to copy and paste your code chunk and get the same error.
    How do I format my text so it has nice code chunks?
  4. minimal - strip away everything that is not directly related to your problem. This usually involves creating a much smaller and simpler set of code and data compared to that which created your issue.

Use the reprex-package to Build Your Reproducible Example

When creating a reprex by hand, it’s easy to accidentally miss something that means your code can’t be run on someone else’s computer. Avoid this problem by using the reprex-package.
The reprex package will save effort for you and others who want to help.


Beginners - Step by Step Help

Help asking R-related questions (not specific to the reprex-package)

Background on the reprex-package


Frequently Asked Questions
First shiny app - error message - missing argument
Need Help : group_by and summarize_each
Generate a data frame from many xml files
Gmailr formatting
Advice for Improving Community Response to Modeling and Machine Learning Topics
How to use tidyverse to group columns
How to Ignore one of the factors in a Column
Document term matrix in XGBoost classifier
Collecting tweets based on csv file with point coordinates
Copy array & keep data formats
Error: no applicable method for 'mutate_' applied to an object of class "character"
Problem with non-ASCII characters in DocumentTermMatrix
Shiny Tutorial Lesson 5: How to add counties name on the map
Building Dendrogram using NormalizeMets
Importing data from table using Shiny
Loading ECG binary .dat files
Create Volcano Plot
How to assign a letter grade to the marks while doing the results analysis for examination results of a class?
Getting monthly data into daily/weekly data
Forecasting - Combination of methods
An important bar in the graph
Ggplot troubleshoot: Error: Aesthetics must be either length 1 or the same as the data (24): x, y, fill
Problem in 'matplot' function
Help with function convert.inp in RMark
Reading RDS file format from HDFS using SparklyR package using Microsoft R Open
Prediction from knn model
Cannot plot plot a tobit model--getting error message(what do I do)?
Cannot see the complete plot for all values of variable
How to get percentage and then create a plot from a given output
Need help to convert data to xml
Facing problem while writing the R code
How to assign categorical variables to specific rows of data
Read string from array
Autolayer() deleted from ggplot2 and forecast package - alternatives?
Switch beetweeen tabItems
Connecting R with Database | Large Query Result
Logistic linear regression failed - wants to know other model which suits this
Convert pair(1L,2L) to pair(1,2)
R-Studio - Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels
Linking CRSP and Compustat in R
Slowing of ggplot2 and plotly
Rstudio to hadoop cluster connection to read hive database tables
Kernlab library
Error in gzfile(path.expand(tarfile), "rb") : cannot open the connection In addition: Warning message: In gzfile(path.expand(tarfile), "rb") : cannot open compressed file 'NA', probable reason 'Permission denied'
Transposing/Reshaping Data Frame
Mixed GLMM Model - Issues with using weights argument
Error: cannot allocate vector of size 196.1 Mb In addition: Warning message:
In pearson correlation if the value comes in complex fromat (1.346045e-17) then what it means
Transforming daily data in wide format to long
Error Preventing Knitting
Can't add images to R Markdown file
Modeling bimodal curves with smsn.mix()
Installing readxl, tidyveerse and devtools
Find the elements that sums closest to zero value
FAQ: Homework Policy
How to average/mean variables in R based on the level of another variable (and save this as a new variable)?
Roxygen2 does not work with R 3.5.0
Pink dots appear in files and unable to save files intermittently MacOS High Sierra
Add a Zoom on my plot
Rcpp can't find sugar functions
How to customise colours to specific things
Plotly plot showing in viewer pane instead of R notebook
Creating a new column with breaks
Bookdown: Blank output files (basically the output in HTML is blank an no RMD data is shown)
Chunk of Python code continues to run after clicking on red square
Merging survey data with geographic data
Demeaning / Mean-Centering of certain values only
Circle in ggplot2
Visualizing interaction terms
Reprex vs. reprex-package discussions
Sqldf questions in R studio
Problem with "jitter"
Dplyr summarise categorial variable that appears multiple times
Problem with summarize in r
A Durbin-Watson test question
Ggplot doesn't take an Empty dataset what to do?
Get Google trends data - problem with output
Ggplot2: How to specify x labels in a datetime data and two segment lines
Debugger stops at incorrect line
R Studio Package Installation Not Recognized (SOLVED--THANKS)
R HELP! PSO Package - "Error during wrapup: incorrect number of dimensions
Problem with "bnlearn" package & string elements table by reading with excel (or readxl...)
Matrices x Vector multiplication using R script
Inconsistent digits beyond decimal when combined with scientific notation kableExtra table R markdown
[ solved ] RStudio IDE Viewer is not rendering the styled HTML output from ztable package
Subsetting dataset to include numerical values in a column
Ggplot not showing all dates on x asis even when forced
Compare each record value from two different data sets
I want to create a matrix with rows and columns
FactomineR - MFA error
Ifelse with mutiple actions
How to create a reprex?
What is wrong with this tidy eval function?
Monte Carlo simulation
R version 3.5.0 Beta
Plot large data in R
Relabel and merge the fasta files
Area weighted seasonal cummulative precipitation extraction based on crop yeild area file
Dplyr filter from another dataframe
Understanding window sizes when using kNN
Error: Long vectors not supported yet
Turning non-military time to military time
How do I make an ad hoc smoothing parameter in the Adehabitat HR package?
Wavelet Coherence
String match in R
Histograms with loops
R does not display output
EasyMARK and Maker.CH problems
Figure Caption in R markdown
Reshape dont work for me
Count Total time from occurences of event
Rstudio crashs on macOS with the X11 cairo type window
Seeking help for using filter and mutate command to break down a current variable into separate variables
Set.seed() with simulations
Non linear equation how add confidence intervals
Degree of vertex
Aligning tables in the center?
Rounding and piping
Importing Data with API
FAQ: How do I edit my Topic or Post?
Tags$img problem with srcset
Programming Help
Kindly help us facing lot of issues
BLAS/LAPACK routine 'DLASCL' gave error code -4
Importing xml in R
Need Help in Running the codes
Track down error in R
Problem with Clustered SE's
Finding all Universal Combinations in an Occurrence Matrix
(not so) stranger things
renderPlotly creates lines between points when combined with ggmap
Error in UseMethod(TermDocumentMatrix, x), Mail Base
Deploying Error, No problem a few days ago
A cry for help with boot()
Recursive calcs on timeseries
Empty spaces instead of wormplots (wp() function in GAMLSS module)
Single and double Quotes at SQLQuery connected to Presto
Writing Greek in Rstudio
GGMAP : Unable to create points on the map
How to apply corrr::correlate by group?
Data frame error - "replacement has 4 rows, data has...."
gvisMotionChart From googleVis is not working any suggestion?
Error in ANOVA contrast commands
"closure" is not subsettable
Refresh query on every new entry in database and instant response on my plot
With monthly measurements points
Plotting over an existing plot with ggplot2
Reproducing results from Cameroon & Trivadi book
StatBin requires a continuous x variable the x variable is discrete
A r code execution error about fuction "text"
NAs introduced by coercion
How to make function which can read an argument without any gap or comma?
Determining peak of normal distributed data
Stranger things
How to deal with character(0) in R
How to use crawler with Rcrawler for automatic download in r?
RStudio breaks on FreeBSD: "TypeError: undefined is not an object (evaluating 'window.desktopHooks.notifyRCrashed')"
Going long to wide with multiple variables based on
How to check Outlier after imputing the missing value by Hmisc Package
Principal Components Analysis (PCA)
Problem with changing the language in Rstudio
Using a Variable Where Quotes Are Needed in Function Argument
How to separate title from desc (scraping data IMDB-Coming Soon Movie)
GWAS analysis using FarmCPU
Flextable output in RMarkDown
Effect size in forest plot using metafor
Error in eval(predvars, data, env) : object 'avg' not found
Subsetting Data
Problem with titles and subplot
Issue with full_join(): new rows that do not belong to any data frame
Selecting suitable supervised learning algorithm
Helping with the code, it doesn't work without any error
Create Volcano Plot
Wordcloud and plotting sentiment score error
How can I plot data in a CSV file after uploading it using R shiny? I have used the code below and seems not working for me
Could not find svmRadial function
Problem with applying function to a dataframe
Flashing/disappearing imageOutput
Define a new column for genre of the movie
I want to use ggplot2 and reshape to draw multiple densities on the same plot, tried this is my code and error,
Xml2 package installation issues - pkg-config PATH help
Selecting data in a range
R : dynamic plot value and x axis value for line chart
Tidyverse ggplot() help: How to segment each column in the geom_bar()
Data Frame: join a groupby with a new column as a new index
#2

A post was split to a new topic: Best Practices: how to prepare your own data for use in a reprex if you can’t, or don’t know how to reproduce a problem with a built-in dataset?


#3

Thanks, Miles, this is great!

A few more resources to add:


Not reprex-specific, but helpful for asking R-related questions


#6

17 posts were split to a new topic: Reprex vs. reprex-package discussions


Box plots aren't displaying correctly
#7

For sharing simple data.frames (those containing only basic types, no dates, no factors, and no row names) I suggest using wrapr::draw_frame() to build sharable examples.

For example suppose our example was the following data.

d <- head(ggplot2::diamonds) 

wrapr::draw_frame can share this data in a very legible form:

library("wrapr")
cat(draw_frame(d))

This outputs the following (older versions of wrapr do not add the "::" qualifier).

wrapr::build_frame(
   "carat", "cut"      , "color", "clarity", "depth", "table", "price", "x" , "y" , "z"  |
   0.23   , "Ideal"    , "E"    , "SI2"    , 61.5   , 55     , 326L   , 3.95, 3.98, 2.43 |
   0.21   , "Premium"  , "E"    , "SI1"    , 59.8   , 61     , 326L   , 3.89, 3.84, 2.31 |
   0.23   , "Good"     , "E"    , "VS1"    , 56.9   , 65     , 327L   , 4.05, 4.07, 2.31 |
   0.29   , "Premium"  , "I"    , "VS2"    , 62.4   , 58     , 334L   , 4.2 , 4.23, 2.63 |
   0.31   , "Good"     , "J"    , "SI2"    , 63.3   , 58     , 335L   , 4.34, 4.35, 2.75 |
   0.24   , "Very Good", "J"    , "VVS2"   , 62.8   , 57     , 336L   , 3.94, 3.96, 2.48 )

The point is, with the wrapr package loaded the above output is actually executable code that produces the same data.frame. One can then copy and paste the above code to start a fresh example from this data (and not need to include steps that took one to this point).

(Was asked to post this to this thread here.)


Error: cannot allocate vector of size 12.4 Gb