devtools::document() give: Error in importIntoEnv(pkgenv, exports, nsenv, exports) : cannot add bindings to a locked environment

Hi,

The function below works, but I get error when using devtools::document() for it.
Any help is much appreciated. Thanks in advance.
John

#' Trains to a numeric variable.
#'
#' @param x Wordembeddings from textImport.
#' @param y The numeric variable to predict.
#' @return A correlation
#' @importFrom dplyr select starts_with filter
#' @importFrom recipes recipe step_naomit step_center step_scale step_pca
#' @importFrom rsample vfold_cv
#' @importFrom parsnip linear_reg set_engine
#' @importFrom tune control_grid tune_grid select_best collect_predictions
#' @export
Train <- function(x, y){
   df <- cbind(df, y)

  # Recipe
  recipe <-
    recipes::recipe(y ~ .,
                    data = df) %>%
    recipes::step_center(all_predictors()) %>%
    recipes::step_scale(all_predictors())

  # Cross-validation
  cv_splits <- rsample::vfold_cv(df, v = 2)

  # Model
  model <-
    parsnip::linear_reg(penalty = tune(), mixture = tune()) %>%
    parsnip::set_engine("glmnet")

  # Tuning parameters
  glmn_grid <- base::expand.grid(
    penalty = 10 ^ seq(-3, -1, length = 20),
    mixture = (0:5) / 5
    )

  ctrl <- tune::control_grid(save_pred = TRUE)

  # Tune_grid
  glmn_tune <- tune::tune_grid(
    recipe,
    model = model,
    resamples = cv_splits,
    grid = glmn_grid,
    control = ctrl
    )

  # Select the best penelty and mixture based on rmse
  best_glmn <- tune::select_best(glmn_tune, metric = "rmse", maximize = TRUE)

  # Get predictions and observed
  predictions <- tune::collect_predictions(glmn_tune) %>%
    dplyr::filter(penalty == best_glmn$penalty, mixture == best_glmn$mixture)
  predictions
}

Example Data

 set.seed(42)
 V1 <- runif(10, 1, 10)
 V2 <- runif(10, 1, 10)
 df <- tibble(V1, V2)
 y <- runif(10, 1, 10)
Train(df, y)

After having restarted computer and updating OS; and more importantly, removing all files concerning devtools::document() in the folder and then adding them back ono by one I do not get this issue anymore .

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

Great. Please mark the solution for the benefit of those to follow.

1 Like