Coder Social home page Coder Social logo

colored_table's Introduction

Colored table in README

library(tidyverse)

df_colors <- tibble(col = c("#4B4B1D", "#92CDEB", "#AE320A", "#B724D3"), 
                    viz = c(" ", "  ", "   ", "    "))

Expected output:

kableExtra

library(kableExtra)

df_colors %>%
  mutate(viz = cell_spec(viz, background = df_colors$col)) %>%
  kable(escape = FALSE)

col

viz

#4B4B1D

#92CDEB

#AE320A

#B724D3

df_colors %>%
  mutate(viz = cell_spec(viz, background = df_colors$col)) %>%
  kable() 

col

viz

#4B4B1D

<span style=" border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #4B4B1D !important;" > </span>

#92CDEB

<span style=" border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #92CDEB !important;" > </span>

#AE320A

<span style=" border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #AE320A !important;" > </span>

#B724D3

<span style=" border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #B724D3 !important;" > </span>

gt

library(gt)

df_colors %>% 
  gt() %>% 
  data_color(columns = vars(viz), colors = df_colors$col)
<style>html { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', 'Fira Sans', 'Droid Sans', Arial, sans-serif; } #fxvbklfcbf .gt_table { display: table; border-collapse: collapse; margin-left: auto; margin-right: auto; color: #000000; font-size: 16px; background-color: #FFFFFF; /* table.background.color */ width: auto; /* table.width */ border-top-style: solid; /* table.border.top.style */ border-top-width: 2px; /* table.border.top.width */ border-top-color: #A8A8A8; /* table.border.top.color */ } #fxvbklfcbf .gt_heading { background-color: #FFFFFF; /* heading.background.color */ border-bottom-color: #FFFFFF; } #fxvbklfcbf .gt_title { color: #000000; font-size: 125%; /* heading.title.font.size */ padding-top: 4px; /* heading.top.padding */ padding-bottom: 1px; border-bottom-color: #FFFFFF; border-bottom-width: 0; } #fxvbklfcbf .gt_subtitle { color: #000000; font-size: 85%; /* heading.subtitle.font.size */ padding-top: 1px; padding-bottom: 4px; /* heading.bottom.padding */ border-top-color: #FFFFFF; border-top-width: 0; } #fxvbklfcbf .gt_bottom_border { border-bottom-style: solid; /* heading.border.bottom.style */ border-bottom-width: 2px; /* heading.border.bottom.width */ border-bottom-color: #A8A8A8; /* heading.border.bottom.color */ } #fxvbklfcbf .gt_column_spanner { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #A8A8A8; padding-top: 4px; padding-bottom: 4px; } #fxvbklfcbf .gt_col_heading { color: #000000; background-color: #FFFFFF; /* column_labels.background.color */ font-size: 16px; /* column_labels.font.size */ font-weight: initial; /* column_labels.font.weight */ vertical-align: middle; padding: 10px; margin: 10px; } #fxvbklfcbf .gt_sep_right { border-right: 5px solid #FFFFFF; } #fxvbklfcbf .gt_group_heading { padding: 8px; color: #000000; background-color: #FFFFFF; /* row_group.background.color */ font-size: 16px; /* row_group.font.size */ font-weight: initial; /* row_group.font.weight */ border-top-style: solid; /* row_group.border.top.style */ border-top-width: 2px; /* row_group.border.top.width */ border-top-color: #A8A8A8; /* row_group.border.top.color */ border-bottom-style: solid; /* row_group.border.bottom.style */ border-bottom-width: 2px; /* row_group.border.bottom.width */ border-bottom-color: #A8A8A8; /* row_group.border.bottom.color */ vertical-align: middle; } #fxvbklfcbf .gt_empty_group_heading { padding: 0.5px; color: #000000; background-color: #FFFFFF; /* row_group.background.color */ font-size: 16px; /* row_group.font.size */ font-weight: initial; /* row_group.font.weight */ border-top-style: solid; /* row_group.border.top.style */ border-top-width: 2px; /* row_group.border.top.width */ border-top-color: #A8A8A8; /* row_group.border.top.color */ border-bottom-style: solid; /* row_group.border.bottom.style */ border-bottom-width: 2px; /* row_group.border.bottom.width */ border-bottom-color: #A8A8A8; /* row_group.border.bottom.color */ vertical-align: middle; } #fxvbklfcbf .gt_striped { background-color: #f2f2f2; } #fxvbklfcbf .gt_from_md > :first-child { margin-top: 0; } #fxvbklfcbf .gt_from_md > :last-child { margin-bottom: 0; } #fxvbklfcbf .gt_row { padding: 8px; /* row.padding */ margin: 10px; vertical-align: middle; } #fxvbklfcbf .gt_stub { border-right-style: solid; border-right-width: 2px; border-right-color: #A8A8A8; padding-left: 12px; } #fxvbklfcbf .gt_summary_row { color: #000000; background-color: #FFFFFF; /* summary_row.background.color */ padding: 8px; /* summary_row.padding */ text-transform: inherit; /* summary_row.text_transform */ } #fxvbklfcbf .gt_grand_summary_row { color: #000000; background-color: #FFFFFF; /* grand_summary_row.background.color */ padding: 8px; /* grand_summary_row.padding */ text-transform: inherit; /* grand_summary_row.text_transform */ } #fxvbklfcbf .gt_first_summary_row { border-top-style: solid; border-top-width: 2px; border-top-color: #A8A8A8; } #fxvbklfcbf .gt_first_grand_summary_row { border-top-style: double; border-top-width: 6px; border-top-color: #A8A8A8; } #fxvbklfcbf .gt_table_body { border-top-style: solid; /* table_body.border.top.style */ border-top-width: 2px; /* table_body.border.top.width */ border-top-color: #A8A8A8; /* table_body.border.top.color */ border-bottom-style: solid; /* table_body.border.bottom.style */ border-bottom-width: 2px; /* table_body.border.bottom.width */ border-bottom-color: #A8A8A8; /* table_body.border.bottom.color */ } #fxvbklfcbf .gt_footnote { font-size: 90%; /* footnote.font.size */ padding: 4px; /* footnote.padding */ } #fxvbklfcbf .gt_sourcenote { font-size: 90%; /* sourcenote.font.size */ padding: 4px; /* sourcenote.padding */ } #fxvbklfcbf .gt_center { text-align: center; } #fxvbklfcbf .gt_left { text-align: left; } #fxvbklfcbf .gt_right { text-align: right; font-variant-numeric: tabular-nums; } #fxvbklfcbf .gt_font_normal { font-weight: normal; } #fxvbklfcbf .gt_font_bold { font-weight: bold; } #fxvbklfcbf .gt_font_italic { font-style: italic; } #fxvbklfcbf .gt_super { font-size: 65%; } #fxvbklfcbf .gt_footnote_glyph { font-style: italic; font-size: 65%; } </style>

col

viz

#4B4B1D

#92CDEB

#AE320A

#B724D3

DT

library(DT)

df_colors %>%
  datatable() %>%
  formatStyle(columns = "viz", valueColumns = "col",
              backgroundColor = styleEqual(df_colors$col, df_colors$col))
## PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
<script type="application/json" data-for="htmlwidget-79d2082c07ebb3208dd7">{"x":{"filter":"none","data":[["1","2","3","4"],["#4B4B1D","#92CDEB","#AE320A","#B724D3"],[" "," "," "," "]],"container":"\n \n \n
<\/th>\n col<\/th>\n viz<\/th>\n <\/tr>\n <\/thead>\n<\/table>","options":{"order":[],"autoWidth":false,"orderClasses":false,"columnDefs":[{"orderable":false,"targets":0}],"rowCallback":"function(row, data) {\nvar value=data[1]; $(this.api().cell(row, 2).node()).css({'background-color':value == '#4B4B1D' ? '#4B4B1D' : value == '#92CDEB' ? '#92CDEB' : value == '#AE320A' ? '#AE320A' : value == '#B724D3' ? '#B724D3' : ''});\n}"}},"evals":["options.rowCallback"],"jsHooks":[]}</script>

colored_table's People

Contributors

abichat avatar

Watchers

 avatar

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.