Skip to content

Instantly share code, notes, and snippets.

@rkrug
Last active March 27, 2019 15:51
Show Gist options
  • Save rkrug/fcb145f5ebf27ded865cca943332dbf6 to your computer and use it in GitHub Desktop.
Save rkrug/fcb145f5ebf27ded865cca943332dbf6 to your computer and use it in GitHub Desktop.
kable table as return from function as character
---
title: "test"
author: "Rainer M Krug"
date: "3/27/2019"
output:
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
```
```{r}
x <- data.frame(a = 1:10, b = runif(10))
```
This works:
```{r, results = "asis"}
cat("Some text")
knitr::kable(x)
```
Now consider the following function:
```{r}
fun <- function (x) {
result <-
paste0(
"Some text\n\n",
knitr::kable(x, format = "markdown")
)
return(result)
}
```
How to make this work?
This does not work:
```{r , results = "asis"}
fun(x)
```
This also does not work:
```{r , results = "asis", comment = NA}
cat(fun(x))
```
How can I make this work?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment