Skip to contents

This function completely removes a model specification that was previously created by create_keras_sequential_spec() or create_keras_functional_spec(). It cleans up both the function in the user's environment and all associated registrations within the parsnip package.

Usage

remove_keras_spec(model_name, env = parent.frame())

Arguments

model_name

A character string giving the name of the model specification function to remove (e.g., "my_mlp").

env

The environment from which to remove the function and its update() method. Defaults to the calling environment (parent.frame()).

Value

Invisibly returns TRUE after attempting to remove the objects.

Details

This function is essential for cleanly unloading a dynamically created model. It performs three main actions:

  1. It removes the model specification function (e.g., my_mlp()) and its corresponding update() method from the specified environment.

  2. It searches parsnip's internal model environment for all objects whose names start with the model_name and removes them. This purges the fit methods, argument definitions, and other registrations.

  3. It removes the model's name from parsnip's master list of models.

This function uses the un-exported parsnip:::get_model_env() to perform the cleanup, which may be subject to change in future parsnip versions.

Examples

if (FALSE) { # \dontrun{
if (requireNamespace("keras3", quietly = TRUE)) {
  # First, create a dummy spec
  input_block <- function(model, input_shape) keras3::keras_model_sequential(input_shape = input_shape)
  dense_block <- function(model, units = 16) model |> keras3::layer_dense(units = units)
  create_keras_sequential_spec("my_temp_model", list(input = input_block, dense = dense_block), "regression")

  # Check it exists in the environment and in parsnip
  exists("my_temp_model")
  "my_temp_model" %in% parsnip::show_engines("my_temp_model")$model

  # Now remove it
  remove_keras_spec("my_temp_model")

  # Check it's gone
  !exists("my_temp_model")
  !"my_temp_model" %in% parsnip::show_engines(NULL)$model
}
} # }