Set a section in a ferx model

Description

Replaces the body of a named section in a .ferx model with new lines, leaving all other sections untouched, and returns x so calls can be chained with |> or %>%.

Usage

ferx_model_set_section(x, section, lines)

Arguments

  • x: A ferx_model object or a path to a .ferx file.
  • section: Name of the section to replace, without brackets (e.g. "fit_options").
  • lines: Character vector of replacement lines (do not include the [section] header line).

Details

When x is a ferx_model object the section is written to x$model. Copy-on-write for bundled examples: if x$model points to a file inside the installed ferx package directory (typically the result of [ferx_example](ferx_example.qmd)()), the file is first copied to tempdir() and x$model is updated to that copy before the edit. This prevents accidental modification of read-only package examples. When x is a plain path string the file is edited in place.

Seealso

[ferx_model_get_section](ferx_model_get_section.qmd)Other model-editing: [ferx_model](ferx_model.qmd), [ferx_model_edit](ferx_model_edit.qmd), [ferx_model_get_section](ferx_model_get_section.qmd), [ferx_model_inspect](ferx_model_inspect.qmd), [ferx_model_show](ferx_model_show.qmd), [ferx_model_validate](ferx_model_validate.qmd)

Concept

model-editing

Value

x, invisibly. For a ferx_model pointing at a bundled package file, the returned object’s $model field will point at the temp-directory copy that was actually edited.

Examples

ex <- ferx_example("warfarin")

# Plain path: edited in place
ferx_model_set_section(ex$model, "fit_options", c(
  "  method     = focei",
  "  maxiter    = 500",
  "  covariance = true"
))

# ferx_model: safe by default. Editing a bundled example transparently
# copies the file to tempdir() and edits the copy; ex$model on disk is
# untouched.
fit <- ferx_model(ex$data, ex$model) |>
  ferx_model_set_section("fit_options", c(
    "  method     = focei",
    "  maxiter    = 500",
    "  covariance = true"
  )) |>
  ferx_fit() |>
  summary()