devtools::check() Error when checking examples

I'm getting an error which seems wrong when running check(), not sure how to fix this or if it's a bug?

The error is

Running examples in ‘slfhelper-Ex.R’ failed
The error most likely occurred in:

> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: get_anon_chi
> ### Title: Match on anon_chi to a dataset using CHI numbers
> ### Aliases: get_anon_chi
> ### ** Examples
> get_anon_chi(data)
Error in UseMethod("left_join") : 
  no applicable method for 'left_join' applied to an object of class "function"
Calls: get_anon_chi ... freduce -> withVisible -> <Anonymous> -> <Anonymous>
Execution halted

The function is below, the function works and if I run the examples in a clean environment they work fine too. The return of read_fst is a tibble but devtools seems to see it as a function.

#' Match on anon_chi to a dataset using CHI numbers
#' @param data tibble or data frame
#' @param chi_var CHI variable: the name of the variable containing CHI (default is chi)
#' @param drop Optional boolean indicating whether the existing chi_var should be dropped - default is TRUE
#' @return a tibble
#' @export
#' @examples
#' get_anon_chi(data)
#' get_anon_chi(data, drop = FALSE)
#' get_anon_chi(data, chi_var = "unique_id")
get_anon_chi <- function(data, chi_var = "chi", drop = TRUE) {
  default_name <- "chi"

  anon_chi_lookup <- fst::read_fst(
    "<FILE PATH>/CHI-to-Anon-lookup.fst"

  data <- data %>%
      by = setNames(default_name, chi_var)

  if (drop) {
    data <- data %>%
      dplyr::select(-{{ chi_var }})

Hi @Moohan! Welcome!

Have you considered whether it could be the first argument, data, that's causing the error, not anon_chi_lookup? Because data is most definitely a function in R, while I can't immediately see how anon_chi_lookup could wind up as one. Did you define data earlier in your examples?

Hi, data is the first parameter of the function so I thought that would be fine scope-wise. However I changed all instances to tibb since that definitely isn't a fucntion and I still get the same error....

I was thinking of where you called the function in the example as get_anon_chi(data) — from what you posted, it wasn't immediately clear to me what (other than the base function) was assigned to data when that line was run.

So is that tibb now, too? If so, then I still don't quite know what's assigned to tibb :sweat_smile:, but yes, perhaps the trouble lies elsewhere.

If you've ruled out the simpler possibility, I think it's going to be hard to debug this problem further without something closer to a reproducible example. I realize that could be hard in this case, but if you can narrow it down to just the code that's causing the devtools::check() failure, perhaps you could post an example mini-package inside an RStudio Cloud project for people to play with?

1 Like

Hi, my code is all on github: Although it's meant for internal use so most of it is unusable outside of our server environment.

Thanks for your help - the issue was as you thought, that in the example text 'data' didn't exists as an object so the funtion was being used...

I've reworked the examples and am now not getting any errors! (Still one warning and one note though)

1 Like

Hi @Moohan,
I've been working though the same error with the same symptoms: example failed but the function works in a clean environment. My issue turned out to be a double roxygen2 pragma:

#' @export
#' @export

Having checked your code, it's not the same issue, but this may be a clue - the error is not necessarily related to the code itself.

Hi @Moohan I am having a similar problem. I was wondering if you managed to solve it.