1

I use and like the sjt.xtab crosstab tool from the sjPlot package. Is there a way to create multiple crosstabs in one output file for a defined independent variable and multiple dependent variables?

Here are the reproducible data:

data <- data.frame(
  group = sample(LETTERS[1:3], 50, replace = T),
  var_1 = sample(c("Yes", "No"), 50, replace = T),
  var_2 = sample(c("Low","Mid","High"), 50, replace = T),
  var_3 = sample(c("Yes","No","Don't know"), 50, replace = T))
)

The code: sjPlot::sjt.xtab(data$group, data$var_1, file="output.html") works great.

I would like to see three crosstables produced by sjt.xtab in one .html or .docx file.

I am working with a huge number of categorical or ordinal variables and I need to perfom lot of cross-checking. That's why I am looking for some solutions to perform them at once and store them in one output/report.

2 Answers 2

1

You could use quarto or RMarkdown to compose a document that can be rendered as HTML or DOCX. Below is an example quarto file that renders to HTML. I tested DOCX too, but the sjPlot::sjt.xtab() tables do not show up in DOCX. Maybe with some tweaking that can be fixed. Tables created with the gt, gtsummary or flextable packages usually work well across different quarto output formats.

---
title: "Multi Output"
format: html
execute:
    echo: false
    warning: false
---

```{r}
data <- data.frame(
  group = sample(LETTERS[1:3], 50, replace = T),
  var_1 = sample(c("Yes", "No"), 50, replace = T),
  var_2 = sample(c("Low","Mid","High"), 50, replace = T),
  var_3 = sample(c("Yes","No","Don't know"), 50, replace = T))
```

## Table 1

```{r}
sjPlot::sjt.xtab(data$group, data$var_1, file="output.html")
```

## Table 2

```{r}
sjPlot::sjt.xtab(data$group, data$var_1, file="output.html")
```

quarto html output

To programmatically create any number of tables (aka looping) you can utilize purrr::map()/purrr::walk() to iterate over the columns in your dataframe. In the example below we first create an html file for each table in a subfolder ("figures"). We then use quarto inlcudes to integrate these into our quarto document. If you were to create your tables with gt or gtsummary this extra step of creating the individual html files for the tables would not be necessary.

---
title: "Multi Output Programmatic"
format: html
execute:
    echo: false
    warning: false
---

```{r}
library(tidyverse)
```

```{r}
data <- data.frame(
  group = sample(LETTERS[1:3], 50, replace = T),
  var_1 = sample(c("Yes", "No"), 50, replace = T),
  var_2 = sample(c("Low","Mid","High"), 50, replace = T),
  var_3 = sample(c("Yes","No","Don't know"), 50, replace = T))
```

## Tables

```{r}
#| include: false
dir.create("figures")
var_names <- names(data)[-1]

map(var_names,
    \(x) sjPlot::sjt.xtab(data$group, data[[x]], file = paste0("figures/", x, ".html")) )
```

```{r}
#| output: asis

walk(var_names, \(x) {
    cat("##", x, "\n\n")
    cat(paste0("{{< include figures/", x, ".html >}}\n\n"))
    })
```

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks, the exporting task is solved. Both ways quarto or rmarkdown works fine. But the loop challenge is still on.

3 Comments

I think you meant to post this as a comment to my answer? I re-read your question, and it does not include any details on a "for-loop challenge".
Yes, Your're right. I described only the result. My fault! I am working with huge number of categorical or ordinal variables and I need to perfrom lot of cross-checking. That's why I am looking for some solutions to perform them at once and store them in one output/report.
I added an example to create any number of tables programmatically to my answer. I also added the information you added in your comment to your question. Feel free to delete this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.