colortable icon indicating copy to clipboard operation
colortable copied to clipboard

color print doesn't work for kable in rmd output

Open mikejiang opened this issue 4 years ago • 11 comments

I used set_styling to add color to tibble columns, it works in console, image

but not in knitr report image

mikejiang avatar Mar 23 '20 01:03 mikejiang

Hi Mike, thanks for the issue! Is this printing to HTML or PDF? Right now those are the only output types supported.

thebioengineer avatar Mar 23 '20 01:03 thebioengineer

As shown, it is part of knitr Rmarkdown output, which is html

mikejiang avatar Mar 23 '20 03:03 mikejiang

Hi Mike, Could you try to make an minimal reproducible example? I am having some difficulty reproducing what that screen grab could be and the version of {colortable} from the master branch and doesnt seem to have an issue: image

thebioengineer avatar Mar 23 '20 04:03 thebioengineer

Below is the source of my Rmd file (click 'edit' to view the source)


output: html_document

knitr::opts_chunk$set(echo = TRUE)
library(colortable)
library(dplyr)
head(cars) %>% mutate(speed = set_styling(speed, text_color = "green"))

Here is my environment

> packageVersion("colortable")
[1] ‘0.1.1’
> packageVersion("dplyr")
[1] ‘0.8.5’
> packageVersion("knitr")
[1] ‘1.28’
> packageVersion("rmarkdown")
[1] ‘2.1’
> version
               _                                                 
platform       x86_64-pc-linux-gnu                               
arch           x86_64                                            
os             linux-gnu                                         
system         x86_64, linux-gnu                                 
status         Under development (unstable)                      
major          4                                                 
minor          0.0                                               
year           2020                                              
month          02                                                
day            08                                                
svn rev        77786                                             
language       R                                                 
version.string R Under development (unstable) (2020-02-08 r77786)
nickname       Unsuffered Consequences  

This gives me html output after compiling into html

##   speed dist
## 1     <span style='color:green;'>4</span>     2
## 2     <span style='color:green;'>4</span>   10
## 3     <span style='color:green;'>7</span>     4
## 4     <span style='color:green;'>7</span>   22
## 5     <span style='color:green;'>8</span>   16
## 6     <span style='color:green;'>9</span>   10

console output is fine, but Rmarkdown inline output is not correct either

image

mikejiang avatar Mar 23 '20 16:03 mikejiang

I see. Thank you for getting that to me. That is because I have not yet implemented a kable printing method for tibbles with color_vctrs. That is in the todo list. for the time being, you can get around that by explicitly printing with a kable:

image

As for printing inline, I need to figure out how to get that to work dynamically. One work around for now is to format the data.frame, setting the method to "html" and then print via kable:

image

thebioengineer avatar Mar 23 '20 17:03 thebioengineer

format(method="html") is useful, which is worthy of more docs (especially in the readme vignette of the repo)

But it seems still not work properly with tibble? image

mikejiang avatar Mar 23 '20 19:03 mikejiang

I'll take a closer look, like I said, I had not really tested inline outputs and need to figure out their methods to resolve this. Let me know if you have any suggestions!

thebioengineer avatar Mar 23 '20 20:03 thebioengineer

Sure. We've found your package greatly reduces the efforts to add color to tibble and port it to both console and html output. It is an awesome and promising work!

mikejiang avatar Mar 23 '20 20:03 mikejiang

Good to hear. This is still early days for the package and am figuring out its behavior. There is going to be a big jump to the vctrs package once dplyr 1.0.0 is fully out

thebioengineer avatar Mar 23 '20 20:03 thebioengineer

@mikejiang - I think I found a half solution where you don't have to use format(method = "html") while I work on implementing a proper printing method/behavior for tibble printing. try installing from the current master and try running the following code in a chunk to print inline:

library(tidyverse)`
library(colortable)
library(knitr)

mtcars %>% 
  as_tibble() %>% # should work with both normal data.frames and tibbles
  mutate(
    mpg = set_styling(mpg, mpg > 20, text_color = "yellow")
  )

thebioengineer avatar Mar 28 '20 06:03 thebioengineer

Another note - I looked more into why sometimes styling was applied w/o the format call before the kable function (like in the readme).

Turns out that kable uses as.matrix to do the type conversion of the non numeric fields to a character. The numeric fields, however, do get explicitly formatted - which then calls the formatting from colortable and everyone is happy.

Long story short, I have a PR open on knitr proposing applying formatting to all records, not just the numeric ones.

thebioengineer avatar Apr 03 '20 01:04 thebioengineer