Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable non-R knitr engines #1149

Closed
nbenn opened this issue Aug 14, 2020 · 8 comments · Fixed by #1392
Closed

Enable non-R knitr engines #1149

nbenn opened this issue Aug 14, 2020 · 8 comments · Fixed by #1392
Labels
feature a feature request or enhancement markdown ⬇️

Comments

@nbenn
Copy link

nbenn commented Aug 14, 2020

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.

@hadley
Copy link
Member

hadley commented Apr 16, 2021

@nbenn just to be clear, you're suggesting that we just need to relax our restriction a little and more knitr engines should work?

@hadley hadley added feature a feature request or enhancement markdown ⬇️ labels Apr 16, 2021
@nbenn
Copy link
Author

nbenn commented Apr 17, 2021

Yes, that was precisely my suggestion. Currently knitr chunks are identified by checking whether some first 3 characters are in c("{r ", "{r}", "{r,"), thereby only allowing the "r" knitr engine. I think this could be relaxed to further knitr engines (maybe even all available), making possible for example the inclusion of a TikZ figure in roxygen2 generated docs (which I personally would find an awesome feature).

@gaborcsardi
Copy link
Member

I think it is fine to allow other engines as well for code fences. Inline code is more problematic, though.

@gadenbuie
Copy link

An additional use case: the new-ish verbatim knitr engine would be perfect for including snippets of R Markdown documents in documentation. Currently it requires @includeRmd or the verbatim chunk will be silently dropped or mangled.

@gaborcsardi
Copy link
Member

@gadenbuie I am not sure what you mean exactly, but if you want to escape code fences, then use can use four tick marks for that in commonmark, and thus in roxygen2:

#' ````
#' ```{r}
#' # comment
#' this <- 10
#' is <- this + 10
#' good <- this + is
#' ```
#' ````

@gadenbuie
Copy link

Sorry, I've since realized the missing code I'm seeing is actually #1350

@gadenbuie
Copy link

gadenbuie commented May 25, 2022

Still, verbatim could be useful since it would allow you to store the Rmd snippet somewhere and then include it via the file chunk option

#' ```{verbatim file="man/fragments/example.Rmd"}
#' ```
<!-- man/fragments/example.Rmd -->
```{r}
# comment
this <- 10
is <- this + 10
good <- this + is
```

@cderv
Copy link
Contributor

cderv commented May 31, 2022

@gadenbuie the embed engine would be a good fit here too - same as verbatim but adds a highlighting language to the chunk

```{embed}
man/fragments/example.Rmd
```

would produce

````md
```{r}
# comment
this <- 10
is <- this + 10
good <- this + is
```
````

but maybe highlighting is not supported for Markdown in rogygen2 though 

gaborcsardi added a commit that referenced this issue Jul 10, 2022
hadley pushed a commit that referenced this issue Jul 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement markdown ⬇️
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants