Skip to content

Enable non-R knitr engines #1149

@nbenn

Description

@nbenn

I'm trying to include a TikZ image in the docs of a package of mine. While looking at my options, i was delighted to see that roxygen2 supports knitr code chunks. Combined with the tikz engine support of knitr I thought I had all I needed to make this work. Unfortunately, currently there is a quite restrictive check in place for what is considered a knitr code chunk

roxygen2/R/markdown.R

Lines 73 to 77 in c73def4

is_markdown_code_node <- function(x) {
info <- str_sub(xml_attr(x, "info"), 1, 3)
str_sub(xml_text(x), 1, 2) == "r " ||
(!is.na(info) && info %in% c("{r ", "{r}", "{r,"))
}

Playing around with this a bit, I was seamlessly able to generate my TikZ image from code simply by relaxing this check a bit by including other knitr engines as reported by knitr::knit_engines$get().

markdown_engine_regexp <- function() {
  opts <- c("r", names(knitr::knit_engines$get()))
  paste0("^\\{(", paste(opts, collapse = "|"), ")[, \\}]")
}

is_markdown_code_node <- function(x) {
  info <- xml_attr(x, "info")
  regx <- markdown_engine_regexp()
  str_sub(xml_text(x), 1, 2) == "r " ||
    (!is.na(info) && grepl(regx, info))
}

See also: master...nbenn:master

While I realize that knitr support in roxygen2 is a new and somewhat experimental addition, I was wondering whether you are open to expanding this powerful feature by allowing non-R language engines to be run. I'm happy to submit a PR if this is the case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions