How to make tables in Rmarkdown as Word - r

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()

Related

using subscripts in gtsummary in R

Context
I am making a table and saving it into Microsoft Word with .docx file.
the table has a variable named PM2.5. I want to subscript 2.5 like PM2.5.
In this answear, I can use the grammar 'PM~2.5~' with as_kable() to use subscrpit in PM2.5.
But when I save the result (tab), it is a blank .docx file.
Question
How can I use subscripts in gtsummary and save it into .docx file?
Reproducible code
library(dplyr)
library(gtsummary)
df = data.frame(PM2.5 = 1)
tab = # make a table using gtsummary
df %>%
tbl_summary(label = PM2.5 ~ 'PM~2.5~') %>% # subscript in main table
modify_table_styling(columns = label,
rows = label == 'PM~2.5~',
footnote = 'PM~2.5~ in footnote') %>% # subscript in footnote
as_kable()
tab %>% flextable::save_as_docx(path = 'test.docx') # a blank .docx file
The reason it is blank is because you're using flextable::save_as_docx() to save the table. That function will only work with flextables...not knitr::kable() tables.
You can put this table in an R markdown or Quarto document with output type Word, and the table will appear.

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 apply code formatting to R Markdown inline code?

suppose I want to print a list of variables in my markdown file and I want to highlight them from the rest of the text using code style. For example using the names of the iris data
The variable names of the iris data set are `r paste0(names(iris), collapse = ", ")`
should return
The variable names of the iris data set are Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species
However, if I use this approach and try to include backticks inside the inline code with e.g.
The variable names of the iris data set are `r paste0(paste0("`", names(iris), "`"), collapse = ", ")`
or any version of trying to escape the backtick character that I could come up with ended with an error message.
Ok... After experimenting a bit I came up with a pretty close solution to what I was looking for:
backticks around the inline code
The variable names of the iris data set are ``r paste0(names(iris), collapse = ", ")``

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

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?

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

Resources