creating reproducible example using reprex package

2019-08-20 03:57发布

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 in R 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?

1条回答
孤傲高冷的网名
2楼-- · 2019-08-20 04:44

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 the outfile argument to specify a base file name and path.)

If I submit this reprex, with working directory set to my home directory:

reprex({
  getwd()
  writeLines(c("V1,V2","a,b"), "precious_data.csv")
  list.files(pattern = "*.csv")
  read.csv("precious_data.csv")
  },
  outfile = NA,
  venue = "so"
)

I get this output:

getwd()
#> [1] "/Users/jenny"
writeLines(c("V1,V2","a,b"), "precious_data.csv")
list.files(pattern = "*.csv")
#> [1] "precious_data.csv"
read.csv("precious_data.csv")
#>   V1 V2
#> 1  a  b

Created on 2018-09-19 by the reprex package (v0.2.1)

Using outfile = NA or outfile = "path/to/desired/file/base" is the general pattern for asserting control over the location of all files generated by reprex().

查看更多
登录 后发表回答