bug fix
diff --git a/R/column_spec.R b/R/column_spec.R
index bc8ef7e..9748713 100644
--- a/R/column_spec.R
+++ b/R/column_spec.R
@@ -13,7 +13,8 @@
#' @param italic A T/F value to control whether the text of the selected column
#' need to be emphasized.
#' @export
-column_spec <- function(kable_input, column, width, bold = F, italic = F) {
+column_spec <- function(kable_input, column,
+ width = NULL, bold = F, italic = F) {
if (!is.numeric(column)) {
stop("column must be a numeric value")
}
@@ -36,7 +37,7 @@
kable_tbody <- xml_tpart(kable_xml, "tbody")
group_header_rows <- attr(kable_input, "group_header_rows")
- all_contents_rows <- seq(1, length(kable_tbody))
+ all_contents_rows <- seq(1, length(xml_children(kable_tbody)))
if (!is.null(group_header_rows)) {
all_contents_rows <- all_contents_rows[!all_contents_rows %in%
group_header_rows]
diff --git a/docs/awesome_table_in_html.Rmd b/docs/awesome_table_in_html.Rmd
index b79235b..aa19e67 100644
--- a/docs/awesome_table_in_html.Rmd
+++ b/docs/awesome_table_in_html.Rmd
@@ -168,7 +168,7 @@
# Group Rows
Sometimes we want a few rows of the table being grouped together. They might be items under the same topic (e.g., animals in one species) or just different data groups for a categorical variable (e.g., age < 40, age > 40). With the new function `group_rows()` in `kableExtra`, this kind of task can be completed in one line. Please see the example below. Note that when you count for the start/end rows of the group, you don't need to count for the header rows nor other group label rows. You only need to think about the row numbers in the "original R dataframe".
```{r}
-kable(mtcars[1:10, 1:6], format = "html", caption = "Group Rows") %>%
+kable(mtcars[1:10, 1:6], caption = "Group Rows") %>%
kable_styling("striped", full_width = F) %>%
group_rows("Group 1", 4, 7) %>%
group_rows("Group 2", 8, 10)
@@ -176,7 +176,7 @@
For advanced users, you can even define your own css for the group labeling.
```{r}
-kable(dt, format = "html") %>%
+kable(dt) %>%
kable_styling("striped", full_width = F) %>%
group_rows("Group 1", 3, 5, label_row_css = "background-color: #666; color: #fff;")
```
@@ -185,7 +185,29 @@
Unlike `group_rows()`, which will insert a labeling row, sometimes we want to list a few sub groups under a total one. In that case, `add_indent()` is probably more apporiate.
For advanced users, you can even define your own css for the group labeling.
```{r}
-kable(dt, format = "html") %>%
+kable(dt) %>%
kable_styling("striped", full_width = F) %>%
add_indent(c(1, 3, 5))
```
+
+---
+
+The following feature is introduced in `kableExtra` 0.2.1.
+
+# Column Style Specification
+When you have a table with lots of explanatory texts, you may want to specified the column width for different column, since the auto adjust in HTML may not work in its best way while basic LaTeX table is really bad at handling text wrapping. Also, sometimes, you may want to highlight a column (e.g. a "Total" column) by making it bold. In these scenario, you can use `column_spec()`. You can find an example below.
+```{r}
+text_tbl <- data.frame(
+ Item = c("Item 1", "Item 2", "Item 3"),
+ FeatureA = 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, format = "html") %>%
+ kable_styling(full_width = F) %>%
+ column_spec(1, bold = T) %>%
+ column_spec(2, width = "30em")
+```
diff --git a/docs/awesome_table_in_html.html b/docs/awesome_table_in_html.html
index 4c3c26a..132501d 100644
--- a/docs/awesome_table_in_html.html
+++ b/docs/awesome_table_in_html.html
@@ -11,7 +11,7 @@
<meta name="author" content="Hao Zhu" />
-<meta name="date" content="2017-05-22" />
+<meta name="date" content="2017-05-24" />
<title>Create Awesome HTML Table with knitr::kable and kableExtra</title>
@@ -217,7 +217,7 @@
<h1 class="title toc-ignore">Create Awesome HTML Table with knitr::kable and kableExtra</h1>
<h4 class="author"><em>Hao Zhu</em></h4>
-<h4 class="date"><em>2017-05-22</em></h4>
+<h4 class="date"><em>2017-05-24</em></h4>
</div>
@@ -2624,7 +2624,7 @@
<div id="group-rows" class="section level1">
<h1>Group Rows</h1>
<p>Sometimes we want a few rows of the table being grouped together. They might be items under the same topic (e.g., animals in one species) or just different data groups for a categorical variable (e.g., age < 40, age > 40). With the new function <code>group_rows()</code> in <code>kableExtra</code>, this kind of task can be completed in one line. Please see the example below. Note that when you count for the start/end rows of the group, you don’t need to count for the header rows nor other group label rows. You only need to think about the row numbers in the “original R dataframe”.</p>
-<pre class="r"><code>kable(mtcars[1:10, 1:6], format = "html", caption = "Group Rows") %>%
+<pre class="r"><code>kable(mtcars[1:10, 1:6], caption = "Group Rows") %>%
kable_styling("striped", full_width = F) %>%
group_rows("Group 1", 4, 7) %>%
group_rows("Group 2", 8, 10)</code></pre>
@@ -2901,7 +2901,7 @@
</tbody>
</table>
<p>For advanced users, you can even define your own css for the group labeling.</p>
-<pre class="r"><code>kable(dt, format = "html") %>%
+<pre class="r"><code>kable(dt) %>%
kable_styling("striped", full_width = F) %>%
group_rows("Group 1", 3, 5, label_row_css = "background-color: #666; color: #fff;")</code></pre>
<?xml version="1.0" encoding="UTF-8" ?>
@@ -3057,7 +3057,7 @@
<div id="add-indentation" class="section level1">
<h1>Add indentation</h1>
<p>Unlike <code>group_rows()</code>, which will insert a labeling row, sometimes we want to list a few sub groups under a total one. In that case, <code>add_indent()</code> is probably more apporiate. For advanced users, you can even define your own css for the group labeling.</p>
-<pre class="r"><code>kable(dt, format = "html") %>%
+<pre class="r"><code>kable(dt) %>%
kable_styling("striped", full_width = F) %>%
add_indent(c(1, 3, 5))</code></pre>
<?xml version="1.0" encoding="UTF-8" ?>
@@ -3204,6 +3204,64 @@
</tr>
</tbody>
</table>
+<hr />
+<p>The following feature is introduced in <code>kableExtra</code> 0.2.1.</p>
+</div>
+<div id="column-style-specification" class="section level1">
+<h1>Column Style Specification</h1>
+<p>When you have a table with lots of explanatory texts, you may want to specified the column width for different column, since the auto adjust in HTML may not work in its best way while basic LaTeX table is really bad at handling text wrapping. Also, sometimes, you may want to highlight a column (e.g. a “Total” column) by making it bold. In these scenario, you can use <code>column_spec()</code>. You can find an example below.</p>
+<pre class="r"><code>text_tbl <- data.frame(
+ Item = c("Item 1", "Item 2", "Item 3"),
+ FeatureA = 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")</code></pre>
+<?xml version="1.0" encoding="UTF-8" ?>
+<table class="table" style="width: auto !important; margin-left: auto; margin-right: auto;">
+<thead>
+<tr>
+<th style="text-align:left;">
+Item
+</th>
+<th style="text-align:left;">
+FeatureA
+</th>
+</tr>
+</thead>
+<tbody>
+<tr>
+<td style="text-align:left;font-weight: bold;">
+Item 1
+</td>
+<td style="text-align:left;width: 30em; ">
+Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex. Morbi malesuada sagittis turpis, at venenatis nisl luctus a.
+</td>
+</tr>
+<tr>
+<td style="text-align:left;font-weight: bold;">
+Item 2
+</td>
+<td style="text-align:left;width: 30em; ">
+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.
+</td>
+</tr>
+<tr>
+<td style="text-align:left;">
+Item 3
+</td>
+<td style="text-align:left;">
+Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat et sollicitudin venenatis. In ac lacus at velit scelerisque mattis.
+</td>
+</tr>
+</tbody>
+</table>
</div>
diff --git a/tests/visual_tests/column_width_html.Rmd b/tests/visual_tests/column_width_html.Rmd
index 54f5ab7..3360c47 100644
--- a/tests/visual_tests/column_width_html.Rmd
+++ b/tests/visual_tests/column_width_html.Rmd
@@ -19,11 +19,11 @@
library(xml2)
dt <- data.frame(
- row_id = 1:3,
+ row_id = c("abb", "xxx", "aaa"),
contents = c(
- "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex. Morbi malesuada sagittis turpis, at venenatis nisl luctus a. Sed suscipit, odio maximus fringilla rutrum, massa dolor elementum eros, at eleifend lectus augue vitae velit. Morbi congue congue lectus et auctor. Pellentesque tincidunt mi nec tristique malesuada. Cras pharetra lectus ut volutpat hendrerit. Ut dapibus orci ligula, id interdum metus mattis at. Sed egestas vitae nisi nec interdum. Nullam efficitur molestie aliquet. Nam cursus consectetur libero id pretium. Maecenas urna neque, dignissim ut leo vel, ultricies vestibulum nisi.",
- "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. Etiam posuere enim a sodales congue. Curabitur ac vehicula tortor. Proin ac nulla in eros scelerisque volutpat in in mi. Nullam efficitur sem nec eros mattis, id venenatis dolor auctor. Nunc dignissim aliquet tincidunt. Nulla rhoncus massa leo, vitae mattis urna lobortis vitae. Curabitur fringilla sodales tincidunt. Cras arcu erat, hendrerit vitae arcu eget, laoreet hendrerit nibh. Maecenas vitae suscipit metus. Fusce ac enim posuere, scelerisque dui vitae, tristique libero. Nunc venenatis sodales nunc, in consectetur nisl hendrerit id. Fusce quis nibh vitae elit tristique blandit. Nunc accumsan pulvinar varius.",
- "Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat et sollicitudin venenatis. In ac lacus at velit scelerisque mattis. Suspendisse potenti. Etiam viverra, massa id lobortis facilisis, ex nibh dictum ligula, id interdum leo velit commodo velit. Nulla malesuada vel arcu at malesuada. Quisque feugiat nunc id nisl varius, nec dapibus tellus ultricies. Donec enim velit, semper in molestie vel, auctor porta lorem. Donec egestas venenatis augue sit amet tristique. Sed in justo ut est dictum consequat. Morbi dictum ornare magna vitae gravida. Nunc congue diam vitae lacinia vulputate. Curabitur hendrerit, erat quis tempus eleifend, dui turpis pellentesque libero, et pretium odio lorem eu magna."
+ "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. "
),
contents2 = c(
"Lorem ipsum dolor",
@@ -31,9 +31,9 @@
"Vivamus venenatis"
),
contents3 = c(
- "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex. Morbi malesuada sagittis turpis, at venenatis nisl luctus a. Sed suscipit, odio maximus fringilla rutrum, massa dolor elementum eros, at eleifend lectus augue vitae velit. Morbi congue congue lectus et auctor. Pellentesque tincidunt mi nec tristique malesuada. Cras pharetra lectus ut volutpat hendrerit. Ut dapibus orci ligula, id interdum metus mattis at. Sed egestas vitae nisi nec interdum. Nullam efficitur molestie aliquet. Nam cursus consectetur libero id pretium. Maecenas urna neque, dignissim ut leo vel, ultricies vestibulum nisi.",
- "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. Etiam posuere enim a sodales congue. Curabitur ac vehicula tortor. Proin ac nulla in eros scelerisque volutpat in in mi. Nullam efficitur sem nec eros mattis, id venenatis dolor auctor. Nunc dignissim aliquet tincidunt. Nulla rhoncus massa leo, vitae mattis urna lobortis vitae. Curabitur fringilla sodales tincidunt. Cras arcu erat, hendrerit vitae arcu eget, laoreet hendrerit nibh. Maecenas vitae suscipit metus. Fusce ac enim posuere, scelerisque dui vitae, tristique libero. Nunc venenatis sodales nunc, in consectetur nisl hendrerit id. Fusce quis nibh vitae elit tristique blandit. Nunc accumsan pulvinar varius.",
- "Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat et sollicitudin venenatis. In ac lacus at velit scelerisque mattis. Suspendisse potenti. Etiam viverra, massa id lobortis facilisis, ex nibh dictum ligula, id interdum leo velit commodo velit. Nulla malesuada vel arcu at malesuada. Quisque feugiat nunc id nisl varius, nec dapibus tellus ultricies. Donec enim velit, semper in molestie vel, auctor porta lorem. Donec egestas venenatis augue sit amet tristique. Sed in justo ut est dictum consequat. Morbi dictum ornare magna vitae gravida. Nunc congue diam vitae lacinia vulputate. Curabitur hendrerit, erat quis tempus eleifend, dui turpis pellentesque libero, et pretium odio lorem eu magna."
+ "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. "
)
)
diff --git a/tests/visual_tests/column_width_pdf.Rmd b/tests/visual_tests/column_width_pdf.Rmd
index d8e8a52..374cc90 100644
--- a/tests/visual_tests/column_width_pdf.Rmd
+++ b/tests/visual_tests/column_width_pdf.Rmd
@@ -19,11 +19,11 @@
library(xml2)
dt <- data.frame(
- row_id = 1:3,
+ row_id = c("abb", "xxx", "aaa"),
contents = c(
- "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex. Morbi malesuada sagittis turpis, at venenatis nisl luctus a. Sed suscipit, odio maximus fringilla rutrum, massa dolor elementum eros, at eleifend lectus augue vitae velit. Morbi congue congue lectus et auctor. Pellentesque tincidunt mi nec tristique malesuada. Cras pharetra lectus ut volutpat hendrerit. Ut dapibus orci ligula, id interdum metus mattis at. Sed egestas vitae nisi nec interdum. Nullam efficitur molestie aliquet. Nam cursus consectetur libero id pretium. Maecenas urna neque, dignissim ut leo vel, ultricies vestibulum nisi.",
- "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. Etiam posuere enim a sodales congue. Curabitur ac vehicula tortor. Proin ac nulla in eros scelerisque volutpat in in mi. Nullam efficitur sem nec eros mattis, id venenatis dolor auctor. Nunc dignissim aliquet tincidunt. Nulla rhoncus massa leo, vitae mattis urna lobortis vitae. Curabitur fringilla sodales tincidunt. Cras arcu erat, hendrerit vitae arcu eget, laoreet hendrerit nibh. Maecenas vitae suscipit metus. Fusce ac enim posuere, scelerisque dui vitae, tristique libero. Nunc venenatis sodales nunc, in consectetur nisl hendrerit id. Fusce quis nibh vitae elit tristique blandit. Nunc accumsan pulvinar varius.",
- "Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat et sollicitudin venenatis. In ac lacus at velit scelerisque mattis. Suspendisse potenti. Etiam viverra, massa id lobortis facilisis, ex nibh dictum ligula, id interdum leo velit commodo velit. Nulla malesuada vel arcu at malesuada. Quisque feugiat nunc id nisl varius, nec dapibus tellus ultricies. Donec enim velit, semper in molestie vel, auctor porta lorem. Donec egestas venenatis augue sit amet tristique. Sed in justo ut est dictum consequat. Morbi dictum ornare magna vitae gravida. Nunc congue diam vitae lacinia vulputate. Curabitur hendrerit, erat quis tempus eleifend, dui turpis pellentesque libero, et pretium odio lorem eu magna."
+ "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. "
),
contents2 = c(
"Lorem ipsum dolor",
@@ -31,14 +31,16 @@
"Vivamus venenatis"
),
contents3 = c(
- "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex. Morbi malesuada sagittis turpis, at venenatis nisl luctus a. Sed suscipit, odio maximus fringilla rutrum, massa dolor elementum eros, at eleifend lectus augue vitae velit. Morbi congue congue lectus et auctor. Pellentesque tincidunt mi nec tristique malesuada. Cras pharetra lectus ut volutpat hendrerit. Ut dapibus orci ligula, id interdum metus mattis at. Sed egestas vitae nisi nec interdum. Nullam efficitur molestie aliquet. Nam cursus consectetur libero id pretium. Maecenas urna neque, dignissim ut leo vel, ultricies vestibulum nisi.",
- "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. Etiam posuere enim a sodales congue. Curabitur ac vehicula tortor. Proin ac nulla in eros scelerisque volutpat in in mi. Nullam efficitur sem nec eros mattis, id venenatis dolor auctor. Nunc dignissim aliquet tincidunt. Nulla rhoncus massa leo, vitae mattis urna lobortis vitae. Curabitur fringilla sodales tincidunt. Cras arcu erat, hendrerit vitae arcu eget, laoreet hendrerit nibh. Maecenas vitae suscipit metus. Fusce ac enim posuere, scelerisque dui vitae, tristique libero. Nunc venenatis sodales nunc, in consectetur nisl hendrerit id. Fusce quis nibh vitae elit tristique blandit. Nunc accumsan pulvinar varius.",
- "Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat et sollicitudin venenatis. In ac lacus at velit scelerisque mattis. Suspendisse potenti. Etiam viverra, massa id lobortis facilisis, ex nibh dictum ligula, id interdum leo velit commodo velit. Nulla malesuada vel arcu at malesuada. Quisque feugiat nunc id nisl varius, nec dapibus tellus ultricies. Donec enim velit, semper in molestie vel, auctor porta lorem. Donec egestas venenatis augue sit amet tristique. Sed in justo ut est dictum consequat. Morbi dictum ornare magna vitae gravida. Nunc congue diam vitae lacinia vulputate. Curabitur hendrerit, erat quis tempus eleifend, dui turpis pellentesque libero, et pretium odio lorem eu magna."
+ "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(dt, "latex", booktabs = T) %>%
- column_spec(2, "5cm", bold = T) %>%
- column_spec(4, "3cm", italic = T)
+ kable_styling(latex_options = "scale_down") %>%
+ column_spec(2, "30em", bold = T) %>%
+ column_spec(4, "3cm", italic = T) %>%
+ group_rows("Group1", 1, 2)
```