knitr kable formatting cell text as ordered list

Viewed 153

I'm creating a table in a rmarkdown/html document and it's inadvertently changing cell values to an ordered list when in the format of for example (123). An example is below. Any help would be most welcomed.

output is here

---
output: html_document
---

df <-
  data.frame(
    col1 = c("a", "b"),
    col2 = c("(123)", "(61)"),
    stringsAsFactors = FALSE
    )

knitr::kable(
  df,
  format = "html",
  align = c('lr')
)
2 Answers

The values are recognized as markdown and pandoc thinks that (123) and (61) are list items. Try

df <- data.frame(col1 = c("a", "b"),
                 col2 = c("(123)", "(61)"),
                 stringsAsFactors = F)

df$col2 <- gsub("\\(", "&lpar;", df$col2)
df$col2 <- gsub("\\)", "&rpar;", df$col2)

to replace the parenthesis by their HTML entity code.

Note: stringsAsFactors = F is the new default in R > 4.0

Another approach is to disable the fancy_lists extension. This will apply globally to the document, so you might prefer @MartinSchmelzer's solution.

Just put this in your YAML:

output: 
  html_document:
    md_extensions: "-fancy_lists"

After you've done this, you'll only get ordered lists with numbers (no letters or Roman numerals), and they have to be in the format 1., not (1) or 1), etc.

Related