问题描述
限时送ChatGPT账号..我经常使用 reprex::reprex
来创建可重现的 R
代码示例,以从其他人那里获得帮助以消除我的代码中的错误.通常,我使用 iris
或 mtcars
等数据集创建最少的示例,并且效果很好.但我总是无法使用 reprex
任何时候我需要使用我自己的 数据,因为问题太具体了,我不能依赖 datasets<中的数据集/code> 库.
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
由 reprex 包 (v0.2.0) 于 2018 年 2 月 19 日创建.
Created on 2018-02-19 by the reprex package (v0.2.0).
reprex
之前的其他地方有一个很好的讨论(如何制作出色的 R 可重现示例?).作者推荐使用类似 dput
-
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
-
如果您有一些难以构建的数据,则使用这些提示,那么您始终可以制作原始数据的子集,使用例如 head()
、subset()
或索引.然后使用例如.dput()
到给我们一些可以立即放入 R
的东西
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
但也提到-
如果你的数据框有一个多层次的因子,dput
输出可能很笨重,因为它仍然会列出所有可能的因素即使它们不存在于您的数据子集中.
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.
总结:
有人知道如何创建一个独立的 reprex
,即使它依赖于使用包含所有数据的本地文件?
Anyone knows how to create a reprex
which is standalone even if it relies on using a local file containing all of your data?
推荐答案
默认情况下,reprex 强烈鼓励在会话临时目录中执行.但有时不可避免地要引用特定的本地文件,所以是的,必须有办法做到这一点.
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.
要请求在当前工作目录中完成所有工作,请设置outfile = NA
.(更一般地,您可以使用 outfile
参数来指定基本文件名和路径.)
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.)
如果我提交此 reprex,并将工作目录设置为我的主目录:
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"
)
我得到这个输出:
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 包 (v0.2.1) 于 2018 年 9 月 19 日创建上>
Created on 2018-09-19 by the reprex package (v0.2.1)
使用 outfile = NA
或 outfile = "path/to/desired/file/base"
是对由reprex()
.
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()
.
这篇关于在 r 中使用 reprex 包创建可重现的示例,其中正在读取本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论