gpt4 book ai didi

R Markdown,chunk选项结果="asis"时输出测试结果(htest)

转载 作者:行者123 更新时间:2023-12-05 01:44:13 25 4
gpt4 key购买 nike

我需要使用 results = "asis" 原因如下:https://stackoverflow.com/a/36381976/

但是,使用该 block 选项意味着其他输出呈现不理想。具体来说,我在输出 prop.test 结果时遇到了问题,但我确信这会发生在其他数据类型上。

我在下面的示例中提供了 4 个选项,所有这些选项在某些方面都有不足:

---
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")
```

browser_screenshot

Pander 使其最接近漂亮的显示,但遗漏了一些信息(置信区间)。也许有办法让它显示全部?

我怎样才能很好地显示 prop.test 和类似的输出?

最佳答案

一个选项是返回到 results = "markup"(默认)并将您的 cat 调用替换为 asis_output(来自 knitr 包)。

---
title: "R Notebook"
output:
html_document:
df_print: paged
---
```{r, echo=F, message=F, warning=F}
library(knitr)
library(pander)
out <- prop.test(c(10,30), c(20,40))
asis_output("# Header \n")
asis_output(" \n## Straight output\n")
out # Only properly renders first line
asis_output(" \n## Print\n")
print(out) # Only properly renders first line
asis_output(" \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
asis_output(" \n## Pander\n")
pander(out) # Misses confidence interval.
asis_output(" \n As you can see, Pander misses some information, such as the confidence interval")
```

关于R Markdown,chunk选项结果="asis"时输出测试结果(htest),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46903792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com