在 table header R Markdown html 输出中显示带有数学符号的 data.frame
Display a data.frame with mathematical notation in table header R Markdown html output
假设我想在 R Markdown 文件(html 输出)中显示来自几个方程的 table 系数。
我希望 table 看起来像这样:
但我一辈子都想不出如何让 R Markdown 解析 table 中的列名。
我得到的最接近的是使用 cat 从我的 data.frame 打印自定义 table 的 hacky 解决方案...不理想。有更好的方法吗?
我是这样创建上面的图像的,在 RStudio 中将我的文件保存为 .Rmd。
---
title: "Math in R Markdown tables"
output:
html_notebook: default
html_document: default
---
My fancy table
```{r, echo=FALSE, include=TRUE, results="asis"}
# Make data.frame
mathy.df <- data.frame(site = c("A", "B"),
b0 = c(3, 4),
BA = c(1, 2))
# Do terrible things to print it properly
cat("Site|$\beta_0$|$\beta_A$")
cat("\n")
cat("----|---------|---------\n")
for (i in 1:nrow(mathy.df)){
cat(as.character(mathy.df[i,"site"]), "|",
mathy.df[i,"b0"], "|",
mathy.df[i,"BA"],
"\n", sep = "")
}
```
您可以使用 kable()
及其 escape
选项来格式化数学符号(请参阅 this answer 相关问题)。然后将数学标题指定为列名,然后就可以了:
---
title: "Math in R Markdown tables"
output:
html_notebook: default
html_document: default
---
My fancy table
```{r, echo=FALSE, include=TRUE, results="asis"}
library(knitr)
mathy.df <- data.frame(site = c("A", "B"),
b0 = c(3, 4),
BA = c(1, 2))
colnames(mathy.df) <- c("Site", "$\beta_0$", "$\beta_A$")
kable(mathy.df, escape=FALSE)
```
假设我想在 R Markdown 文件(html 输出)中显示来自几个方程的 table 系数。
我希望 table 看起来像这样:
但我一辈子都想不出如何让 R Markdown 解析 table 中的列名。
我得到的最接近的是使用 cat 从我的 data.frame 打印自定义 table 的 hacky 解决方案...不理想。有更好的方法吗?
我是这样创建上面的图像的,在 RStudio 中将我的文件保存为 .Rmd。
---
title: "Math in R Markdown tables"
output:
html_notebook: default
html_document: default
---
My fancy table
```{r, echo=FALSE, include=TRUE, results="asis"}
# Make data.frame
mathy.df <- data.frame(site = c("A", "B"),
b0 = c(3, 4),
BA = c(1, 2))
# Do terrible things to print it properly
cat("Site|$\beta_0$|$\beta_A$")
cat("\n")
cat("----|---------|---------\n")
for (i in 1:nrow(mathy.df)){
cat(as.character(mathy.df[i,"site"]), "|",
mathy.df[i,"b0"], "|",
mathy.df[i,"BA"],
"\n", sep = "")
}
```
您可以使用 kable()
及其 escape
选项来格式化数学符号(请参阅 this answer 相关问题)。然后将数学标题指定为列名,然后就可以了:
---
title: "Math in R Markdown tables"
output:
html_notebook: default
html_document: default
---
My fancy table
```{r, echo=FALSE, include=TRUE, results="asis"}
library(knitr)
mathy.df <- data.frame(site = c("A", "B"),
b0 = c(3, 4),
BA = c(1, 2))
colnames(mathy.df) <- c("Site", "$\beta_0$", "$\beta_A$")
kable(mathy.df, escape=FALSE)
```