rformattable

Setting bar size in formattable


I am wonder if its possible reverse the size of the color_bar command in formattable, example:

library(formattable)

df = data.frame(Ranking = c(1, 2, 3, 4, 5, 8, 10))

formattable(df, list(Ranking = color_bar("red")))

My table result is:

table

Is it possible get the largest bar in number 1 and the shortest bar in number 10? I have a Ranking column where the 1 number is the best ranking.

EDIT1

Other solution that work for me is omit or remove the number in the color_bar.

EDIT2:

Other question : How I could center the header?

My code:

formattable(df, align =c("c"), list(Ranking= color_bar("red")))

Solution

  • Try this solution:

    ```{r, include=FALSE, warning=FALSE}
    library(formattable)
    
    InvProp = function(x) (1.05 - ((x) / (max(x)))) #inverse proportion
    #make our formattable
    df = data.frame(" " = c(1, 2, 3, 4, 5, 8, 10, 15), check.names=FALSE)
    table2 <- formattable(df, list(" " = color_bar("red", InvProp)))
    ```
    
    #you can modify title style with html and css, as you wish
    <center>
    <caption>My formattable</caption>
    </center>
    `r table2`
    

    enter image description here


    An addition

    library(formattable)
    library(kableExtra)
    
    InvProp = function(x) (1.1 - ((x) / (max(x)))) #inverse proportion
    df = data.frame(names = c(1, 2, 3, 4, 5, 8, 10), check.names=FALSE)
    
    df %>% 
         mutate(
             "names" = color_bar("red", InvProp)(names)) %>%
        kable("html", escape = F, align = c("r"), col.names = NULL) %>%
        kable_styling("hover", full_width = F) %>%
        column_spec(1, width = "5cm", color = "white")
    

    After you can customize it as you wish. Good luck.

    enter image description here