I often use reprex::reprex
to create reproducible examples of R
code to get help from others to get rid of errors in my code. Usually, I create minimal examples using datasets like iris
or mtcars
and it works well.
But I always fail to use reprex
any time I need to use my own data since the problem is so specific and I can't rely on datasets from datasets
library.
In that case, I get the following error:
# loading needed libraries
library(ggplot2)
library(cowplot)
library(devtools)
# reading the datafile
data <- utils::read.csv(file = "data.csv")
#> Warning in file(file, "rt"): cannot open file 'data.csv': No such file or
#> directory
#> Error in file(file, "rt"): cannot open the connection
Created on 2018-02-19 by the reprex package (v0.2.0).
There is a great discussion from pre-reprex
era elsewhere (How to make a great R reproducible example?). The author recommends using something like dput
-
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 inR
immediately
But also mentions-
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.
So, if I want to work will my full dataset, this is not a good option.
In summary:
Anyone knows how to create a reprex
which is standalone even if it relies on using a local file containing all of your data?