Comments (21)
While trying to understand what is going on in yihui/knitr#1805, here is an update regarding opting-in for coloured html output from crayon using fansi
📦
The hook thing described above has not been included in crayon directly, but it has been in fansi
with a special set_knit_hooks
function.
This is useful for preserving tidyverse coloring in output, errors and others.
To render with rmarkdown::render
---
title: "MWE"
output: html_document
---
Required setup
```{r color, echo = FALSE, results='asis'}
# crayon needs to be explicitly activated in Rmd
options(crayon.enabled = TRUE)
# Hooks needs to be set to deal with outputs
# thanks to fansi logic
old_hooks <- fansi::set_knit_hooks(knitr::knit_hooks,
which = c("output", "message", "error"))
```
Enabling coloring message using crayon in Rmd
```{r, message= TRUE}
message(crayon::make_style("green")("My green message."))
```
Keeping color in tidyverse error as rlang use color
```{r, error = TRUE}
rlang::abort(rlang:::format_error_bullets(c(x = "dplyr-like error")), .subclass = "foobar")
```
Keeping the red values in tibble output
```{r}
tibble::tibble(x = c(10, -10))
```
Hoping it will help others who want to preserve R terminal colouring.
from crayon.
Thanks @fmmattioni !
I think this question has nothing to do with the current topic of this closed issue. Please, could you ask this question in RStudio Community ? It would be a better place to have a discussion there and I could give you an answer there. Thank you very much !
from crayon.
@phineas-pta craoyn does not do anything towards this, so I am afraid you'll need to ask at fansi or shiny.
from crayon.
Yeah, I thought about this, too, and will be happy to add whatever is needed.
Btw. unrelatedly, color in RStudio would be great, too. I know it is not easy....
from crayon.
Yeah, that's on the todo list
from crayon.
@yihui Any idea how this could work? I guess some support is needed from pandoc as well? Does that exist?
from crayon.
I don't have a clear idea about this. Maybe it is the responsibility of evaluate (around here https://github.com/hadley/evaluate/blob/master/R/watcher.r#L32) to generate HTML spans, or maybe one can use the knitr output
hook (http://yihui.name/knitr/hooks/) to post-process the text output from chunks. Actually I'm not even sure if the ANSI escape codes can be preserved in the evaluate output at all (if they cannot, there is no way to move forward).
from crayon.
It does not have to be ANSI, actually. crayon
is adding the ANSI sequences, and it could just detect that it is inside of knitr and add whatever markup is needed instead. E.g. attach some attributes to the output.
But I think that even just passing the ANSI seqs to evaluate / knitr could work. An ANSI colored output is just a regular string, with some strange-looking markup in it, so the output
hook is worth a try. I'll take a look in a sec.
from crayon.
OK, I tried it. The ANSI markup goes through, so I just need an output hook that interprets it, and converts it to HTML. \o/
from crayon.
So, it works very well IMO. Here is an example:
https://gist.github.com/gaborcsardi/1fafd4f6c795f00989b8#file-test-rmd
You can see the output here:
https://htmlpreview.github.io/?https://gist.githubusercontent.com/gaborcsardi/1fafd4f6c795f00989b8/raw/11f132b17ba317cd4ee2408a418c400353561a3d/test.html
What would be the best way to ship this? A function in the crayon
package that sets up the knitr output hooks? Or you want to put this within knitr? Or a third package?
I think you could even turn this on by default in the default html hooks.
Unfortunately AFAIK there is no way to color a markdown document, at least not with GFM. :(
from crayon.
Maybe a function in crayon like hook_output_crayon()
so that users can opt-in by setting the output
hook to this function? You can condition the behavior on a chunk option, say, crayon = TRUE
(i.e. do gsub()
only when isTRUE(options$crayon)
).
from crayon.
OK, I can do that. I would turn it on by default, though. I mean, once the hooks are set, it is on, and you can turn it off with a chunk option. It would be very annoying to turn it on manually all the time, and (for HTML output), it is harmless.
Btw. this approach is not very good if you have both markdown and html output, because the markdown will have the ANSI seqs, but I guess I can just add a markdown output hook to remove them.
from crayon.
"Turning it on manually all the time" will be annoying if many people actually use this hook. I'm not sure this will be so popular that I need to support it "officially" (which means I'll introduce a Suggests dependency in knitr, and perhaps spend some time on maintaining this hook in the future). I like the colored terminal output, but personally I'm not very interested in colored output in HTML, so my current decision is if you want this feature, you opt in by setting the output hook.
from crayon.
It will be annoying if many people actually use this hook.
Well, the last thing I want is to annoy people. :)
from crayon.
Are there any new thoughts on this issue? I am not sure I understand why such a hook would be annoying. I would like to use it for my own reports, but I cannot view the gist. I also posted here.
from crayon.
@wlandau-lilly If you are asking me, the answer is no. I'm not interested in this feature, but knitr is open --- you don't really need my specific support. You should be able to opt in by yourself.
from crayon.
@yihui I do not care where or how it is implemented, and I agree that knitr
is probably not the right place. To be honest, right now I am just looking for a temporary solution. I do not know how to create such a hook myself.
from crayon.
I would love to have functions in crayon
to convert coloring between ANSI and HTML. Then, it would be easy to write a knitr
output hook, and the usefulness would extend beyond knitr
.
from crayon.
This seems to work:
---
title: "MWE"
output: html_document
---
```{r color, echo = FALSE}
options(crayon.enabled = TRUE)
knitr::knit_hooks$set(message = function(x, options){
paste0(
"<pre class=\"r-output\"><code>",
ansistrings::ansi_to_html(text = x, fullpage = FALSE),
"</code></pre>"
)
})
message(crayon::make_style("green")("My green message."))
```
Markdown output:
---
title: "MWE"
output: html_document
---
<pre class="r-output"><code>
## <span style="color:#4e9a06">My green message.</span>
</code></pre>
I look forward to the release of ansistrings.
from crayon.
That is a really nice solution that @cderv provided! Thank you!
One question, though: this makes the outputs to be in separate boxes.. for example, in the last example given, I would like to have both the call tibble::tibble(x = c(10, -10))
and its output in the same box. Is this possible somehow?
from crayon.
Hello,
Does this feature work with shiny::verbatimTextOutput
?
from crayon.
Related Issues (20)
- Detection of color-capable output broken inside an RStudio Job run as a script HOT 1
- win10_build: argument is of length 0 HOT 1
- Backport palette support from cli
- Move `master` branch to `main` HOT 1
- Ns in DNAstringset greys out all text in console that follows HOT 2
- Force all warnings to use crayon? HOT 3
- Release crayon 1.4.2
- red$bold(X) results in : Error in gsub: input string 1 is invalid UTF-8 HOT 3
- Color detection Github Actions HOT 5
- Does not install on R 3.4 and R 3.5, on Windows HOT 1
- Release crayon 1.5.0
- Potential NAMESPACE/import issue? HOT 1
- Release crayon 1.5.1
- Partial argument name: read.table(..., comment = ";")
- crayon doesn't mark encoding on UTF-8 strings in some cases HOT 3
- num_ansi_colors ignores stream parameter HOT 1
- strip_style() returns garbled output in non-UTF-8 locale when UTF-8 characters are present HOT 2
- Release crayon 1.5.2
- Upkeep for crayon (2023)
- crayon update from CRAN
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from crayon.