Correct workflow reading in local file for reprex

What is the correct way to make a reprex that uses data read in from a local file?

Running reprex::reprex() on a local file I get

TCGA_BRCA_mRNAex_annotation <- readr::read_csv("main/data/Data_TCGA_BrCa_RNASeq_Expression_Trends/TCGA_BRCA_mRNAex_annotation.csv")
#> Error: 'main/data/Data_TCGA_BrCa_RNASeq_Expression_Trends/TCGA_BRCA_mRNAex_annotation.csv' does not exist in current working directory ('/private/var/folders/xs/bp9xghmn1sdfp3h6yz2j389nq5fs7n/T/RtmptpOSoW').

Created on 2018-05-22 by the reprex package (v0.2.0).

because reprex is using temp files. So what I've done is reprex::reprex(outfile = NA) to make

TCGA_BRCA_mRNAex_annotation <- readr::read_csv("main/data/Data_TCGA_BrCa_RNASeq_Expression_Trends/TCGA_BRCA_mRNAex_annotation.csv")
#> Warning: Missing column names filled in: 'X1' [1]
#> Parsed with column specification:
#> cols(
#>   X1 = col_double(),
#>   X = col_double(),
#>   Hugo_Symbol = col_character(),
#>   Entrez_Gene_Id = col_double(),
#>   Entrez_Gene_ID_0 = col_double(),
#>   RefSeq_ID_0 = col_character(),
#>   Accession_0 = col_character(),
#>   Gene_symbol = col_character(),
#>   SNP = col_double(),
#>   CHR = col_double(),
#>   BP = col_double(),
#>   TCGAsymbolEQMETABRICsymbol = col_logical(),
#>   refseqID = col_character(),
#>   MatchingIlluminaProbeId = col_character()
#> )

Created on 2018-05-22 by the reprex package (v0.2.0).

but then now there are output files with extensions .R, .html, .md, .utf8.md, generated with names derived from tempfile(). How do I get the reprex without having these files generated?

Reading data in from a local file is contrary to the goals of a reproducible example, which are for the example to be self-contained and not dependent on having access to your local file system.

Even though you got reprex to run in your second example, the result isn't reproducible: I can't copy and paste it and have it run, because I still don't have access to your CSV file.

This discussion has several ideas for how to include your own data in a reproducible fashion:

If you'd like feedback or help on how to make any of those solutions work for your case, definitely go ahead and ask!

P.S. Thank you for asking this question — it's a common point of confusion! And thank you even more for trying to make a reprex :grinning:

2 Likes

Thanks @jcblum for the quick response! My objective was to share an example with only a colleague, who does have access to the local file system. I suppose using reprex would be overkill then in this case? The package was intended to produce examples for wider audiences.

I can see the benefit of using it in that context — one of my favorite things about reprex is how it seamlessly includes output as well as code, and you can’t beat the convenience.

As you’ve maybe already realized, reprex works by using knitr/Rmarkdown under the hood, which is why those intermediate files appear. Can you try setting outfile to a base name of your choice, instead of NA, so the files will at least be more easily removed with code?

The reprex package philosophy explains why reprex does what it does:
http://reprex.tidyverse.org/articles/reprex-dos-and-donts.html#package-philosophy

Maybe formatR::tidy_eval is enough for your use case?

1 Like

Thanks for the recommendation! After some investigation, the closest mimic to reprex for my use case is

clipr::write_clip(capture.output(formatR::tidy_eval(prefix = "#> ")))

An alternative using reprex could also be

reprex::reprex(outfile = "test")
file.remove(list.files(pattern = "test_reprex"))
1 Like