glue icon indicating copy to clipboard operation
glue copied to clipboard

glue knitr engine and multiline chunks

Open krlmlr opened this issue 1 year ago • 4 comments

Chunks of the form

```{glue}
line 1
line 2
```

give an error, we probably need paste0(collapse = "\n") similarly to the glue_sql engine.

Tests for those engines (snapshot tests?) would be great too, these are currently excluded from the coverage checks.

krlmlr avatar Jan 20 '24 09:01 krlmlr

I just found this error. This is quite annoying. Multiline cell is quite useful for producing big test output content

---
output: 
  html_document:
    keep_md: true
---

```{r}
#| label: setup
#| include: false
library(glue)
```



```{glue, results = 'asis', echo = FALSE}
#### mtcars has **{nrow(mtcars)} rows** and _{ncol(mtcars)} columns_.

Another line below
```

The issue.


Error:
! All unnamed arguments must be length 1
Backtrace:
     ▆
  1. └─global .main()
  2.   └─execute(...)
  3.     └─rmarkdown::render(...)
  4.       └─knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
  5.         └─knitr:::process_file(text, output)
  6.           ├─xfun:::handle_error(...)
  7.           ├─base::withCallingHandlers(...)
  8.           └─knitr:::process_group(group)
  9.             └─knitr:::call_block(x)
 10.               └─knitr:::block_exec(params)
 11.                 ├─knitr:::in_input_dir(engine(options))
 12.                 │ └─knitr:::in_dir(input_dir(), expr)
 13.                 └─glue (local) engine(options)
 14.                   ├─base::do.call(glue, c(list(options$code), glue_options))
 15.                   └─glue (local) `<fn>`(`<chr>`, .envir = `<env>`)
 16.                     └─glue::glue_data(...)

From https://github.com/tidyverse/glue/blob/a3f80d678274ef634c10c2cb094c939b1543222a/R/glue.R#L134-L136

Probably this is the line missing for eng_glue https://github.com/tidyverse/glue/blob/a3f80d678274ef634c10c2cb094c939b1543222a/R/knitr.R#L24 as this only is done https://github.com/tidyverse/glue/blob/a3f80d678274ef634c10c2cb094c939b1543222a/R/knitr.R#L6

cderv avatar Mar 19 '25 10:03 cderv

@cderv I am definitely not working on glue right now, so if this is bugging you and you see a likely path forward, I encourage you to make a PR.

jennybc avatar Mar 19 '25 17:03 jennybc

FWIW, I've been using the following to enhance the glue chunk to support inline expressions of the form `r expr` and to work around this issue.

knitr::opts_hooks$set(echo = function(options) {
  if (options$engine == "glue") {
    options$echo <- FALSE
    options$.open <- "`r "
    options$.close <- "`"
    options$.trim <- FALSE
    if (options$results == "hide") {
      options$code <- "NULL"
    } else {
      # https://github.com/tidyverse/glue/issues/319
      options$code <- paste0(options$code, collapse = "\n")
    }
  }
  return(options)
})

krlmlr avatar Mar 19 '25 18:03 krlmlr

to support inline expressions of the form r expr

Really nice idea !

I am definitely not working on glue right now, so if this is bugging you and you see a likely path forward, I encourage you to make a PR.

Noted. I'll do one if I find the time slot for some R package work.

cderv avatar Mar 20 '25 10:03 cderv