I created a table with R table1 and want to save it as a png image with a line of code and not with the Export button in the R GUI interface. I need this so that I can upload the table to Rmarkdown using the papaja package which does not load and knit HTML tables correctly. I Had a look here: R: table1 output , but it does not solve my problem. I tried using htmlwidgetes and webshot too, but without success. Any advice appreciated.
Asked
Active
Viewed 1,774 times
1
-
2Does this answer your question: [How to capture html output as png in R](https://stackoverflow.com/questions/35056733/how-to-capture-html-output-as-png-in-r) – dcarlson Jul 06 '20 at 20:41
1 Answers
0
If you want to include the table created by the table1 package into a papaja document, there actually is a simple solution. Consider the example from the table1 documentation:
dat <- subset(survival::pbc, !is.na(trt)) # Exclude subjects not randomized
dat$trt <- factor(dat$trt, levels=1:2, labels=c("D-penicillamine", "Placebo"))
dat$sex <- factor(dat$sex, levels=c("m", "f"), labels=c("Male", "Female"))
dat$stage <- factor(dat$stage, levels=1:4, labels=paste("Stage", 1:4))
dat$edema <- factor(dat$edema, levels=c(0, 0.5, 1),
labels=c("No edema",
"Untreated or successfully treated",
"Edema despite diuretic therapy"))
dat$spiders <- as.logical(dat$spiders)
dat$hepato <- as.logical(dat$hepato)
dat$ascites <- as.logical(dat$ascites)
label(dat$age) <- "Age (y)"
label(dat$sex) <- "Sex"
label(dat$stage) <- "Histologic stage of disease"
label(dat$edema) <- "Edema status"
label(dat$spiders) <- "Blood vessel malformations in the skin"
label(dat$hepato) <- "Presence of hepatomegaly or enlarged liver"
label(dat$ascites) <- "Presence of ascites"
label(dat$platelet) <- "Platelet count (billions per liter)"
label(dat$protime) <- "Standardised blood clotting time"
label(dat$albumin) <- "Serum albumin (g/dL)"
label(dat$alk.phos) <- "Alkaline phosphotase (U/L)"
label(dat$ast) <- "Aspartate aminotransferase (U/mL)"
label(dat$bili) <- "Serum bilirubin (mg/dL)"
label(dat$chol) <- "Serum cholesterol (mg/dL)"
label(dat$copper) <- "Urine copper (microg/day)"
label(dat$trig) <- "Triglycerides (mg/dL)"
table1(~ age + sex + stage + edema + spiders + hepato + ascites +
platelet + protime + albumin + alk.phos + ast + bili + chol +
copper + trig | trt, data=dat)
The table1()
function invisibly returns an output object that may be further processed. This is possible either via knitr::kable()
or with papaja's apa_table()
function (or other table function, e.g. from kableExtra.
First, save the output of table1()
as an object:
output <- table1(~ age + sex + stage + edema + spiders + hepato + ascites +
platelet + protime + albumin + alk.phos + ast + bili + chol +
copper + trig | trt, data=dat)
Using knitr::kable()
yields a table that exceeds page margin, so here is an example with apa_table()
:
library(papaja)
# Restructure object
x <- attr(output, "obj")$contents
names(x) <- lapply(x, function(x){rownames(x)[[1L]]})
x <- lapply(x, function(x){x[-1L, ]})
# Use apa_table()
apa_table(x, caption = "Output from table1 in a pdf document.")

Marius Barth
- 596
- 2
- 9