Convert a "table1"-table to pdf-friendly format - r

I have a table with descriptive statistics I generated using the table1 package (I used it since it can produce descriptive statistics for both categorical and continuous variables). The issue I'm having is that table1 only produce tables in HTML-format. I'd like to transform my table1-table to a kable-table (or equivalent) so I knit my markdown to a pdf-file.
I tried the following code
library(table1)
library(kable)
tb <- table1(reformulate(colnames(df)), data=df, output = "latex")
kable(tb, caption = "Descriptive statistics")
But this produces an empty table looking like this:
Does anyone know how to solve this?

Related

How to print TukeyHSD to pretty table format in for example .pdf with R?

I can run ANOVA and TukeyHSD on my dataset, and the output can easily be saved to a txt format as such:
#AOV test
res.aov <- aov(variable_of_interest ~ Treatment*some_variable*some_parameter, data = dataframe)
#save output to text file
capture.output(TukeyHSD(res.aov), file="TUKEY_outfile.txt")
However, the output is pretty ugly in my opinion, and it takes a lot of time to get it into Excel and format it into a pretty format. Is there a way to directly print the outputs to a nice table format in for example excel or .pdf? I saw a LATEX solution, but it was unfortunately very complicated.
As a working example, the following code could be used:
fm1 <- aov(breaks ~ wool + tension, data = warpbreaks)
capture.output(TukeyHSD(fm1), file="example_tukey.txt")
Here is a possible solution. If you plan to move code/output/tables/graphics from R to pdf/html/latex format files regularly, you should invest in the time to learn Rmarkdown or Bookdown which are specifically designed exactly for this purpose. If you just want to export a few tables to html or latex, the package xtable may work for you. The following code uses the example on the TukeyHSD manual page to illustrate producing an html file containing three tables:
fm1 <- aov(breaks ~ wool * tension, data = warpbreaks)
TK <- TukeyHSD(fm1)
print(xtable(TK$wool), type="html", file="TukeyTables.html")
cat("<p></p>", file="TukeyTables.html", append=TRUE)
print(xtable(TK$tension), type="html", file="TukeyTables.html", append=TRUE)
cat("<p></p>", file="TukeyTables.html", append=TRUE)
print(xtable(TK$'wool:tension'), type="html", file="TukeyTables.html", append=TRUE)
This code produces an html file with three tables will look something like the figure. There are many arguments in xtable function that give you substantial control over the formatting, but the package is really designed for latex.
.

How to make tables in Rmarkdown as Word

I am trying to include a table in Rmarkdown that it looks like in Word. This is how I have the table in Word:
Is there a way that I can include this table exactly as the image in a Rmardown document?
Get a screenshot of the table from the document and use
![Caption for the picture.](/path/to/your/folder/imagename.png)
If we want to create a table from a model object use gtsummary or flextable
library(gtsummary)
library(flextable)
data(trial)
model <- glm(response ~ trt, trial, family = binomial)
tbl_regression(model)
as_flextable(model)
Based on the format showed, crosstable would also work
library(crosstable)
crosstable(ggplot2::mpg, class, by = drv) %>%
crosstable::as_flextable()

R Sweave: output a very large table with linebreaks at the end of the page

I'm using R Sweave and wanted to begin my document with showing a sample of my table. My problem is, that my table has 39 variables and many rows. For the rows it isn't a problem, I can take only a few ones using sample_n, but I need to habe all my variables visible. It would sadly not fit either on a landscape sheet. I'm using xtable to generate my table. I think the easier way would be to put so much variables as possible on the sheet, then begin with the rest under, and so on, until it is all displayed.
Here some minimalist exemple:
dat <- bind_cols(mtcars, mtcars, mtcars, mtcars)
a <- as.data.frame(dat) %>%
sample_n(5)
print(xtable(a))
I've already know the longtable function, but it would only help me if I had too much rows, and not too much columns, isn't it? I'm still a little bit lost with having at the same time R and LaTeX on the same file...
An answer using my huxtable package. Create the table, then break it up by columns:
library(huxtable)
dat <- sample_n(as.data.frame(bind_cols(mtcars, mtcars, mtcars, mtcars)), 5)
ht <- as_hux(dat, add_colnames = TRUE)
# now format to taste:
bold(ht)[1,] <- TRUE
ht[,1:5] # first 5 columns. Will print as LaTeX within a Rmarkdown document

How to split kable over multiple columns?

I am trying to produce a "longitudinal" layout for long tables in RMarkdown with kable. For example, I would like a table to be split over two columns, like in the example below:
dd <- data.frame(state=state.abb, freq=1:50)
kable(list(state=dd[1:25,], state=dd[26:50,]))
However, this hack produces an output that looks a way worse than the normal kable output (for example the header is not in bold). Is there a "proper" way to do this using kable?
kable is a great tool, but has limits. For the type of table you're describing I would use one of two different tools depending on output wanted.
Hmisc::latex for .Rnw -> .tex -> .pdf
htmlTable::htmlTable for .Rmd -> .md -> .html
Here is an example of the latter:
dd <- data.frame(state=state.name, freq=1:50)
dd2 <- cbind(dd[1:25, ], dd[26:50, ])
library(htmlTable)
htmlTable(dd2,
cgroup = c("Set 1:25", "Set 26:50"),
n.cgroup = c(2, 2),
rnames = FALSE)
You can still use Kable with a slight modification to your code.
dd <- data.frame(state=state.abb, freq=1:50)
knitr::kable(
list(dd[1:25,], dd[26:50,]),
caption = 'Two tables placed side by side.',
booktabs = TRUE
)
This code is a modification of this. You can also find more information about tables on that page

How to change column heading using xtable in R?

I have the following piece of code:
tableData <- head(original_table[,c("column1",
"column2",
"column3")])
library(xtable)
xt <- xtable(tableData)
print(xt,type="html")
The 'original_table' object is a table where the columns have very awkward names which I do not want in the final output from printing the xtable.
I have a lot of code using the 'original_table' object which comes after the xtable is created. So I do not want to change the column headings in the original table.
How can I change the column headings using xtable so they can appear as something like 'Height','Width' and 'Breadth' in my table output?
xtable inherits data.frame.
So,
library(xtable)
xt <- xtable(tableData)
names(xt) <- c('Height','Width','Breadth' )
will work.

Resources