Skip to contents

This function inserts nodes into an rmd_ast at specified locations relative to selected nodes. The selection is implemented using the same approach as rmd_select() which enables a variety of useful syntax for selecting nodes.

The function checks if the selected indices form continuous ranges and can either insert before or after that range. allow_multiple parameter can be used to allow insertion based on multiple discontinuous ranges.

Usage

rmd_insert(
  x,
  ...,
  nodes,
  location = c("before", "after"),
  allow_multiple = FALSE
)

Arguments

x

Rmd object, e.g. rmd_ast or rmd_tibble, to insert the nodes.

...

One or more unquoted expressions separated by commas for node selection. Uses the same syntax as rmd_select().

nodes

Nodes to insert. Can be a single rmd node object, a list of rmd node objects, or an rmd_ast object.

location

Character. Either "before" or "after" to specify where to insert relative to the selected nodes. "before" inserts before the first node of each selected range, "after" inserts after the last node of each selected range.

allow_multiple

Logical. If FALSE (default), throws an error when selected indices are discontinuous. If TRUE, allows inserting at multiple discontinuous locations.

Value

Returns the modified Rmd object with nodes inserted at the specified locations.

Examples

rmd = parse_rmd(system.file("examples/hw01.Rmd", package = "parsermd"))

new_nodes = list(
  rmd_markdown(lines = "This is a comment"),
  rmd_chunk(engine = "r", code = "# New code")
)
rmd_insert(rmd, "plot-dino", nodes = new_nodes, location = "after")
#> ├── YAML [2 fields]
#> ├── Heading [h3] - Load packages
#> │   └── Chunk [r, 2 lines] - load-packages
#> ├── Heading [h3] - Exercise 1
#> │   ├── Markdown [1 line]
#> │   └── Heading [h4] - Solution
#> │       └── Markdown [1 line]
#> ├── Heading [h3] - Exercise 2
#> │   ├── Markdown [1 line]
#> │   └── Heading [h4] - Solution
#> │       ├── Markdown [3 lines]
#> │       ├── Chunk [r, 5 lines] - plot-dino
#> │       ├── Markdown [1 line]
#> │       ├── Chunk [r, 1 line] - 
#> │       ├── Markdown [1 line]
#> │       └── Chunk [r, 2 lines] - cor-dino
#> └── Heading [h3] - Exercise 3
#>     ├── Markdown [1 line]
#>     └── Heading [h4] - Solution
#>         ├── Markdown [3 lines]
#>         ├── Chunk [r, 1 line] - plot-star
#>         ├── Markdown [1 line]
#>         └── Chunk [r, 1 line] - cor-star

new_heading = rmd_heading(name = "Analysis", level = 2L)
rmd_insert(rmd, has_type("rmd_chunk"), nodes = new_heading, 
           location = "before", allow_multiple = TRUE)
#> ├── YAML [2 fields]
#> ├── Heading [h3] - Load packages
#> ├── Heading [h2] - Analysis
#> │   ├── Chunk [r, 2 lines] - load-packages
#> │   ├── Heading [h3] - Exercise 1
#> │   │   ├── Markdown [1 line]
#> │   │   └── Heading [h4] - Solution
#> │   │       └── Markdown [1 line]
#> │   └── Heading [h3] - Exercise 2
#> │       ├── Markdown [1 line]
#> │       └── Heading [h4] - Solution
#> │           └── Markdown [3 lines]
#> ├── Heading [h2] - Analysis
#> │   ├── Chunk [r, 5 lines] - plot-dino
#> │   └── Markdown [1 line]
#> ├── Heading [h2] - Analysis
#> │   ├── Chunk [r, 2 lines] - cor-dino
#> │   └── Heading [h3] - Exercise 3
#> │       ├── Markdown [1 line]
#> │       └── Heading [h4] - Solution
#> │           └── Markdown [3 lines]
#> ├── Heading [h2] - Analysis
#> │   ├── Chunk [r, 1 line] - plot-star
#> │   └── Markdown [1 line]
#> └── Heading [h2] - Analysis
#>     └── Chunk [r, 1 line] - cor-star

rmd_insert(rmd, c(1, 3, 5), 
  nodes = rmd_markdown(lines = "Separator"), 
  location = "after", 
  allow_multiple = TRUE)
#> ├── YAML [2 fields]
#> ├── Markdown [1 line]
#> ├── Heading [h3] - Load packages
#> │   ├── Chunk [r, 2 lines] - load-packages
#> │   └── Markdown [1 line]
#> ├── Heading [h3] - Exercise 1
#> │   ├── Markdown [1 line]
#> │   ├── Markdown [1 line]
#> │   └── Heading [h4] - Solution
#> │       └── Markdown [1 line]
#> ├── Heading [h3] - Exercise 2
#> │   ├── Markdown [1 line]
#> │   └── Heading [h4] - Solution
#> │       ├── Markdown [3 lines]
#> │       ├── Chunk [r, 5 lines] - plot-dino
#> │       ├── Markdown [1 line]
#> │       └── Chunk [r, 2 lines] - cor-dino
#> └── Heading [h3] - Exercise 3
#>     ├── Markdown [1 line]
#>     └── Heading [h4] - Solution
#>         ├── Markdown [3 lines]
#>         ├── Chunk [r, 1 line] - plot-star
#>         ├── Markdown [1 line]
#>         └── Chunk [r, 1 line] - cor-star