Chunk options can be written in special comments (e.g., after #| for R code chunks) inside a code chunk. This function partitions these options from the chunk body.

divide_chunk(engine, code)

Arguments

engine

The name of the language engine (to determine the appropriate comment character).

code

A character vector (lines of code).

Value

A list with the following items:

  • options: The parsed options (if there are any) as a list.

  • src: The part of the input that contains the options.

  • code: The part of the input that contains the code.

Note

Chunk options must be written on continuous lines (i.e., all lines must start with the special comment prefix such as #|) at the beginning of the chunk body.

Examples

# parse yaml-like items
yaml_like = c("#| label: mine", "#| echo: true", "#| fig.width: 8", "#| foo: bar",
    "1 + 1")
writeLines(yaml_like)
#> #| label: mine
#> #| echo: true
#> #| fig.width: 8
#> #| foo: bar
#> 1 + 1
xfun::divide_chunk("r", yaml_like)
#> $options
#> $options$label
#> [1] "mine"
#> 
#> $options$echo
#> [1] TRUE
#> 
#> $options$fig.width
#> [1] 8
#> 
#> $options$foo
#> [1] "bar"
#> 
#> 
#> $src
#> [1] "#| label: mine"  "#| echo: true"   "#| fig.width: 8" "#| foo: bar"    
#> 
#> $code
#> [1] "1 + 1"
#> 

# parse CSV syntax
csv_like = c("#| mine, echo = TRUE, fig.width = 8, foo = 'bar'", "1 + 1")
writeLines(csv_like)
#> #| mine, echo = TRUE, fig.width = 8, foo = 'bar'
#> 1 + 1
xfun::divide_chunk("r", csv_like)
#> $options
#> $options$label
#> [1] "mine"
#> 
#> $options$echo
#> [1] TRUE
#> 
#> $options$fig.width
#> [1] 8
#> 
#> $options$foo
#> [1] "bar"
#> 
#> 
#> $src
#> [1] "#| mine, echo = TRUE, fig.width = 8, foo = 'bar'"
#> 
#> $code
#> [1] "1 + 1"
#>