gtsummary table footnote missing in knitted pdf - r-markdown

I tried to knit my Rmarkdown report as PDF, and I noticed that the footnote is not there anymore. What did I miss?
stats_summary_table <-
dat %>%
tbl_summary(by = id,
missing = "no",
digits = list(all_continuous() ~ c(0, 0, 1, 1, 3)),
type = list(all_numeric() ~ "continuous"),
statistic = list(all_continuous() ~
"{min} ~ {max} {mean} ± {sd} [{cv}]")) %>%
modify_footnote(starts_with("stat_") ~ "Range and mean±SD [cv]")

PDF output with the gt package is still under development. Can you please confirm the footnotes work properly with PDF output using only the gt package? That will tell us if the issue lies within gt or gtsummary.
In the meantime, may I suggest you utilize one of the other print engines supported by the gtsummary package (gt is the default)
http://www.danieldsjoberg.com/gtsummary/articles/rmarkdown.html

Related

tableby in papaja with twocolumn classoption

I use the wonderful function tableby to have some summary statistics of my population and I also use the amazing package papaja to write my article.
Here is my code for the use of tableby.
DemoGroup <- tableby (Group ~ Age + education + logMAR + QIT + IRP + ICV + AQ + otherDiag, data=demoShort)
summary(DemoGroup,digits = 1, digits.p =3, booktabs = T, title = "(\\#tab:demo) Demo")
I need my document in a two-column fashion (and use the two-column classoption). However, when I try to knit my document, I have this error related to my table (everything works fine in a single column document):
I was unable to find any missing LaTeX packages from the error log 2022_MMN_FS_TSA_WO.log.
! Package longtable Error: longtable not in 1-column mode.
Error: LaTeX failed to compile 2022_MMN_FS_TSA_WO.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips.
Is there a way to fix this error?
Thank you for your help,
Chers,
Adeline

RMarkdown, knitr::kable shows "\begin{table}" after knitting to pdf document

One of cells in my RMarkdown document
```{r echo=FALSE}
head(data,3) %>% knitr::kable(caption = "Pierwsze 3 wiersze ze zbioru danych Lista_1.csv", digits = 2, booktabs = T)
gives weird result after knitting to pdf:
Of course there shouldn't be "\begin{table}" ,"\caption{}" and "\end{table}" parts. I use knitr::kable often and it never worked this way. Does anyone know how to fix it?
Edit: I have also noticed that all section headers (like "##Section2") below the table are centered. They shouldn't.
I've just found this question: How do I prevent kable from leaving raw latex in the final document if I include a caption in a table?
and used tip from comment (format = pandoc). It worked for me.

Knit a kable to PDF in RMarkdown that includes special characters in the table values

I'm trying to format a kable that includes the permyriad sign "‱". Permyriad means 1 out of every 10,000, so 1‱ = 0.01%.
I can get it to work with the special character σ, as in the screenshot and code below. Looking for a way to replace "σ" replaced with "‱".
I am pretty sure that there exists a magical combination of the three variables what_should_this_be, should_i_escape_or_not, and id_like_to_use_booktabs that will do the trick.
I'm doing this within RStudio using the tinytex package.
Here's what I've attempted so far:
The exact value for the variable what_should_this_be that results in knitting the ‱ sign in the final pdf. The Unicode value for "‱" is U+2031.
Values I've tried:
Combinations of "\textperthousand" with varying numbers of escapes, with and without brackets, with and without opening & closing $
Copy-pasting the ‱ symbol directly with varying numbers of escapes
"\U2031" with varying numbers of escapes
Various combinations with should_i_escape_or_not set to TRUE or FALSE.
I'd like to use booktabs... but that might be asking a bit much, so I've tried various combinations setting id_like_to_use_booktabs to TRUE or FALSE.
Various combinations of setting the "Typeset LaTeX into PDF using:" option in RStudio > Tools > Sweave
```{r, echo = FALSE}
library(magrittr)
what_should_this_be <- "$\\sigma$"
should_escape_or_not <- FALSE
id_like_to_use_booktabs <- TRUE
knitr::kable(
head(mtcars) %>%
dplyr::select(mpg) %>%
tibble::rownames_to_column("car") %>%
dplyr::mutate(mpg = paste0(mpg, what_should_this_be)),
align = "cc",
escape = should_escape_or_not,
booktabs = id_like_to_use_booktabs,
caption = "Works with character $\\sigma$, but what about permyriad?"
)
```
Could use the textcomp package and \textpertenthousand
---
output:
pdf_document:
latex_engine: xelatex
header-includes:
- \usepackage{textcomp}
---
```{r, echo = FALSE}
library(magrittr)
what_should_this_be <- "\\textpertenthousand"
knitr::kable(
head(mtcars) %>%
dplyr::select(mpg) %>%
tibble::rownames_to_column("car") %>%
dplyr::mutate(mpg = paste0(mpg, what_should_this_be)),
align = "cc",
escape = F,
booktabs = T,
caption = "Works with character $\\sigma$, but what about permyriad?"
)
```

R Markdown Table 1000 separator

I am trying to publish a table with 1000 separators and I am not having any luck with it. I followed the link here: Set global thousand separator on knitr but am not having much success.
My sample dataset is here: https://goo.gl/G7sZhr
The RMarkdown code is here:
---
title: "Table Example"
author: "Krishnan Viswanathan"
date: "August 4, 2015"
output: html_document
---
Load Data
{r, results='asis', message = FALSE, tidy=TRUE}
load("i75_from_flow.RData")
library(data.table)
{r, results='asis', echo=FALSE,message = FALSE, tidy=TRUE}
i75_from_flow <- i75_from_flow[order(-Tons),]
knitr::kable(i75_from_flow)
However, when I include this chunk of code (knit_hook$set) in the RMarkdown document, i get errors.
```{r, results='asis', echo=FALSE,message = FALSE, tidy=TRUE}
i75_from_flow <- i75_from_flow[order(-Tons),]
knit_hooks$set(inline = function(x) {
prettyNum(x, big.mark=",")
})
knitr::kable(i75_from_flow)
```
Error:
# object knit_hooks not found.
Any insights on what I am doing wrong and how to fix this is much appreciated.
Thanks,
Krishnan
The easiest is to use the format arguments of the kable() function itself, where you can specify the big number mark like so:
kable(df, format.args = list(big.mark = ","))
So your example would look like:
```{r, results='asis', echo=FALSE,message = FALSE, tidy=TRUE}
i75_from_flow <- i75_from_flow[order(-Tons),]
knitr::kable(i75_from_flow, format.args = list(big.mark = ","))
```
without any need for knitr hooks.
What about using pander with bunch of options to fine-tune your markdown table:
> pander::pander(i75_from_flow, big.mark = ',')
----------------------------
ORIGFIPS TERMFIPS Tons
---------- ---------- ------
12,023 12,117 5,891
12,119 12,105 4,959
12,001 12,057 3,585
12,001 12,113 3,083
12,047 12,047 1,517
----------------------------
The reason that the knit_hooks object is not found is that you either need to load the knitr package or use the knitr:: prefix in order to set the knit_hooks options. For example:
knitr::knit_hooks$set(inline = function(x) {
prettyNum(x, big.mark=",")
})

Finding and Excluding Columns of Dataframe with Certain Patterns in Header Names with R

I'm trying to find a solution to a data frame subsetting need I have.
I have a data frame that with the following general structure I have named wrkDat
A R2_A B R2_B ..... Z R2_Z
1.1 0.99 2.2 0.97 ..... 26.6 0.96
1.9 0.89 4.2 0.99 ..... 12.8 0.78
I would like to be able to selectively subset so I don't have the R2 columns.
Initially I was thinking something like the following would work, but it does not.
selected <- "^R2." %in% colnames(wrkDat)
wrkDat <- wrkDat[,!selected]
Thank you to Mitra and plafort for getting me thinking about the grep functions. I found the following that worked perfectly. I don't know what I was thinking trying to use regex patterns with %in%....
selected <- grepl("^R2.", colnames(wrkDat))
wrkDat <- wrkDat[,!selected]
wrkDat <- wrkDat[,-grep("^R2.",colnames(wrkDat)]
should do the trick
You could do:
library(dplyr)
df %>% select(-starts_with("R2"))