### Run Shiny example from Gist Source: http://haozhu233.github.io/kableExtra/use_kable_in_shiny.html Executes a pre-built Shiny application example directly from a GitHub Gist. ```R shiny::runGist("https://gist.github.com/haozhu233/9e675e1a8a1bb4744f9ebc9246a2366b") ``` -------------------------------- ### Install kableExtra Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Commands to install the package from CRAN or the development version from GitHub. ```R install.packages("kableExtra") # For dev version # install.packages("devtools") devtools::install_github("haozhu233/kableExtra") ``` -------------------------------- ### Install kableExtra Image Dependencies Source: http://haozhu233.github.io/kableExtra/save_kable_and_as_image.html Install the 'magick' and 'webshot2' packages, which are required for kableExtra's image conversion features. Verify that magick::read_image() can properly read PDF files. ```r install.packages("magick") install.packages("webshot2") ``` -------------------------------- ### Create a scrollable table with kableExtra Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Use scroll_box to add scrolling functionality to a kable table. This example demonstrates setting a 100% width and a fixed height of 200px. ```r kbl(cbind(mtcars, mtcars)) %>% add_header_above(c("a" = 5, "b" = 18)) %>% kable_paper() %>% scroll_box(width = "100%", height = "200px") ``` -------------------------------- ### Row Specification for Styling Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Apply styling to specific rows using `row_spec`. This example bolds rows 3 through 5 and sets their text color to white with a red background. ```r kbl(dt) %>% kable_paper("striped", full_width = F) %>% column_spec(5:7, bold = T) %>% row_spec(3:5, bold = T, color = "white", background = "#D7261E") ``` -------------------------------- ### Save kable table to PDF and PNG Source: http://haozhu233.github.io/kableExtra/save_kable_and_as_image.html Use save_kable to save LaTeX or HTML tables to specified file formats like PDF or PNG. Ensure necessary packages like magick and webshot2 are installed. ```r library(kableExtra) kable(mtcars[1:5, ], "html") %>% kable_styling("striped") %>% row_spec(1, color = "red") %>% save_kable("inst/test.pdf") kable(mtcars, "latex") %>% kable_styling(latex_options = "striped") %>% save_kable("inst/test.png") ``` -------------------------------- ### Header Row Specification Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Format the header row by using `row_spec(row = 0, ...)`. This example rotates the header text by -45 degrees. ```r kbl(dt) %>% kable_paper("striped", full_width = F) %>% row_spec(0, angle = -45) ``` -------------------------------- ### Add In-table Footnote Markers Source: http://haozhu233.github.io/kableExtra/kableExtra_in_other_HTML_themes.html The `add_footnote()` function automatically transforms `[note]` within table captions or headers into footnote markers. This example shows adding markers to both the caption and a header. ```r kable(dt, caption = "Demo Table[note]") %>% kable_styling("striped") %>% add_header_above(c(" ", "Group 1[note]" = 3, "Group 2[note]" = 3)) %>% add_footnote(c("This table is from mtcars", "Group 1 contains mpg, cyl and disp", "Group 2 contains hp, drat and wt"), notation = "symbol") ``` -------------------------------- ### 应用多种 Bootstrap 样式 Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html_cn.html 通过 bootstrap_options 参数组合使用 striped、hover、condensed 和 responsive 等样式。 ```R kable(dt, "html") %>% kable_styling(bootstrap_options = c("striped", "hover")) ``` ```R kable(dt, "html") %>% kable_styling(bootstrap_options = c("striped", "hover", "condensed")) ``` ```R kable(dt, "html") %>% kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) ``` -------------------------------- ### Initialize Bootstrap tooltips Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html HTML snippet to enable Bootstrap tooltips on a page. ```HTML ``` -------------------------------- ### Initialize Data for Tables Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Load the library and prepare a subset of the mtcars dataset for table generation. ```R library(kableExtra) dt <- mtcars[1:5, 1:6] ``` -------------------------------- ### Configure Global Table Format Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Optional configuration for older versions of kableExtra to set the default output format. ```R # If you are using kableExtra < 0.9.0, you are recommended to set a global option first. # options(knitr.table.format = "html") ## If you don't define format here, you'll need put `format = "html"` in every kable function. ``` -------------------------------- ### Load kableExtra and Prepare Data Source: http://haozhu233.github.io/kableExtra/kableExtra_in_other_HTML_themes.html Loads the necessary libraries and prepares a subset of the mtcars dataset for table rendering. ```r library(knitr) library(kableExtra) dt <- mtcars[1:5, 1:6] ``` -------------------------------- ### Initialize Bootstrap popovers Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html HTML snippet to enable Bootstrap popovers on a page. ```HTML ``` -------------------------------- ### 应用基础 Bootstrap 样式 Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html_cn.html 使用 kable_styling 为表格添加默认的 Bootstrap 样式。 ```R dt %>% kable("html") %>% kable_styling() ``` -------------------------------- ### Specify output file path for as_image Source: http://haozhu233.github.io/kableExtra/save_kable_and_as_image.html When using as_image in environments like xaringan that require standalone HTML, specify the 'file' option with the desired path to save the image. ```r kable(mtcars, "html") %>% kable_styling("striped") %>% row_spec(1, color = "red") %>% as_image(file = "img/something.png") ``` -------------------------------- ### 安装 kableExtra 开发版 Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html_cn.html 使用 devtools 从 GitHub 安装最新开发版本。 ```R # install.packages("devtools") devtools::install_github("haozhu233/kableExtra") ``` -------------------------------- ### Create a Shiny application with kableExtra Source: http://haozhu233.github.io/kableExtra/use_kable_in_shiny.html Defines a UI with a slider input and a server function that renders a filtered, styled table using kableExtra. ```R library(shiny) ui <- fluidPage( # Application title titlePanel("mtcars"), sidebarLayout( sidebarPanel( sliderInput("mpg", "mpg Limit", min = 11, max = 33, value = 20) ), mainPanel( tableOutput("mtcars_kable") ) ) ) server <- function(input, output) { library(dplyr) library(kableExtra) output$mtcars_kable <- function() { req(input$mpg) mtcars %>% mutate(car = rownames(.)) %>% select(car, everything()) %>% filter(mpg <= input$mpg) %>% knitr::kable("html") %>% kable_styling("striped", full_width = F) %>% add_header_above(c(" ", "Group 1" = 5, "Group 2" = 6)) } } # Run the application shinyApp(ui = ui, server = server) ``` -------------------------------- ### Create Basic kable Table Source: http://haozhu233.github.io/kableExtra/using_kableExtra_in_radix.html Generates a simple kable table without any additional styling. This serves as a baseline for comparison. ```r kable(iris[1:6, ], caption = "Basic kable") ``` -------------------------------- ### Load Sparkline Dependencies Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Ensures the sparkline package dependencies are loaded. This is a preparatory step for using sparklines within KableExtra tables. ```R # Not evaluated library(sparkline) sparkline::sparkline(0) ``` -------------------------------- ### Apply Responsive Bootstrap Option Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Add the `responsive` option to enable horizontal scrolling on smaller screens. This ensures tables remain usable on devices with limited width. ```r kbl(dt) %>% kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) ``` -------------------------------- ### 设置全局表格格式 Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html_cn.html 通过 options 设置全局默认格式,避免在每个 kable 调用中重复定义。 ```R options(knitr.table.format = "html") ``` -------------------------------- ### Create Table with Sparklines using KableExtra Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Generates a data frame with sparkline charts for specified columns and displays it as an HTML table using kable() and kable_paper(). Set escape to FALSE to render HTML within the table. ```R spk_dt <- data.frame( var = c("mpg", "wt"), sparkline = c(sparkline::spk_chr(mtcars$mpg), sparkline::spk_chr(mtcars$wt)) ) kbl(spk_dt, escape = F) %>% kable_paper(full_width = F) ``` -------------------------------- ### Load Required Libraries Source: http://haozhu233.github.io/kableExtra/use_kableExtra_with_formattable.html Loads the necessary R packages for table manipulation and visualization. ```r library(knitr) library(kableExtra) library(formattable) library(dplyr) ``` -------------------------------- ### 生成基础 HTML 表格 Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html_cn.html 使用 kable 函数将数据框转换为 HTML 格式。 ```R library(kableExtra) dt <- mtcars[1:5, 1:6] kable(dt, "html") ``` -------------------------------- ### Render Basic HTML Table with kable Source: http://haozhu233.github.io/kableExtra/kableExtra_in_other_HTML_themes.html Renders a basic HTML table using the kable function. The appearance is influenced by the R Markdown theme. ```r kable(dt) ``` -------------------------------- ### Integrate kableExtra with formattable Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Combines kableExtra styling with formattable functions for advanced table visualization. ```R library(formattable) ft_dt <- mtcars[1:5, 1:4] ft_dt$car <- row.names(ft_dt) row.names(ft_dt) <- NULL ft_dt$mpg <- color_tile("white", "orange")(ft_dt$mpg) ft_dt$cyl <- cell_spec(ft_dt$cyl, angle = (1:5)*60, background = "red", color = "white", align = "center") ft_dt$disp <- ifelse( ft_dt$disp > 200, cell_spec(ft_dt$disp, color = "red", bold = T), cell_spec(ft_dt$disp, color = "green", italic = T) ) ft_dt$hp <- color_bar("lightgreen")(ft_dt$hp) ft_dt <- ft_dt[c("car", "mpg", "cyl", "disp", "hp")] kbl(ft_dt, escape = F) %>% kable_paper("hover", full_width = F) %>% column_spec(5, width = "3cm") %>% add_header_above(c(" ", "Hello" = 2, "World" = 2)) ``` -------------------------------- ### Convert xtable to kable and apply kableExtra formatting Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Use `xtable2kable()` to convert an xtable object to a kable object, then apply kableExtra formatting like `column_spec`. ```r # Not evaluating xtable::xtable(mtcars[1:4, 1:4], caption = "Hello xtable") %>% xtable2kable() %>% column_spec(1, color = "red") ``` -------------------------------- ### Create Formatted Table with kableExtra Source: http://haozhu233.github.io/kableExtra/use_kableExtra_with_formattable.html Applies formattable's color_tile and color_bar to numeric columns and uses kableExtra's cell_spec for conditional formatting and styling. Ensure escape = F is used in kable for HTML formatting. ```r mtcars[1:5, 1:4] %>% mutate( car = row.names(.), mpg = color_tile("white", "orange")(mpg), cyl = cell_spec(cyl, "html", angle = (1:5)*60, background = "red", color = "white", align = "center"), disp = ifelse(disp > 200, cell_spec(disp, "html", color = "red", bold = T), cell_spec(disp, "html", color = "green", italic = T)), hp = color_bar("lightgreen")(hp) ) %>% select(car, everything()) %>% kable("html", escape = F) %>% kable_styling("hover", full_width = F) %>% column_spec(5, width = "3cm") %>% add_header_above(c(" ", "Hello" = 2, "World" = 2)) ``` -------------------------------- ### Generate Basic HTML Table Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Creates a plain HTML table from a data frame without additional CSS styling. ```R kbl(dt) ``` -------------------------------- ### Apply Bootstrap Theme Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Uses kable_styling to apply a default Bootstrap theme to the HTML table. ```R dt %>% kbl() %>% kable_styling() ``` -------------------------------- ### Apply Striped and Hover Bootstrap Options Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Use `kable_styling` with `bootstrap_options` to add striped rows and highlight hovered rows. This is useful for improving table readability and user interaction. ```r kbl(dt) %>% kable_styling(bootstrap_options = c("striped", "hover")) ``` -------------------------------- ### Apply Alternative Table Themes Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Applies various built-in themes to customize the appearance of HTML tables. These functions support lightable_options like striped and hover. ```R dt %>% kbl() %>% kable_paper("hover", full_width = F) ``` ```R dt %>% kbl(caption = "Recreating booktabs style table") %>% kable_classic(full_width = F, html_font = "Cambria") ``` ```R dt %>% kbl() %>% kable_classic_2(full_width = F) ``` ```R dt %>% kbl() %>% kable_minimal() ``` ```R dt %>% kbl() %>% kable_material(c("striped", "hover")) ``` ```R dt %>% kbl() %>% kable_material_dark() ``` -------------------------------- ### Create a table with popover messages Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Uses cell_spec and spec_popover to add interactive popovers to table cells. ```R popover_dt <- data.frame( position = c("top", "bottom", "right", "left"), stringsAsFactors = FALSE ) popover_dt$`Hover over these items` <- cell_spec( paste("Message on", popover_dt$position), # Cell texts popover = spec_popover( content = popover_dt$position, title = NULL, # title will add a Title Panel on top position = popover_dt$position )) kbl(popover_dt, escape = FALSE) %>% kable_paper("striped", full_width = FALSE) ``` -------------------------------- ### Convert kable to XML and Modify Source: http://haozhu233.github.io/kableExtra/kableExtra_and_xml2.html Converts a kable table to an XML object, allows modification using xml2 functions, and then renders the modified XML back into a kable table. Requires kableExtra and xml2 libraries. ```R library(kableExtra) library(xml2) demo <- kable(mtcars[1:4, 1:4]) %>% kable_styling(full_width = F) %>% kable_as_xml() demo %>% xml_child(2) %>% xml_child(2) %>% xml_child(1) %>% xml_set_attr("class", "badIdea") xml_as_kable(demo) ``` -------------------------------- ### Apply Striped, Hover, and Condensed Bootstrap Options Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Combine multiple Bootstrap options, including `condensed` for shorter row heights, to customize table appearance. This is beneficial for space-saving designs. ```r kbl(dt) %>% kable_styling(bootstrap_options = c("striped", "hover", "condensed")) ``` -------------------------------- ### Apply column width and styling with column_spec Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Use column_spec to set specific column widths and apply bold text or background colors. Be cautious with performance on very large tables. ```R text_tbl <- data.frame( Items = c("Item 1", "Item 2", "Item 3"), Features = c( "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex. Morbi malesuada sagittis turpis, at venenatis nisl luctus a. ", "In eu urna at magna luctus rhoncus quis in nisl. Fusce in velit varius, posuere risus et, cursus augue. Duis eleifend aliquam ante, a aliquet ex tincidunt in. ", "Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat et sollicitudin venenatis. In ac lacus at velit scelerisque mattis. " ) ) kbl(text_tbl) %>% kable_paper(full_width = F) %>% column_spec(1, bold = T, border_right = T) %>% column_spec(2, width = "30em", background = "yellow") ``` -------------------------------- ### Visualize Data with Viridis Color Palettes Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Applies `spec_color`, `spec_font_size`, and `spec_angle` helpers to rescale continuous variables for visualization within table cells. `spec_color` maps variables to color palettes, and `spec_font_size` adjusts font size. ```R vs_dt <- iris[1:10, ] vs_dt[1:4] <- lapply(vs_dt[1:4], function(x) { cell_spec(x, bold = T, color = spec_color(x, end = 0.9), font_size = spec_font_size(x))}) vs_dt[5] <- cell_spec(vs_dt[[5]], color = "white", bold = T, background = spec_color(1:10, end = 0.9, option = "A", direction = -1)) kbl(vs_dt, escape = F, align = "c") %>% kable_classic("striped", full_width = F) ``` -------------------------------- ### Visualize Data with Viridis Color and dplyr Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Uses `dplyr`'s `mutate_if` to apply `cell_spec` with `spec_color` and `spec_font_size` to numeric columns, and styles the 'Species' column with a background color using `spec_color`. Requires `kable` and `kable_paper` for final table rendering. ```R iris[1:10, ] %>% mutate_if(is.numeric, function(x) { cell_spec(x, bold = T, color = spec_color(x, end = 0.9), font_size = spec_font_size(x))}) %>% mutate(Species = cell_spec( Species, color = "white", bold = T, background = spec_color(1:10, end = 0.9, option = "A", direction = -1) )) %>% kable(escape = F, align = "c") %>% kable_paper(c("striped", "condensed"), full_width = F) ``` -------------------------------- ### Positioning Tables Source: http://haozhu233.github.io/kableExtra/kableExtra_in_other_HTML_themes.html Align tables using the position argument in kable_styling. Use float options to wrap text around the table. ```R kable(dt) %>% kable_styling(full_width = F, position = "left") ``` ```R kable(dt) %>% kable_styling(full_width = F, position = "float_right") ``` -------------------------------- ### Set Global Table Format Option Source: http://haozhu233.github.io/kableExtra/kableExtra_in_other_HTML_themes.html Sets the global option for kable table format to HTML. This avoids needing to specify format = "html" in every kable function call. ```r options(knitr.table.format = "html") ## If you don't define format here, you'll need put `format = "html"` in every kable function. ``` -------------------------------- ### Create a scrollable table with kableExtra Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Use scroll_box to constrain the dimensions of an HTML table. Specify height for vertical scrolling, width for horizontal, or both for two-way scrolling. ```R kbl(cbind(mtcars, mtcars)) %>% kable_paper() %>% scroll_box(width = "500px", height = "200px") ``` -------------------------------- ### Format text with color and font size Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Uses text_spec to apply dynamic colors and font sizes to a vector of strings. ```R sometext <- strsplit(paste0( "You can even try to make some crazy things like this paragraph. ", "It may seem like a useless feature right now but it's so cool ", "and nobody can resist. ;)" ), " ")[[1]] text_formatted <- paste( text_spec(sometext, color = spec_color(1:length(sometext), end = 0.9), font_size = spec_font_size(1:length(sometext), begin = 5, end = 20)), collapse = " ") # To display the text, type `r text_formatted` outside of the chunk ``` -------------------------------- ### Convert LaTeX table to image in HTML document Source: http://haozhu233.github.io/kableExtra/save_kable_and_as_image.html Render a LaTeX table as an image within an HTML document using as_image. This is useful for cross-formatting support, especially when targeting formats like Word. ```r library(kableExtra) kable(mtcars, "latex", booktabs = T) %>% kable_styling(latex_options = c("striped", "scale_down")) %>% row_spec(1, color = "red") %>% as_image() ``` -------------------------------- ### Convert LaTeX table to image with specified width Source: http://haozhu233.github.io/kableExtra/save_kable_and_as_image.html Convert a LaTeX table to an image, specifying the desired width in inches. The image will be scaled accordingly. ```r kable(mtcars, "latex", booktabs = T) %>% kable_styling(latex_options = c("striped", "scale_down")) %>% row_spec(1, color = "red") %>% as_image(width = 8) ``` -------------------------------- ### Generate Paginated Table with rmarkdown Source: http://haozhu233.github.io/kableExtra/using_kableExtra_in_radix.html Utilizes the rmarkdown package to create a paginated HTML table, suitable for displaying large datasets interactively. ```r rmarkdown::paged_table(iris) ``` -------------------------------- ### Save HTML Table with KableExtra Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Saves an HTML table generated by kable() and kable_paper() to a file. Set self_contained to TRUE to embed CSS within the HTML file. ```R kbl(mtcars) %>% kable_paper() %>% save_kable(file = "table1.html", self_contained = T) ``` -------------------------------- ### Insert Images into Columns Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Use `column_spec` with the `image` argument to add images to table columns. Ensure the image file is accessible. ```r tbl_img <- data.frame( name = c("kableExtra 1", "kableExtra 2"), logo = "" ) tbl_img %>% kbl(booktabs = T) %>% kable_paper(full_width = F) %>% column_spec(2, image = "kableExtra_sm.png") ``` -------------------------------- ### Style kable Table with kableExtra in Radix Source: http://haozhu233.github.io/kableExtra/using_kableExtra_in_radix.html Use this snippet to apply various styling options like striped, hover, and condensed rows, along with header and column specifications, to a kable table in Radix. Ensure kableExtra.html.bsTable is TRUE (default in v1.1.0+). ```r library(kableExtra) kable(iris[1:6, ], caption = "kable with kableExtra") %>% kable_styling(c("striped", "hover", "condensed"), full_width = F, position = "left") %>% add_header_above(c("numerical" = 4, "categorical" = 1)) %>% column_spec(1, bold = T) %>% row_spec(0, italic = T) ``` -------------------------------- ### Apply row indentation Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Use add_indent to list sub-groups under a total row without inserting a new labeling row. ```R kbl(dt) %>% kable_paper("striped", full_width = F) %>% add_indent(c(1, 3, 5)) ``` -------------------------------- ### Specify Image Size in Columns Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Control image dimensions within table columns by using `spec_image` with the `image` argument in `column_spec`. Specify width and height in pixels. ```r tbl_img %>% kbl(booktabs = T) %>% kable_paper(full_width = F) %>% column_spec(2, image = spec_image( c("kableExtra_sm.png", "kableExtra_sm.png"), 50, 50)) ``` -------------------------------- ### Add Number Footnotes to a Table Source: http://haozhu233.github.io/kableExtra/kableExtra_in_other_HTML_themes.html Use the 'number' notation for footnotes marked with sequential numbers. This is useful for standard numbered references. ```r kable(dt) %>% kable_styling("striped") %>% add_footnote(c("Footnote 1", "Have a good day."), notation = "number") ``` -------------------------------- ### Convert HTML table to image with specified width Source: http://haozhu233.github.io/kableExtra/save_kable_and_as_image.html Convert an HTML table to an image, specifying the desired width in inches. This allows embedding bootstrap-flavored tables as images in documents like Word or PDF. ```r kable(mtcars, "html") %>% kable_styling("striped") %>% row_spec(1, color = "red") %>% as_image(width = 4) ``` -------------------------------- ### Group Rows with Custom CSS Source: http://haozhu233.github.io/kableExtra/kableExtra_in_other_HTML_themes.html Customize the appearance of group label rows using the label_row_css argument in pack_rows(). This allows for custom background colors and text colors. ```R kable(dt) %>/ kable_styling("striped", full_width = F) %>/ pack_rows("Group 1", 3, 5, label_row_css = "background-color: #666; color: #fff;") ``` -------------------------------- ### CSS for Animated Table Cells Source: http://haozhu233.github.io/kableExtra/kableExtra_and_xml2.html Provides CSS keyframes and a class definition for creating a rotating animation on HTML table cells. This CSS should be included in R Markdown documents to apply the effect. ```css @-webkit-keyframes rotate { 0% {transform: rotate(0deg)} 100% {transform: rotate(360deg)} } .badIdea { position:relative; animation: rotate 3s linear infinite; } ``` -------------------------------- ### Add Indentation with add_indent() Source: http://haozhu233.github.io/kableExtra/kableExtra_in_other_HTML_themes.html Use add_indent() to create hierarchical row structures by indenting specific rows. This is useful for sub-grouping without creating explicit label rows. ```R kable(dt) %>/ kable_styling("striped", full_width = F) %>/ add_indent(c(1, 3, 5)) ``` -------------------------------- ### Adding Extra Header Rows Source: http://haozhu233.github.io/kableExtra/kableExtra_in_other_HTML_themes.html Use add_header_above to create grouped headers. Multiple header rows can be stacked by piping additional calls. ```R kable(dt) %>% kable_styling("striped") %>% add_header_above(c(" " = 1, "Group 1" = 2, "Group 2" = 2, "Group 3" = 2)) ``` ```R kable(dt) %>% kable_styling(c("striped", "bordered")) %>% add_header_above(c(" ", "Group 1" = 2, "Group 2" = 2, "Group 3" = 2)) %>% add_header_above(c(" ", "Group 4" = 4, "Group 5" = 2)) %>% add_header_above(c(" ", "Group 6" = 6)) ``` -------------------------------- ### Render Table with Full Width Disabled Source: http://haozhu233.github.io/kableExtra/kableExtra_in_other_HTML_themes.html Renders an HTML table using kable and kable_styling, with the full_width option set to FALSE to prevent the table from taking the full page width. ```r kable(dt) %>% kable_styling(full_width = F) ``` -------------------------------- ### Add Symbol Footnotes to a Table Source: http://haozhu233.github.io/kableExtra/kableExtra_in_other_HTML_themes.html Use the 'symbol' notation for footnotes marked with common symbols. This provides a distinct visual style for footnotes. ```r kable(dt) %>% kable_styling("striped") %>% add_footnote(c("Footnote 1", "Footnote 2", "Footnote 3"), notation = "symbol") ``` -------------------------------- ### Specify Column Styles with column_spec() Source: http://haozhu233.github.io/kableExtra/kableExtra_in_other_HTML_themes.html Customize column appearance using column_spec(). This function allows for setting column width and applying styles like bold text to specific columns. ```R text_tbl <- data.frame( Items = c("Item 1", "Item 2", "Item 3"), Features = c( "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex. Morbi malesuada sagittis turpis, at venenatis nisl luctus a. ", "In eu urna at magna luctus rhoncus quis in nisl. Fusce in velit varius, posuere risus et, cursus augue. Duis eleifend aliquam ante, a aliquet ex tincidunt in. ", "Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat et sollicitudin venenatis. In ac lacus at velit scelerisque mattis. " ) ) kable(text_tbl) %>/ kable_styling(full_width = F) %>/ column_spec(1, bold = T) %>/ column_spec(2, width = "30em") ``` -------------------------------- ### Customize Footnote Titles and Formatting Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Customize footnote titles and their formatting (italic, underline) using `*_title` arguments and `title_format`. Display footnotes as chunks instead of lists with `footnote_as_chunk = T`. ```R kbl(dt, align = "c") %>% kable_paper(full_width = F) %>% footnote(general = "Here is a general comments of the table. ", number = c("Footnote 1; ", "Footnote 2; "), alphabet = c("Footnote A; ", "Footnote B; "), symbol = c("Footnote Symbol 1; ", "Footnote Symbol 2"), general_title = "General: ", number_title = "Type I: ", alphabet_title = "Type II: ", symbol_title = "Type III: ", footnote_as_chunk = T, title_format = c("italic", "underline") ) ``` -------------------------------- ### Control Table Width with Full_width = FALSE Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Use `kable_paper` with `full_width = FALSE` to prevent tables from taking up the full page width. This is useful for smaller tables in R Markdown documents where a page-wide table might look unappealing. ```r kbl(dt) %>% kable_paper(bootstrap_options = "striped", full_width = F) ``` -------------------------------- ### Format Iris Dataset with cell_spec and spec_color Source: http://haozhu233.github.io/kableExtra/use_kableExtra_with_formattable.html Applies conditional formatting to the iris dataset using cell_spec for species and mutate_if for numeric columns, leveraging spec_color for color generation. The output format is HTML. ```r iris[1:10, ] %>% mutate( Species = cell_spec(Species, color = spec_color(1:10, option = "A"), link = "#", tooltip = paste0("Sepal Length: ", Sepal.Length)) ) %>% mutate_if(is.numeric, function(x){ cell_spec(x, "html", color = spec_color(x), font_size = spec_font_size(x), bold = T) }) %>% kable("html", escape = F, align = "c") %>% kable_styling("condensed", full_width = F) ``` -------------------------------- ### Apply Conditional Formatting with dplyr Pipe Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Demonstrates a full `dplyr` pipe for applying `cell_spec` for conditional formatting, including text color, alignment, angle, and background color. This approach chains operations seamlessly. ```R mtcars[1:10, 1:2] %>% mutate( car = row.names(.), mpg = cell_spec(mpg, "html", color = ifelse(mpg > 20, "red", "blue")), cyl = cell_spec(cyl, "html", color = "white", align = "c", angle = 45, background = factor(cyl, c(4, 6, 8), c("#666666", "#999999", "#BBBBBB"))) ) %>% select(car, mpg, cyl) %>% kbl(format = "html", escape = F) %>% kable_paper("striped", full_width = F) ``` -------------------------------- ### Adjusting Font Size Source: http://haozhu233.github.io/kableExtra/kableExtra_in_other_HTML_themes.html Apply a specific font size to a table using the font_size option. ```R kable(dt) %>% kable_styling(font_size = 7) ``` -------------------------------- ### Add Table Footnotes with Multiple Notation Systems Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Use the `footnote` function to add general, number, alphabet, and symbol footnotes to a kable table. The `general` type has no label, while others use corresponding marks. This fulfills APA table footnote requirements. ```R kbl(dt, align = "c") %>% kable_classic(full_width = F) %>% footnote(general = "Here is a general comments of the table. ", number = c("Footnote 1; ", "Footnote 2; "), alphabet = c("Footnote A; ", "Footnote B; "), symbol = c("Footnote Symbol 1; ", "Footnote Symbol 2") ) ``` -------------------------------- ### Align Table Position Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Sets the horizontal alignment of a table when full_width is disabled. ```R kbl(dt) %>% kable_styling(bootstrap_options = "striped", full_width = F, position = "left") ``` -------------------------------- ### Add header rows to group columns Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Use add_header_above to create multi-row headers. The input is a named character vector where names are labels and values are column spans. ```R kbl(dt) %>% kable_classic() %>% add_header_above(c(" " = 1, "Group 1" = 2, "Group 2" = 2, "Group 3" = 2)) ``` ```R kbl(dt) %>% kable_paper() %>% add_header_above(c(" ", "Group 1" = 2, "Group 2" = 2, "Group 3" = 2)) %>% add_header_above(c(" ", "Group 4" = 4, "Group 5" = 2)) %>% add_header_above(c(" ", "Group 6" = 6)) ``` -------------------------------- ### Apply conditional formatting with vectorized column_spec Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Leverage vectorized arguments in column_spec to apply dynamic styling based on data values, such as color scales or popovers. ```R mtcars[1:8, 1:8] %>% kbl() %>% kable_paper(full_width = F) %>% column_spec(2, color = spec_color(mtcars$mpg[1:8]), link = "https://haozhu233.github.io/kableExtra/") %>% column_spec(6, color = "white", background = spec_color(mtcars$drat[1:8], end = 0.7), popover = paste("am:", mtcars$am[1:8])) ``` -------------------------------- ### Enable Fixed Table Header Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Fixes the header row at the top of the table during scrolling. ```R kbl(mtcars[1:10, 1:5]) %>% kable_styling(fixed_thead = T) ``` -------------------------------- ### Wrap Text Around Table Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Uses float options to allow text to wrap around the table. ```R kbl(dt) %>% kable_styling(bootstrap_options = "striped", full_width = F, position = "float_right") ``` -------------------------------- ### Group Rows with pack_rows() Source: http://haozhu233.github.io/kableExtra/kableExtra_in_other_HTML_themes.html Use pack_rows() to group consecutive rows in a kable table. Specify the group label and the start/end row numbers from the original data frame. ```R kable(mtcars[1:10, 1:6], caption = "Group Rows") %>/ kable_styling("striped", full_width = F) %>/ pack_rows("Group 1", 4, 7) %>/ pack_rows("Group 2", 8, 10) ``` -------------------------------- ### Add Alphabet Footnotes to a Table Source: http://haozhu233.github.io/kableExtra/kableExtra_in_other_HTML_themes.html Use the 'alphabet' notation to add footnotes marked with letters. Ensure your footnote text matches the number of footnotes provided. ```r kable(dt) %>% kable_styling("striped") %>% add_footnote(c("Footnote 1", "Have a good day."), notation = "alphabet") ``` -------------------------------- ### Resolve pack_rows conflicts Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Use this configuration to resolve namespace conflicts between kableExtra and dplyr when using pack_rows. ```R conflicted::conflict_prefer("group_rows", "kableExtra", "dplyr") ``` -------------------------------- ### Group rows via labeling Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Use pack_rows to group specific rows together. Row indices refer to the original dataframe, excluding header or other group label rows. ```R kbl(mtcars[1:10, 1:6], caption = "Group Rows") %>% kable_paper("striped", full_width = F) %>% pack_rows("Group 1", 4, 7) %>% pack_rows("Group 2", 8, 10) ``` ```R # Not evaluated. This example generates the same table as above. kbl(mtcars[1:10, 1:6], caption = "Group Rows") %>% kable_paper("striped", full_width = F) %>% pack_rows(index = c(" " = 3, "Group 1" = 4, "Group 2" = 3)) ``` ```R kbl(dt) %>% kable_paper("striped", full_width = F) %>% pack_rows("Group 1", 3, 5, label_row_css = "background-color: #666; color: #fff;") ``` -------------------------------- ### Manually Add Footnote Marks with escape = F Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Manually add footnote marks to table elements using `footnote_marker_***()` functions. Ensure `escape = F` is set in the `kable` function to prevent special character escaping when using manual marks. ```R dt_footnote <- dt names(dt_footnote)[2] <- paste0(names(dt_footnote)[2], footnote_marker_symbol(1)) row.names(dt_footnote)[4] <- paste0(row.names(dt_footnote)[4], footnote_marker_alphabet(1)) kbl(dt_footnote, align = "c", # Remember this escape = F escape = F) %>% kable_paper(full_width = F) %>% footnote(alphabet = "Footnote A; ", symbol = "Footnote Symbol 1; ", alphabet_title = "Type II: ", symbol_title = "Type III: ", footnote_as_chunk = T) ``` -------------------------------- ### Point Range Plot for Forest Plots Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Utilize `spec_pointrange` for creating point-and-range visualizations, commonly used in regression tables for coefficients and confidence intervals. The `vline = 0` argument adds a vertical line at zero. ```r coef_table <- data.frame( Variables = c("var 1", "var 2", "var 3"), Coefficients = c(1.6, 0.2, -2.0), Conf.Lower = c(1.3, -0.4, -2.5), Conf.Higher = c(1.9, 0.6, -1.4) ) data.frame( Variable = coef_table$Variables, Visualization = "" ) %>% kbl(booktabs = T) %>% kable_classic(full_width = FALSE) %>% column_spec(2, image = spec_pointrange( x = coef_table$Coefficients, xmin = coef_table$Conf.Lower, xmax = coef_table$Conf.Higher, vline = 0) ) ``` -------------------------------- ### Adjust Table Font Size Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Reduces the font size of a specific table. ```R kbl(dt) %>% kable_styling(bootstrap_options = "striped", font_size = 7) ``` -------------------------------- ### Collapse repeating rows Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Use collapse_rows to merge repeating cells in columns into multi-row cells for multi-layer structural information. ```R collapse_rows_dt <- data.frame(C1 = c(rep("a", 10), rep("b", 5)), C2 = c(rep("c", 7), rep("d", 3), rep("c", 2), rep("d", 3)), C3 = 1:15, C4 = sample(c(0,1), 15, replace = TRUE)) kbl(collapse_rows_dt, align = "c") %>% kable_paper(full_width = F) %>% column_spec(1, bold = T) %>% collapse_rows(columns = 1:2, valign = "top") ``` -------------------------------- ### Inline Boxplot in Column Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Generate an inline boxplot for a column using `spec_boxplot`. The `mpg_list` is split by cylinder count to create separate boxplots. ```r mpg_list <- split(mtcars$mpg, mtcars$cyl) disp_list <- split(mtcars$disp, mtcars$cyl) inline_plot <- data.frame(cyl = c(4, 6, 8), mpg_box = "", mpg_hist = "", mpg_line1 = "", mpg_line2 = "", mpg_points1 = "", mpg_points2 = "", mpg_poly = "") inline_plot %>% kbl(booktabs = TRUE) %>% kable_paper(full_width = FALSE) %>% column_spec(2, image = spec_boxplot(mpg_list)) %>% column_spec(3, image = spec_hist(mpg_list)) %>% column_spec(4, image = spec_plot(mpg_list, same_lim = TRUE)) %>% column_spec(5, image = spec_plot(mpg_list, same_lim = FALSE)) %>% column_spec(6, image = spec_plot(mpg_list, type = "p")) %>% column_spec(7, image = spec_plot(mpg_list, disp_list, type = "p")) %>% column_spec(8, image = spec_plot(mpg_list, polymin = 5)) ``` -------------------------------- ### Apply Conditional Formatting to Cells Source: http://haozhu233.github.io/kableExtra/awesome_table_in_html.html Use `cell_spec` within `dplyr::mutate` to apply conditional formatting based on column values. Ensure `escape = FALSE` is set in `kbl` when using `cell_spec`. ```R cs_dt <- mtcars[1:10, 1:2] cs_dt$car = row.names(cs_dt) row.names(cs_dt) <- NULL cs_dt$mpg = cell_spec(cs_dt$mpg, color = ifelse(cs_dt$mpg > 20, "red", "blue")) cs_dt$cyl = cell_spec( cs_dt$cyl, color = "white", align = "c", angle = 45, background = factor(cs_dt$cyl, c(4, 6, 8), c("#666666", "#999999", "#BBBBBB"))) cs_dt <- cs_dt[c("car", "mpg", "cyl")] kbl(cs_dt, escape = F) %>% kable_paper("striped", full_width = F) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.