3

I need to use results = "asis" for reasons stated here: https://stackoverflow.com/a/36381976/

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.

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

browser_screenshot

Pander gets it closest to a nice display but misses some information (confidence interval). Perhaps there's a way to make it display all?

How can I nicely display the output of prop.test and similar?

Spacedman
  • 92,590
  • 12
  • 140
  • 224
conor
  • 1,204
  • 1
  • 18
  • 22
  • 1
    You can also combine `pander` with `broom`, eg `pander(broom::glance(out), split.table = Inf)` – daroczig Oct 24 '17 at 07:06
  • 2
    You should check [here](http://haozhu233.github.io/kableExtra/awesome_table_in_html.html) to see how to make your kable result look better – Hao Oct 24 '17 at 15:56
  • I've accepted @Hardik gupta's answer. @daroczig's comment using Pander+Broom is also very nice, though excludes the `out$data.name` variable – conor Oct 25 '17 at 00:47
  • @Hao thanks for the useful link. While it doesn't cover the rendering of the output of statistical tests such as `prop.test` that this question is about, I could see it being useful by first converting the output to a data.frame and combining the result with `kable` – conor Oct 25 '17 at 00:55

2 Answers2

4

One option is to return to results = "markup" (the default) and replace your cat calls with asis_output (from the knitr package).

---
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")
```
Benjamin
  • 16,897
  • 6
  • 45
  • 65
  • This did allow the "Straight output" and "Print" versions to write `out` as you'd see it in the console, which is handy. The Kable and Pander outputs were the same as my example. – conor Oct 25 '17 at 00:58
2

You can use formattable like this

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)

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

enter image description here

Hardik Gupta
  • 4,700
  • 9
  • 41
  • 83
  • This works, and I like the freedom of editing the data frame (e.g. reordering and renaming columns) and rendering that. – conor Oct 25 '17 at 00:45