简体   繁体   English

Knitr html 格式问题

[英]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.当编织到 html 时,dataframe 中的值会发生扭曲。当值小于 3 个字符时会发生这种情况,如下所示。 -5770 is converted properly to (5770) but -577 results in 577. with an li element appended after. -5770 已正确转换为(5770) ,但 -577 的结果为577.并在其后附加了一个 li 元素。 Is this a bug with knitr or an error in my code?这是 knitr 的错误还是我的代码中的错误?

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:在 R-studio 中更正 Output:

在此处输入图像描述

Wrong output in html file using knit to html with R-studio:使用knit 的 html 文件中的错误 output与 R-studio 的 html:

在此处输入图像描述

So the problem is the (577) or any whole number inside a bracket get phrased by pandocs into an ordered list.所以问题是(577)或括号内的任何整数被 pandocs 表达为有序列表。 To prevent this you can disable this in you YAML, just add the following.为了防止这种情况,您可以在 YAML 中禁用它,只需添加以下内容。

output: 
  html_document:
    md_extensions: "-fancy_lists"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM