在读取本地文件的 r 中使用 reprex 包创建可重现的示例
creating reproducible example using reprex package in r where a local file is being read
我经常使用 reprex::reprex
创建可重现的 R
代码示例,以从其他人那里获得帮助以消除我的代码中的错误。通常,我使用 iris
或 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
由 reprex package (v0.2.0) 创建于 2018-02-19。
reprex
之前的时代在其他地方 (How to make a great R reproducible example?) 进行了很好的讨论。作者建议使用 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
而且还提到-
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.
所以,如果我想使用我的完整数据集,这不是一个好的选择。
总结:
有谁知道如何创建独立的 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"
)
我得到这个输出:
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
由 reprex package (v0.2.1)
创建于 2018-09-19
使用 outfile = NA
或 outfile = "path/to/desired/file/base"
是控制 reprex()
.
生成的所有文件位置的通用模式
我经常使用 reprex::reprex
创建可重现的 R
代码示例,以从其他人那里获得帮助以消除我的代码中的错误。通常,我使用 iris
或 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
由 reprex package (v0.2.0) 创建于 2018-02-19。
reprex
之前的时代在其他地方 (How to make a great R reproducible example?) 进行了很好的讨论。作者建议使用 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
而且还提到-
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.
所以,如果我想使用我的完整数据集,这不是一个好的选择。
总结:
有谁知道如何创建独立的 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"
)
我得到这个输出:
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
由 reprex package (v0.2.1)
创建于 2018-09-19使用 outfile = NA
或 outfile = "path/to/desired/file/base"
是控制 reprex()
.