issue #26 & #66
diff --git a/R/row_spec.R b/R/row_spec.R
index 3cd4808..f45cb3d 100644
--- a/R/row_spec.R
+++ b/R/row_spec.R
@@ -1,8 +1,7 @@
 #' Specify the look of the selected row
 #'
 #' @description This function allows users to select a row and then specify
-#' its look. Right now it supports the following two properties: bold text and
-#' italic text.
+#' its look. It can also specify the format of the header row when `row` = 0.
 #'
 #' @param kable_input Output of `knitr::kable()` with `format` specified
 #' @param row A numeric value or vector indicating which row(s) to be selected. You don't
@@ -13,10 +12,13 @@
 #' need to be emphasized.
 #' @param monospace A T/F value to control whether the text of the selected column
 #' need to be monospaced (verbatim)
-#' @param color A character string for column text color. Here please pay
+#' @param color A character string for row text color. Here please pay
 #' attention to the differences in color codes between HTML and LaTeX.
-#' @param background A character string for column background color. Here please
+#' @param background A character string for row background color. Here please
 #' pay attention to the differences in color codes between HTML and LaTeX.
+#' @param align A character string for cell alignment. For HTML, possible values could
+#' be `l`, `c`, `r` plus `left`, `center`, `right`, `justify`, `initial` and `inherit`
+#' while for LaTeX, you can only choose from `l`, `c` & `r`.
 #'
 #' @examples x <- knitr::kable(head(mtcars), "html")
 #' row_spec(x, 1:2, bold = TRUE, italic = TRUE)
@@ -24,7 +26,7 @@
 #' @export
 row_spec <- function(kable_input, row,
                      bold = FALSE, italic = FALSE, monospace = FALSE,
-                     color = NULL, background = NULL) {
+                     color = NULL, background = NULL, align = NULL) {
   if (!is.numeric(row)) {
     stop("row must be numeric. ")
   }
@@ -35,50 +37,49 @@
   }
   if (kable_format == "html") {
     return(row_spec_html(kable_input, row, bold, italic, monospace,
-                         color, background))
+                         color, background, align))
   }
   if (kable_format == "latex") {
     return(row_spec_latex(kable_input, row, bold, italic, monospace,
-                          color, background))
+                          color, background, align))
   }
 }
 
 row_spec_html <- function(kable_input, row, bold, italic, monospace,
-                          color, background) {
+                          color, background, align) {
   kable_attrs <- attributes(kable_input)
   kable_xml <- read_kable_as_xml(kable_input)
-  kable_tbody <- xml_tpart(kable_xml, "tbody")
 
-  group_header_rows <- attr(kable_input, "group_header_rows")
-  if (!is.null(group_header_rows)) {
-      row <- positions_corrector(row, group_header_rows,
-                                 length(xml_children(kable_tbody)))
+  if (!is.null(align)) {
+    if (align %in% c("l", "c", "r")) {
+      align <- switch(align, r = "right", c = "center", l = "left")
+    }
   }
 
-  for (j in row) {
-    target_row <- xml_child(kable_tbody, j)
-    for (i in 1:length(xml_children(target_row))) {
-      target_cell <- xml_child(target_row, i)
-      if (bold) {
-        xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
-                                                 "font-weight: bold;")
-      }
-      if (italic) {
-        xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
-                                                 "font-style: italic;")
-      }
-      if (monospace) {
-        xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
-                                                 "font-family: monospace;")
-      }
-      if (!is.null(color)) {
-        xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
-                                                 "color: ", color, ";")
-      }
-      if (!is.null(background)) {
-        xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
-                                                 "background-color: ",
-                                                 background, ";")
+  if (0 %in% row) {
+    kable_thead <- xml_tpart(kable_xml, "thead")
+    original_header_row <- xml_child(kable_thead, length(xml_children(kable_thead)))
+    for (theader_i in 1:length(xml_children(original_header_row))) {
+      target_header_cell <- xml_child(original_header_row, theader_i)
+      xml_cell_style(target_header_cell, bold, italic, monospace, color, background, align)
+    }
+    row <- row[row != 0]
+  }
+
+  if (length(row) != 0) {
+    kable_tbody <- xml_tpart(kable_xml, "tbody")
+
+    group_header_rows <- attr(kable_input, "group_header_rows")
+    if (!is.null(group_header_rows)) {
+      row <- positions_corrector(row, group_header_rows,
+                                 length(xml_children(kable_tbody)))
+    }
+
+    for (j in row) {
+      target_row <- xml_child(kable_tbody, j)
+      for (i in 1:length(xml_children(target_row))) {
+        target_cell <- xml_child(target_row, i)
+        xml_cell_style(target_cell, bold, italic, monospace, color, background, align)
       }
     }
   }
@@ -88,15 +89,44 @@
   return(out)
 }
 
+xml_cell_style <- function(x, bold, italic, monospace, color, background, align) {
+  if (bold) {
+    xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
+                                             "font-weight: bold;")
+  }
+  if (italic) {
+    xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
+                                             "font-style: italic;")
+  }
+  if (monospace) {
+    xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
+                                             "font-family: monospace;")
+  }
+  if (!is.null(color)) {
+    xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
+                                             "color: ", color, ";")
+  }
+  if (!is.null(background)) {
+    xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
+                                             "background-color: ",
+                                             background, ";")
+  }
+  if (!is.null(align)) {
+    xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
+                                   "text-align: ", align, ";")
+  }
+  return(x)
+}
+
 row_spec_latex <- function(kable_input, row, bold, italic, monospace,
-                           color, background) {
+                           color, background, align) {
   table_info <- magic_mirror(kable_input)
   out <- enc2utf8(as.character(kable_input))
   row <- row + 1
   for (i in row) {
     target_row <- table_info$contents[i]
     new_row <- latex_new_row_builder(target_row, bold, italic, monospace,
-                                     color, background)
+                                     color, background, align)
     out <- sub(target_row, new_row, out, perl = T)
   }
 
@@ -106,7 +136,7 @@
 }
 
 latex_new_row_builder <- function(target_row, bold, italic, monospace,
-                                  color, background) {
+                                  color, background, align) {
   new_row <- latex_row_cells(target_row)
   if (bold) {
     new_row <- lapply(new_row, function(x) {
@@ -129,11 +159,19 @@
       paste0("\\\\textcolor{", color, "}{", x, "}")
     })
   }
+
+  if (!is.null(align)) {
+    new_row <- lapply(new_row, function(x) {
+      paste0("\\\\multicolumn{1}{", align, "}{", x, "}")
+    })
+  }
   new_row <- paste(unlist(new_row), collapse = " & ")
 
   if (!is.null(background)) {
     new_row <- paste0("\\\\rowcolor{", background, "}  ", new_row)
   }
 
+
+
   return(new_row)
 }