3

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?

www
  • 38,575
  • 12
  • 48
  • 84
Indrajeet Patil
  • 4,673
  • 2
  • 20
  • 51
  • The error says there's no such file in your working directory. You need to change your working directory to point to where the file is located. – IRTFM Feb 19 '18 at 21:20
  • 1
    Reduce the problem to a **minimal** reproducible example. Get it down to a size that makes sense to `dput`. Rarely does an example require all your data. StackOverflow doesn't allow you to upload data files so your problem should be self contained. If you absolutely need a particular full data file, post it publicly to the internet and use the URL to point people to it, eg `read.csv("http://www.myspecial.com/data.txt")` – MrFlick Feb 19 '18 at 21:23
  • That error persists even if my working directory contains that file. – Indrajeet Patil Feb 19 '18 at 21:23
  • 1
    Uploading your file to a site like [ShareCSV](http://www.sharecsv.com/) would be easier like MrFlick suggested – Tung Feb 19 '18 at 22:13
  • If your data is less than 25MB you can even post it to Github and `read.csv("http://raw.githubusercontent.com/rest/of/url/to/your/data")` straight into the R session. But definitely follow @MrFlick 's guidelines above too :) – mysteRious Mar 21 '18 at 21:23

1 Answers1

6

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().

jennybryan
  • 2,606
  • 2
  • 18
  • 33