3

I'm working on a project, where I load a json file. By default, I start the project at the root of the project, however when testing, the path changes. Therefore I included the following statement:

if (grepl("testthat", getwd())) {
    template <- jsonlite::read_json("../../templates/initial_status.json")
  }else{
    template <- jsonlite::read_json("./templates/initial_status.json")
  }

This works, however, when using covr::gitlab() the file cannot be opened. I guess the working directory changed and therefore the file is not found:

Error in `open.connection(con, "rb")`: kann Verbindung nicht öffnen
Backtrace:
    x
 1. \-MyPackage::api_create(...) at test-API.R:40:2
 2.   \-MyPackage:::get_initial_status(initial_status)
 3.     \-jsonlite::read_json("../../templates/initial_status.json")

Of course a working around is to use setwd(...) however I don't like this, because I want to publish the package and therefore user may place the package in different locations.

What's the best solution here?

Ai4l2s
  • 525
  • 2
  • 9
  • Maybe this is why people prefer `here::here()`, but I don't know exactly. – Mossa May 10 '22 at 08:44
  • hey thnak for the tip! I now use `path <- here("templates", "initial_status.json") template <- jsonlite::read_json(path)`. while this is working for testing, `covr` still fails with the same error message :/ – Ai4l2s May 10 '22 at 09:10
  • is this project open source? link to the repo? – Mossa May 10 '22 at 09:13
  • no can't publish in the moment but it's planned – Ai4l2s May 10 '22 at 09:16
  • Put a `debugonce(jsonlite::read_json)` and see what happens.. – Mossa May 10 '22 at 09:18
  • does not change anything. should this print something? – Ai4l2s May 10 '22 at 10:51
  • seems like `covr` set workspace to s.th. like `C:/Users/%USERNAME%/AppData/Local/Temp/RtmpWCdAqq/R_LIBS43b46f466da2/MyPackage` which does only include test folder... – Ai4l2s May 10 '22 at 11:01
  • Alright; Try to use the `inst` folder in an R-package, i.e. [this](https://r-pkgs.org/inst.html) – Mossa May 10 '22 at 11:56
  • It is meant to be the folder of stuff that has to be there, without being manipulated or removed. – Mossa May 10 '22 at 11:57

0 Answers0