Johannes Schwenzer
Johannes Schwenzer

Reputation: 63

handling downloaded files in examples for CRAN publishing

I wrote a library which I want to publish to CRAN. Adhering with all the requirements added a lot of bloat and annoying extras to the library that only exist such that every function has a runnable example. Since the functions depend on the output of each other I had to include about 13 extra datasets which all had to be documented meticulously. Basically I spent about half a year of development simply on those requirements without improving the actual functionality. I encountered now the following problem when running devtools::check_win_devel():

Running examples in 'oRaklE-Ex.R' failed
The error most likely occurred in:

> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: long_term_future
> ### Title: Long-term trend predictions for future years
> ### Aliases: long_term_future
> 
> ### ** Examples
> 
> working_directory <- getwd()
> setwd(tempdir())
> example_longterm_future_predictions <- long_term_future(example_longterm_future_macro_data)
Warning in readChar(con, 5L, useBytes = TRUE) :
  cannot open compressed file './FR/models/longterm/best_lm_model1.Rdata', probable reason 'No such file or directory'
Error in readChar(con, 5L, useBytes = TRUE) : cannot open the connection
Calls: long_term_future -> load -> readChar
Execution halted

the issue is that I need to access a linear model from a previous function to get my results. The way I handled this is the following:

  1. I save the current working directory path into a variable

  2. switch the wd to a temporary one

  3. the function checks if the current wd contains "Temp" like this: if (grepl("Temp", getwd())) {...}

  4. If yes make the model new and save it

  5. get the model like this:

    for (i in 1:3){
        model_path= paste0("./", unique(longterm_future_macro_data$country),"/models/longterm/best_lm_model",i,".Rdata") ...}
    
  6. now that the model is loaded do the rest.

How would you go about fixing the error I got? Saving the model in inst/extdata/ and accessing it with system.file() isn't an option because I have another function that needs to load 84 models.

Upvotes: 0

Views: 18

Answers (1)

Johannes Schwenzer
Johannes Schwenzer

Reputation: 63

So the answer is to generalize the logic that checks for temporary folders as such:

if (grepl("Rtmp", getwd()))

instead of:

if (grepl("Temp", getwd())) 

Upvotes: 0

Related Questions