簡體   English   中英

R - 測試使用測試目錄外的數據文件

[英]R - testthat using data file outside the testing directory

我使用testthat測試包含類似於以下文件樹的包:

   .
    ├── 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目錄並且無法查看數據文件,給出以下錯誤:

...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

您的問題有兩種解決方案:

您可以使用相對路徑( ../data/testhaplom.out ):

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

或者您可以使用system.file來獲取數據目錄的位置:

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

我更喜歡第二種解決方案。

BTW: file.path在每個平台上使用正確的路徑分隔符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM