Confusing "Error in as.environment(where) : using 'as.environment(NULL)' is defunct" with using `devtools::document()`


#1

Hi, I'm writing my own package, and I'm getting the following error when using devtools::document() to (I think) try and document my functions

Error in as.environment(where) : using 'as.environment(NULL)' is defunct

Reprex-ing this is a bit tough, but do any of you have an idea of what this could be resulting from? Below is a fairly representative function in the package and my DESCRIPTION file, if that makes it any clearer. Thanks!

#' Returns a data frame of players' stats and player URLs for user supplied teams & seasons
#'
#' @param ... Function requires a `team_url`, `team_name`, `league`, and `season`. Additional data may be supplied. All of this information comes directly from `get_teams()`, if desired.
#' @param .progress Sets a Progress Bar. Defaults to FALSE.
#' @import dplyr
#' @examples 
#' 
#' # The function works in conjunction with get_teams()
#' teams <- get_teams("ohl", "2017-2018")
#' get_player_stats_team(teams)
#' 
#' # All functions are easily pipeable too
#' get_teams(c("shl", "allsvenskan"), c("2008-2009", "2009-2010", "2010-2011")) %>%
#'   get_player_stats_team(.progress = TRUE)
#'   
#' # It's also easy to get player stats for only 1 team   
#' get_teams("ncaa iii", "2017-2018") %>%
#'   filter(team == "Hamilton College") %>%
#'   get_player_stats_team()

get_player_stats_team <- function(..., .progress = FALSE) {
  
  if (.progress) {progress_bar <- progress_estimated(nrow(...), min_time = 0)}
  
  player_stats_team <- purrr::pmap_dfr(..., function(team_url, team_name, league, season, ...) {
    
    if (.progress) {progress_bar$tick()$print()}
    
    seq(5, 10, by = 0.001) %>%
      sample(1) %>%
      Sys.sleep()
    
    page <- team_url %>% xml2::read_html()
    
    player_stats <- page %>%
      rvest::html_node('[class="table table-striped table-sortable skater-stats highlight-stats"]') %>%
      rvest::html_table() %>%
      purrr::set_names("number", "name", "games_played", "goals", "assists", "points", "penalty_minutes", "plus_minus", "blank", "games_played_playoffs", "goals_playoffs", "assists_playoffs", "points_playoffs", "penalty_minutes_playoffs", "plus_minus_playoffs") %>%
      as_tibble() %>%
      filter(row_number() > 1) %>%
      filter(row_number() < if_else(!any(is.na(number) & name != ""), n(), which(is.na(number) & name != "")[1])) %>%
      filter(!is.na(number) & name != "") %>%
      mutate(position = stringr::str_split(name, "\\(", simplify = TRUE, n = 2)[,2]) %>%
      mutate(position = stringr::str_split(position, "\\)", simplify = TRUE, n = 2)[,1]) %>%
      mutate(name = stringr::str_split(name, "\\(", simplify = TRUE, n = 2)[,1]) %>%
      mutate_all(~na_if(., "-")) %>%
      mutate_all(~na_if(., "")) %>%
      mutate_all(stringr::str_squish) %>%
      select(-c(blank, number)) %>%
      select(name, position, everything())
    
    player_urls <- page %>%
      rvest::html_nodes("tbody+ tbody .txt-blue a") %>%
      rvest::html_attr("href") %>%
      as_tibble() %>%
      purrr::set_names("player_url") %>%
      filter(row_number() < if_else(!any(!stringr::str_detect(player_url, "player")), n(), which(!stringr::str_detect(player_url, "player"))[1]))
    
    all_data <- player_stats %>%
      bind_cols(player_urls) %>% 
      mutate(team = team_name) %>%
      mutate(league = league) %>%
      mutate(season = season) %>%
      mutate(team_url = team_url) %>%
      select(name, team, league, season, everything())
    
    return(all_data)
    
  })
  
  return(player_stats_team)
  
}
Package: elite
Title: A Package for Scraping Hockey Data from Elite Prospects
Version: 0.1
Authors@R: person("Evan", "Oppenheimer", email = "eoppe1022@gmail.com", role = c("aut", "cre"))
Description: The "elite" package allows users to easily scrape data from Elite Prospects in a [fairly] tidy manner.
Depends: R (>= 3.4.3)
Imports: dplyr, lubridate, magrittr, purrr, rvest, stringr, tidyr, xml2
License: CC0
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.1.0.9000


#2

Do you have the traceback?

Based on this, I'd venture a guess that where is NULL, but I don't think you're calling that directly (at least not in the code you've included here).

As devtools::document() is running, it usually prints what's going on as it progresses, so that could be a starting point for figuring out which part of your package it's getting hung up on (though the traceback should be informative there, as well)).


#3

Thanks for the response, @mara. Here's the output from traceback(), though I'm not sure what any of it means...

> traceback()
10: exists(name, env)
9: parser(call, env, block)
8: object_from_call(call = attr(block, "call"), env = env, block = block, 
       file = attr(block, "filename"))
7: block_find_object(block, env)
6: FUN(X[[i]], ...)
5: lapply(blocks, block_set_env, env = env, registry = registry, 
       global_options = options)
4: roxygen2::roxygenise(pkg$path, roclets = roclets, load_code = pkgload::pkg_ns)
3: force(code)
2: withr::with_envvar(r_env_vars(), roxygen2::roxygenise(pkg$path, 
       roclets = roclets, load_code = pkgload::pkg_ns))
1: document()

#4

Hmm…yeah, it basically looks like it's looking for an environment but can't find one (though that's just my best interpretation of something I don't understand all that well). The traceback will be helpful, though, for someone who knows more.


#5

This is a known problem with devel devtools / the recent roxygen release. It should be fixed by https://github.com/r-lib/devtools/pull/1849, which I just merged; so you should be able to fix this by updating to the latest version of devel devtools.

devtools::install_github("r-lib/devtools")