在 r 中使用 reprex 包创建可重现的示例,其中正在读取本地文件

编程入门 行业动态 更新时间:2024-10-12 05:52:12
本文介绍了在 r 中使用 reprex 包创建可重现的示例,其中正在读取本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我经常使用 reprex::reprex 来创建可重现的 R 代码示例,以从其他人那里获得帮助以消除我的代码中的错误.通常,我使用 irismtcars 等数据集创建最少的示例,并且效果很好.但我总是无法使用 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 in R 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 = NAoutfile = "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:关键词]

本文发布于:2023-04-30 13:22:23,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   本地文件   reprex

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!