Search code examples
rr-markdownknitrkablekableextra

Knitr html formatting issue


The values in the dataframe is warped when knitting to html. It happens when value is less that 3 characters as shown below. -5770 is converted properly to (5770) but -577 results in 577. with an li element appended after. Is this a bug with knitr or an error in my code?

round_numeric <- function(num, prec = 0) {
  return (round(as.numeric(num), prec))
}

format_numeric <- function(num, prec = 0) {
  rounded_num <- abs(round_numeric(num, prec))
  res <- format(rounded_num, nsmall = prec, big.mark = ',', trim = TRUE)
  return (ifelse(num >= 0, res, sprintf('(%s)', res)))
}

col1 <- format_numeric(-5770)
col2 <- format_numeric(-577)
col3 <- format_numeric(300)

df <- t(data.frame(row1 = c(col1, col2, col3)))

df %>%
  kable(align = 'c', format = 'html') %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"))

Correct Output in R-studio:

enter image description here

Wrong output in html file using knit to html with R-studio:

enter image description here


Solution

  • So the problem is the (577) or any whole number inside a bracket get phrased by pandocs into an ordered list. To prevent this you can disable this in you YAML, just add the following.

    output: 
      html_document:
        md_extensions: "-fancy_lists"