R Markdown,当块选项结果="asis"时输出测试结果(htest). [英] R Markdown, output test results (htest) when chunk option results="asis"
问题描述
出于以下原因,我需要使用results = "asis"
: https://stackoverflow.com/a/36381976/
I need to use results = "asis"
for reasons stated here: https://stackoverflow.com/a/36381976/
但是,使用该chunk选项意味着其他输出呈现不理想的状态.具体来说,我在输出prop.test
结果时遇到问题,但是我敢肯定,其他数据类型也会发生这种情况.
However, using that chunk option means other outputs render non-ideally. Specifically I'm having issues outputting prop.test
results, but I'm sure this would happen for other data types.
在下面的示例中,我提供了4个选项,所有这些选项在某种程度上都不够用:
I've provided 4 options in the example below, all of which fall short in some way:
---
title: "R Notebook"
output:
html_document:
df_print: paged
---
```{r, echo=F, message=F, warning=F, results="asis"}
library(knitr)
library(pander)
out <- prop.test(c(10,30), c(20,40))
cat("# Header \n")
cat(" \n## Straight output\n")
out # Only properly renders first line
cat(" \n## Print\n")
print(out) # Only properly renders first line
cat(" \n## Kable\n")
#kable(out) # Will fail: Error in as.data.frame.default(x) : cannot coerce class ""htest"" to a data.frame
kable(unlist(out)) # Renders everything but in an ugly way
cat(" \n## Pander\n")
pander(out) # Misses confidence interval.
cat(" \n As you can see, Pander misses some information, such as the confidence interval")
```
Pander
使其最接近美观的显示,但缺少一些信息(置信区间).也许有一种方法可以使其全部显示?
Pander
gets it closest to a nice display but misses some information (confidence interval). Perhaps there's a way to make it display all?
如何很好地显示prop.test
及类似内容的输出?
How can I nicely display the output of prop.test
and similar?
推荐答案
您可以像这样使用formattable
library(knitr)
library(formattable)
out <- prop.test(c(10,30), c(20,40))
cat("# Header \n")
cat(" \n## Straight output\n")
out # Only properly renders first line
cat(" \n## Print\n")
print(out) # Only properly renders first line
cat(" \n## Kable\n")
#kable(out) # Will fail: Error in as.data.frame.default(x) : cannot coerce class ""htest"" to a data.frame
kable(unlist(out)) # Renders everything but in an ugly way
cat(" \n## Pander\n")
df <- data.frame(value = unlist(out))
tdf <- as.data.frame(t(df))
formattable(tdf)
您可以保留所需的列,并更新列名称,因为所有这些列均位于data frame
中.外观很粗糙的例子在这里
You can keep the columns you want, update the column names as all of these are in data frame
. A rough example of how it looks is here
这篇关于R Markdown,当块选项结果="asis"时输出测试结果(htest).的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!