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?
By default, reprex strongly encourages execution in the session temp directory. But sometimes it is unavoidable to refer to a specific local file, so yes, there has to be a way to do this.
To request that all work be done in current working directory, set
outfile = NA
. (More generally, you can use theoutfile
argument to specify a base file name and path.)If I submit this reprex, with working directory set to my home directory:
I get this output:
Created on 2018-09-19 by the reprex package (v0.2.1)
Using
outfile = NA
oroutfile = "path/to/desired/file/base"
is the general pattern for asserting control over the location of all files generated byreprex()
.