0

I have the following code:

---
title: "test"
author: "author"
date: "2020/11/17"
output: pdf_document
always_allow_html: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r cars}
library(compareGroups)
export2md(createTable(compareGroups(Species~.,data=iris)))
```

I use the following command to run this code:

rmarkdown::render("test.Rmd")

I expect that the table will look like this:


Summary descriptives table by groups of `Species'
setosa  versicolor  virginica   p.overall
N=50    N=50    N=50    
Sepal.Length    5.01 (0.35) 5.94 (0.52) 6.59 (0.64) <0.001
Sepal.Width 3.43 (0.38) 2.77 (0.31) 2.97 (0.32) <0.001
Petal.Length    1.46 (0.17) 4.26 (0.47) 5.55 (0.55) <0.001
Petal.Width 0.25 (0.11) 1.33 (0.20) 2.03 (0.27) <0.001

But ends up looking like this in the PDF I produce from the Rmarkdown code:

library(compareGroups)
export2md(createTable(compareGroups(Species~.,data=iris)))
## Warning in export2md(createTable(compareGroups(Species ~ ., data = iris))): You are calling export2md ## -> html format is assigned
Summary descriptives table by groups of ‘Species’
setosa
versicolor
virginica
p.overall
N=50
N=50
N=50
Sepal.Length
5.01 (0.35)
5.94 (0.52)
6.59 (0.64)
<0.001
Sepal.Width
3.43 (0.38)
2.77 (0.31)
2.97 (0.32)
<0.001
Petal.Length
1.46 (0.17)
4.26 (0.47)
5.55 (0.55)
<0.001
Petal.Width
0.25 (0.11)

Why would this be and what can I do to properly display the table in my exported pdf?

Pete
  • 321
  • 4
  • 16

2 Answers2

1

This allows for a fresh clean environment. resource info

SOLUTION:

xfun::Rscript_call(
rmarkdown::render,
list(input = 'test.Rmd', output_format = 'pdf_document'))

enter image description here

OTHER POTENTIAL SOLUTIONS:

Not sure why, but WITHOUT changing your code, the render() did produce the incorrect output you had. BUT using the knit button in RStudio rendered it to the right output

But another way is to output it to HTML, and then print it to pdf via the web browser for another PDF document output, which is an official way to get some best results when working with tables and output formats.

---
title: "test"
author: "author"
date: "2020/11/17"
output: html_document
always_allow_html: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r cars}
library(compareGroups)
export2md(createTable(compareGroups(Species~.,data=iris)))
```

enter image description here

Daniel_j_iii
  • 3,041
  • 2
  • 11
  • 27
1

Problem has been solved (rather simply).

Code should have been:

export2md(table_1, format="markdown")

Pete
  • 321
  • 4
  • 16