Git Product home page Git Product logo

Comments (21)

cderv avatar cderv commented on June 2, 2024 4

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))
```
Will give this output

image

Hoping it will help others who want to preserve R terminal colouring.

from crayon.

cderv avatar cderv commented on June 2, 2024 1

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.

gaborcsardi avatar gaborcsardi commented on June 2, 2024 1

@phineas-pta craoyn does not do anything towards this, so I am afraid you'll need to ask at fansi or shiny.

from crayon.

gaborcsardi avatar gaborcsardi commented on June 2, 2024

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.

hadley avatar hadley commented on June 2, 2024

Yeah, that's on the todo list

from crayon.

gaborcsardi avatar gaborcsardi commented on June 2, 2024

@yihui Any idea how this could work? I guess some support is needed from pandoc as well? Does that exist?

from crayon.

yihui avatar yihui commented on June 2, 2024

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.

gaborcsardi avatar gaborcsardi commented on June 2, 2024

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.

gaborcsardi avatar gaborcsardi commented on June 2, 2024

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.

gaborcsardi avatar gaborcsardi commented on June 2, 2024

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.

yihui avatar yihui commented on June 2, 2024

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.

gaborcsardi avatar gaborcsardi commented on June 2, 2024

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.

yihui avatar yihui commented on June 2, 2024

"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.

gaborcsardi avatar gaborcsardi commented on June 2, 2024

It will be annoying if many people actually use this hook.

Well, the last thing I want is to annoy people. :)

from crayon.

wlandau-lilly avatar wlandau-lilly commented on June 2, 2024

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.

yihui avatar yihui commented on June 2, 2024

@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.

wlandau-lilly avatar wlandau-lilly commented on June 2, 2024

@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.

wlandau-lilly avatar wlandau-lilly commented on June 2, 2024

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.

wlandau-lilly avatar wlandau-lilly commented on June 2, 2024

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.

fmmattioni avatar fmmattioni commented on June 2, 2024

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.

phineas-pta avatar phineas-pta commented on June 2, 2024

Hello,

Does this feature work with shiny::verbatimTextOutput?

from crayon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.