Open GegznaV opened 1 year ago
The output is not really handle by Quarto (you'll get the same thing with rmarkdown
).
If you want special formatting, you'll have to use chunk hooks for example.
Related discussion for cli
: #3957.
Here is a partial example to get you started:
---
title: "Quarto Playground"
format: html
---
This is a playground for Quarto.
```{r}
library(knitr)
knit_print.Desc <- function(x, options) {
fansi::sgr_to_html(
x = paste(capture.output(DescTools:::print.Desc(x)), collapse = "\n"),
warn = FALSE,
term.cap = "256"
)
}
registerS3method("knit_print", "Desc", knit_print.Desc)
#| output: asis
library(DescTools)
DescTools::Desc(with(airquality, table(OzHi = Ozone > 80, Month)), plotit = FALSE)
Closing this, as it is not planned to handle any possible output, especially because, the same would need to be done for all R packages and the same for any supported languages.
I'm going to reopen this as an enhancement issue: we already process ANSI color code in jupyter output, and it might be possible to do something in the knitr side as well.
Oh really, there have been few discussion about knitr and ansi colour, especially for cli. Great news!
@cscheid Thanks for correcting me on this 😉
See https://github.com/quarto-dev/quarto-cli/issues/4204. Even in Julia our support isn't great, and ideally we'd fix it across all engines.
@mcanouil, thank you for your insights. Still, iId like to clarify 2 things:
1) In:
```{r} #| output: asis library(DescTools) DescTools::Desc(with(airquality, table(OzHi = Ozone > 80, Month)), plotit = FALSE)
Did you mean #| results: asis
?
2) Using the hook, is this a reasonable way to remove ANSI codes:
```{r}
library(knitr)
knit_print.Desc <- function(x, options) {
stringr::str_remove_all(
paste(capture.output(DescTools:::print.Desc(x)), collapse = "\n"),
"\\x1b\\[[0-9;]*m"
)
}
registerS3method("knit_print", "Desc", knit_print.Desc)
output
is Quarto, but both are supported internally by Quarto.From the code, it seems, one could use options("crayon.enabled" = TRUE/FALSE)
, but it does not seem to actually work for all ANSI colouring (definitely a bug in DescTools
).
FALSE | TRUE |
---|---|
we already process ANSI color code in jupyter output, and it might be possible to do something in the knitr side as well.
@cscheid usually the R packages that does the ANSI color (like cli) usually deactivate them when knitting is detected. So there is nothing to fix in Quarto I believe. But if we want to bring the feature in Quarto to be compatible with knitr, we could make the other tools reactivate it when in Quarto. (or happy to bring the feature in R Markdown too if possible).
Interesting to work on this with you when we do.
If possible, the colored output would be appreciated too. But at least no extra symbols would be nice too.
This is really an upstream issue here: Desctool package is the one outputing this characters using cat()
https://github.com/AndriSignorell/DescTools/blob/214a6615769fb4c2d39734c88595d968912ce72b/R/Desc.R#L1388-L1390
if (!x$approx.ok) {
cat("\033[31m\nWarning message:\n Exp. counts < 5: Chi-squared approx. may be incorrect!!\n\033[39m")
}
They are not using cli or other tools that knows when deactivate ANSI escape characters that are meant for Terminal and not for document like HTML and PDF.
So an issue should be open in DescTool IMO as they are outputing character you don't want in your output document. Even if we do support it in Quarto, it will be an issue for other documentation system. @GegznaV are you willing to open one there ?
Quarto or R Markdown are just showing what the package is cat()
-ing.
This is really an upstream issue here: Desctool package is the one outputing this characters using cat() AndriSignorell/DescTools@214a661/R/Desc.R#L1388-L1390
FWIW, this specific issue have been solved in DescTools directly. See https://github.com/AndriSignorell/DescTools/commit/822c734abb3789c95c0cee99d5aea82f62389016#
pandoc 3.4 was just released and it includes a new ANSI output format. Wonder if this could be levraged by Quarto for this (when Quarto uses pandoc 3.4).
New output format: ansi (for formatted console output) (Evan Silberman). Most Pandoc elements are supported and printed in a reasonable way, if not always ideally. This version does no detection of terminal capabilities, nor does it fall back to different output styles for less-capable terminals.
I saw that. My understanding is that it is an output format to format output for console. Same thing that what cli is doing in R, and what we get in knitr. What is required here is a ANSI Terminal Output parser (in a way) that would know how to convert for suitable HTML and other output.
So I don't think it could be useful as is.
Bug description
Colors or R warnings (see 1 in the figure) in Quarto are marked with symbols (see 2, 3). These symbols must not be present.
Steps to reproduce
/.../ Mantel-Haenszel Chi-squared: X-squared = 1.1306, df = 1, p-value = 0.2876
Warning message: Exp. counts < 5: Chi-squared approx. may be incorrect!!
Contingency Coeff. 0.287 Cramer's V 0.300 Kendall Tau-b 0.065
/.../
/.../ Mantel-Haenszel Chi-squared: X-squared = 1.1306, df = 1, p-value = 0.2876 [31m Warning message: Exp. counts < 5: Chi-squared approx. may be incorrect!!
[39m Contingency Coeff. 0.287 Cramer's V 0.300 Kendall Tau-b 0.065 /.../