Ind*_*til 3 r reproducible-research tidyverse reprex
我经常使用reprex::reprex创建可重现的R代码示例来获得其他人的帮助以消除代码中的错误。通常,我使用像irisor 这样的数据集创建最少的示例mtcars,并且效果很好。但reprex每当我需要使用自己的数据时,我总是无法使用,因为问题非常具体,我不能依赖datasets库中的数据集。
在这种情况下,我收到以下错误:
# 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
Run Code Online (Sandbox Code Playgroud)
由reprex 包(v0.2.0) 于 2018-02-19 创建。
在其他地方有一个关于前reprex时代的精彩讨论(如何制作一个伟大的 R 可重现示例?)。作者建议使用类似dput-
如果您有一些数据很难使用这些技巧构建,那么您始终可以使用例如 或索引来创建原始数据的子
head()集subset()。然后使用例如。给我们一些可以立即dput()投入的东西R
但也提到——
如果您的数据框具有多个级别的因子,则
dput输出可能会很麻烦,因为它仍然会列出所有可能的因子级别,即使它们不存在于数据子集中。
因此,如果我想使用我的完整数据集,这不是一个好的选择。
总之:
任何人都知道如何创建一个reprex独立的文件,即使它依赖于使用包含所有数据的本地文件?
默认情况下,reprex 强烈鼓励在会话临时目录中执行。但有时不可避免地要引用特定的本地文件,所以是的,必须有一种方法来做到这一点。
要请求所有工作都在当前工作目录中完成,请设置outfile = NA. (更一般地,您可以使用outfile参数来指定基本文件名和路径。)
如果我提交此 reprex,并将工作目录设置为我的主目录:
reprex({
getwd()
writeLines(c("V1,V2","a,b"), "precious_data.csv")
list.files(pattern = "*.csv")
read.csv("precious_data.csv")
},
outfile = NA,
venue = "so"
)
Run Code Online (Sandbox Code Playgroud)
我得到这个输出:
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
Run Code Online (Sandbox Code Playgroud)
由reprex 包(v0.2.1)于 2018-09-19 创建
使用outfile = NAoroutfile = "path/to/desired/file/base"是断言对 . 生成的所有文件的位置进行控制的一般模式reprex()。