R-使用测试目录外的数据文件进行测试 [英] R - testthat using data file outside the testing directory

查看:98
本文介绍了R-使用测试目录外的数据文件进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用testthat来测试具有类似于以下内容的文件树的软件包:

I am using testthat to test a package with a file tree similar to the following:

   .
    ├── data
    │   └── testhaplom.out
    ├── inst
    │   └── test
    │       ├── test1.r
    │       ├── tmp_S7byVksGRI6Q
    │       │   └── testm.desc
    │       └── tmp_vBcIkMN1arbn
    │           ├──testm.bin
    │           └── testm.desc
    ├── R
    │   ├── haplom.r
    │   └── winIdx.r
    └── tmp_eUG3Qb0PKuiN
        └── testhaplom.hap2.desc

test1.r文件中,我需要将data/testhaplom.out文件用作特定功能的输入数据,但是如果执行test_file(test1.r),它将切换到inst/test目录,并且看不到数据文件,出现以下错误:

In the test1.r file, I need to use the data/testhaplom.out file as input data for a certain function, but if I do test_file(test1.r), it changes into the inst/test directory and cannot see the data file, giving the error below:

...Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  cannot open file 'data/testhaplom.out': No such file or directory

推荐答案

针对您的问题有两种解决方案:

There are two solutions for your problem:

您可以使用相对路径(../data/testhaplom.out):

You could use the relative path (../data/testhaplom.out):

expect_true(file.exists(file.path("..", "data", "testhaplom.out")))

或者您可以使用system.file获取数据目录的位置:

Or you could use system.file to get the location of the data directory:

expect_true(file.exists(file.path(system.file("data", package="YOUR_R_PACKAGE"), "testhaplom.out")))

我更喜欢第二种解决方案.

I prefer the second solution.

顺便说一句:file.path在每个平台上使用正确的路径分隔符.

BTW: file.path use the correct path separator on each platform.

这篇关于R-使用测试目录外的数据文件进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆