I use the following code to create tables for likert scales
``{r results='asis', echo=FALSE}
data_split <- split(data, data$class)
for(i in seq_along(data_split))
{
tab <- data_split[[i]] %>%
pivot_longer(11:48, names_to = "Item") |>
tabyl(Item, value, show_na=F, sort=T) |>
adorn_percentages("row") %>%
adorn_pct_formatting(digits = 0) %>%
adorn_ns(position="front") %>%
gt() |>
opt_stylize(style = 1) |>
print(tab)
}
This works fine when exporting it to html, unfortunately when choosing pdf, the result are shown without format and not as table, just the raw results/texts.
Any ideas?
Here the qhole markdown:
---
title: "Results"
author: ""
header-includes:
\usepackage[ dvipsnames ]{ xcolor }
format:
pdf:
classoption: landscape
geometry:
- top=20mm
- left=15mm
editor: visual
---
{r setup, include=FALSE}
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
library(tidyverse)
library(kableExtra)
library(janitor)
library(gt)
data <- read.csv("C:/Users/.../dataset.csv", quote = "'\"", na.strings=c("", "\"\""), stringsAsFactors=FALSE, fileEncoding="UTF-8-BOM")
# LimeSurvey Field type: F
data[, 1] <- as.numeric(data[, 1])
attributes(data)$variable.labels[1] <- "I like swimming."
data[, 1] <- factor(data[, 1], levels=c(3,2,1,0),labels=c("++", "+", "-", "--"))
names(data)[1] <- "I like swimming."
# LimeSurvey Field type: F
data[, 2] <- as.numeric(data[, 2])
attributes(data)$variable.labels[2] <- "Reading is useful."
data[, 2] <- factor(data[, 2], levels=c(3,2,1,0),labels=c("++", "+", "-", "--"))
names(data)[2] <- "Reading is useful."
# LimeSurvey Field type: F
data[, 3] <- as.numeric(data[, 3])
attributes(data)$variable.labels[3] <- "My teacher is friendly."
data[, 3] <- factor(data[, 13], levels=c(3,2,1,0),labels=c("++", "+", "-", "--"))
names(data)[3] <- "My teacher is friendly."
# LimeSurvey Field type: F
data[, 4] <- as.numeric(data[, 4])
attributes(data)$variable.labels[4] <- "Test ABC"
data[, 4] <- factor(data[, 4], levels=c(3,2,1,0),labels=c("++", "+", "-", "--"))
names(data)[4] <- "Test ABC"
# LimeSurvey Field type: F
data[, 5] <- as.numeric(data[, 5])
attributes(data)$variable.labels[5] <- "class"
data[, 5] <- factor(data[, 5], levels=c(1,2,3,4),labels=c("1.", "2.", "3.", "4."))
names(data)[5] <- "class"
Rücklauf (vollständig ausgefüllt): r casesFinished Personen
\newpage
Anhang
Gesamtergebnisse
{r results='asis', echo=FALSE}
data_selected %>%
pivot_longer(1:4,
names_to = "Item") |>
tabyl(Item, value, show_na=F, sort=T) |>
adorn_percentages("row") %>%
adorn_pct_formatting(digits = 0) %>%
adorn_ns(position="front") %>%
gt() |>
opt_stylize(style = 1)
\newpage
Ergebnisse nach Klassenstufe
{r results='asis', echo=FALSE}
data_split <- split(data, data_selected$class)
for(i in seq_along(data_split))
{
tab <- data_split[[i]] %>%
pivot_longer(1:4, names_to = "Item") |>
tabyl(Item, value, show_na=F, sort=T) |>
adorn_percentages("row") %>%
adorn_pct_formatting(digits = 0) %>%
adorn_ns(position="front") %>%
gt() |>
opt_stylize(style = 1) |>
tab_header(title = md("Results for class"), subtitle = names(data_split[i])) |>
print(tab)
}
Example for Dataset (data.csv):
" [(1) I like swimming.],"" [(2) Reading is useful.]"","" [(3) My teacher is friendly.]"","" [(4) Test ABC]"",""class"""
"3,""3"",""3"",""3"",""1"""
"3,""3"",""3"",""2"",""1"""
"3,""3"",""3"",""2"",""1"""
"3,""3"",""3"",""3"",""1"""
"3,""3"",""3"",""3"",""1"""
","""","""","""","""""
"2,""3"",""2"",""3"",""1"""
"3,""2"",""3"",""3"",""1"""
"2,""3"",""2"",""3"",""1"""
"2,""3"",""3"",""3"",""1"""
"3,""3"",""3"",""2"",""1"""
"3,""2"",""2"",""3"",""2"""
"2,""3"",""3"",""3"",""2"""
"2,""2"",""2"",""2"",""2"""
"3,""2"",""2"",""1"",""2"""
"2,""3"",""3"",""0"",""2"""
"2,""3"",""3"",""3"",""2"""
"1,""1"",""2"",""2"",""2"""
"2,""3"",""3"",""2"",""2"""
"2,""2"",""3"",""3"",""2"""
"2,""3"",""3"",""2"",""2"""
"2,""2"",""3"",""2"",""2"""
"3,""3"",""3"",""3"",""1"""
"3,""2"",""3"",""3"","""""
"3,""3"",""2"",""1"",""1"""
"3,""2"",""3"",""3"",""1"""
"3,""3"",""3"",""2"",""1"""
"2,""2"",""3"",""2"",""2"""
"2,""2"",""3"",""3"",""2"""
"2,""3"",""3"",""3"",""3"""
"2,""3"",""3"",""2"",""3"""
"2,""3"",""3"",""3"",""3"""
"2,""2"",""3"",""3"",""3"""
"2,""3"",""3"",""3"",""3"""
"3,""2"",""3"",""3"",""3"""
"3,""3"",""3"",""3"",""3"""
"2,""3"",""2"",""3"",""3"""
"2,""3"",""3"",""2"",""3"""
"2,""2"",""3"","""",""3"""
"2,""3"",""3"",""3"",""3"""
"2,""3"",""3"",""3"",""3"""
"2,""3"",""3"",""2"",""3"""
"3,""2"",""3"",""2"","""""
"3,""3"",""3"",""3"",""3"""
"3,""3"",""3"",""3"",""3"""
"2,""2"",""3"",""2"",""2"""
"1,""2"",""2"",""2"",""2"""
"3,""3"",""3"",""3"",""1"""
"0,""3"",""3"",""3"",""1"""
"2,""3"",""3"",""1"",""2"""
"0,""1"",""2"",""1"",""2"""
"2,""1"",""3"",""3"",""2"""
"3,""3"",""3"",""2"",""2"""
"2,""2"",""2"",""2"",""2"""
"2,""2"",""3"",""3"",""3"""
"3,""3"",""2"",""3"",""3"""
"2,""3"",""3"",""3"",""3"""
"3,""3"",""3"",""3"",""3"""
"3,""3"",""3"",""3"",""3"""
"3,""3"",""3"",""3"",""3"""
"2,""3"",""3"",""2"",""3"""
"2,""3"",""3"",""2"",""3"""
"3,""3"",""3"",""3"",""3"""
"3,""3"",""3"",""3"",""3"""
"2,""2"",""3"",""3"","""""
"3,""3"",""3"",""2"",""3"""
"3,""3"",""3"",""3"",""3"""
"3,""3"",""3"",""3"",""3"""
"2,""3"",""3"",""3"",""3"""
"3,""3"",""3"",""3"",""3"""
"3,""3"",""3"",""3"",""3"""
"2,""3"",""3"",""3"",""3"""
","""","""","""","""""
"2,""3"",""1"",""3"",""4"""
"3,""3"",""3"",""3"",""4"""
"3,""2"",""3"",""2"",""4"""
"3,""3"",""3"",""3"",""4"""
"2,""2"",""3"",""2"",""4"""
"2,""3"",""3"",""3"",""4"""
"2,""2"",""2"",""2"",""4"""
"3,""1"",""3"",""1"",""4"""
"0,""1"",""0"",""2"",""4"""
"1,""3"",""2"",""2"",""4"""
"3,""2"",""3"",""3"",""4"""
"2,""3"",""2"",""3"",""4"""
"2,""3"",""3"",""2"",""4"""
"2,""2"",""3"",""2"",""4"""
"2,""3"",""2"",""2"",""4"""
"2,""2"",""3"",""2"",""4"""
"2,""2"",""3"",""2"",""4"""
"2,""3"",""3"",""3"",""4"""
"3,""3"",""3"",""3"",""4"""
"2,""3"",""3"",""2"",""4"""
"2,""3"",""3"",""2"",""4"""
"3,""2"",""2"",""1"",""4"""
"2,""3"",""2"",""2"",""4"""
","""","""","""",""1"""
"3,""3"",""3"",""2"",""1"""
"3,""3"",""3"",""3"","""""
"2,""3"",""3"",""3"",""1"""
"3,""3"",""3"",""3"",""1"""
"3,"""",""3"",""3"",""1"""
"3,""1"",""2"",""3"",""1"""
"2,""3"",""2"",""2"",""1"""
"2,""3"",""3"",""2"","""""
"3,""3"",""3"",""3"",""1"""
"3,""3"",""3"",""3"","""""
"3,""3"",""3"",""3"",""1"""
"3,""3"",""3"",""3"",""1"""
"2,""3"",""3"",""3"",""1"""
"2,""3"",""2"",""3"",""1"""
","""","""","""","""""
","""","""","""","""""
"3,""3"",""3"",""3"",""1"""
"3,""2"",""3"",""2"",""1"""
"2,""2"",""2"",""2"",""1"""
Does that help to reproduce? Kind regards :-)
Following How do I display gt tables created in a loop in RMarkdown Word output? which deals with the case of word output, one possible option would be to output your gt table using
as_latex()and to wrap the output in a latex code chunk.Using a minimal reprex using
ggplot2::mpgas example data: