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

Conditional colouring of values in specific columns #644

Open
OskarGauffin opened this issue Apr 8, 2023 · 2 comments
Open

Conditional colouring of values in specific columns #644

OskarGauffin opened this issue Apr 8, 2023 · 2 comments

Comments

@OskarGauffin
Copy link

OskarGauffin commented Apr 8, 2023

Dear @krlmlr or anyone else who knows,

I've been trying to modify tibble printing of the red colour of negative values to a green colour of three specific columns when column values exceed 1, for my specific tibble-extended class

As I understand it, this would imply a modification of the "body". Sadly, I couldn't really understand how to proceed examining the body-example provided in the vignette: https://cran.r-project.org/web/packages/pillar/vignettes/extending.html

My closest attempt was very ad-hoc, with a strsplit on whitespace in setup$body, to find the right column and manually add a glue_col on the specific columns. But splitting on whitespace does not work well, some rows have extra whitespace in the beginning of the row, shifting the column that receives the color.

I imagine a more proper solution would operate earlier in the code, somewhere where the body is created. Do you have any pointers, or could you add an additional example on how to modify the body with conditional colouring of values above say 1, in one or two columns?

@krlmlr
Copy link
Member

krlmlr commented Apr 19, 2023

Thanks, sorry for being slow. Would you like to share your attempts? What does your code look like, what doesn't work?

@OskarGauffin
Copy link
Author

OskarGauffin commented Apr 20, 2023

Edit: I just noticed that you have quite a bit of docs that I had not read. Feel free to close this, I'll open a new issue if I have more specific questions later on, sorry for bothering you prematurely.


Thanks for the answer,

Here's some code, using capture.output and gluing in some red where it's wanted. Some NAs that can be fixed, but for instance, the indentation of columns breaks down with this approach.

The essence is that I'd like to use the very nice printing that regular tibbles have, but add some colour to some places in it, similar to the red negative values. From the docs, I imagine there might be a way through modifying the body related functions in pillar, and was hoping for some helpful pointers on which function to modify, or even better, if an example in this general direction could be added in the vignette.

mtcars |> 
  tibble::as_tibble() |> 
  # Capture the default tibble printing
  (\(x){utils::capture.output(print(x))})() |>  
  # Split by row
  (\(x){split(x, f = 1:length(x))})() |>  
  purrr::walk(\(x){
    # Split by ws
    row_as_vector <- purrr::list_c(strsplit(x, " +")) 
    # Take the hp column
    hp_value <- suppressWarnings(as.numeric(row_as_vector[6])) 
    # Colour red when hp is above 100
    if(is.numeric(hp_value)){ 
      row_as_vector[6] <-  ifelse(100 < as.numeric(hp_value), paste0("{red ", hp_value, "}"), hp_value)
    }
    # Print with glue_col 
    print(glue::glue_col(paste0(row_as_vector, collapse=" ")))
  })

capture

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants