Does anyone know how to place a formula, a (weird) character, or words in italic within a sentence of a footnote of a table?
I'm creating a pdf file with Rmarkdown and kableExtra. But stuff like $Y_{t-1}$ or $p < .001$ (since I want the p to be italic) does not work. Or should I really learn xtable?
The trick is 1. to escape latex code and special characters four times, e.g. \\\\frac, 2. to set option escape=FALSE in footnote().
---
title: "Untitled"
output: pdf_document
---
```{r tab}
library(knitr)
library(kableExtra)
df <- data.frame(v1=rnorm(6), v2=runif(6), v3=rbinom(6, 1, .33),
row.names=LETTERS[1:6])
kable(df, "latex", align="c", booktabs=TRUE) %>%
footnote(general=c("$a^2+b^2=c^2,$",
"$\\\\sigma^2=\\\\frac{1}{n-1}\\\\sum_{i=1}^n(x_i-\\\\bar{x})^2;$",
"1,000 \\\\$;", "100\\\\%."),
number=c("Hello\ there! \\\\textit{Hello\ there!}"),
footnote_as_chunk=TRUE,
escape=FALSE)
```
Yields:
Related
This works in a usual code chunk in R markdown:
m1_aov <- anova(m1)
m1_aov$`Sum Sq`[2] %>% round(3)
Unfortunately, using the latter in inline code breaks the knitr parser down
`r m1_aov$`Sum Sq`[2] %>% round(3)`
Indeed, it also breaks Stackoverflow.
I looked at this related question but could not infer a working solution to my problem. Any hint?
Expanding the comment with a working example:
---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
```{r}
a <- tibble::tibble(`a column` = 1:10) # using tibble to get a column name with a white space
m <- mean(a$`a column`)
```
Mean is `r m`
To me this looks like a neat trick because it avoids to include unecessary long code inside the text, and do not create the problem you are facing at the (small) cost of creating new objects.
The output:
I have a table in a Rmd file to print to pdf in which I need to add a dagger symbol into a column header. The basic test code is:
---
title: "Untitled"
author: "L. G. Hunsicker"
date: "4/29/2022"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F)
library(magrittr)
library(knitr)
library(kableExtra)
```
```{r test}
df <- data.frame(x = 1:10)
names(df)[1] <- 'maxCpep (ng/mL) †'
df %>% kbl()
```
I have tried several ways to get the dagger symbol to print, but with each effort, the outcome is that the code for the dagger symbol is simply printed as text or raises an error (with the backslash). I have tried using bquote, \dagger, \dagger, etc. I also tried to use a superscript "a" as an alternative. Again, kbl just prints the literal code text without substituting the required symbol. There must be a straight-forward way to insert special characters or math strings into a kable header, but I have been unable to find it.
Thanks in advance for any help with this issue.
Larry Hunsicker
You can directly add a Unicode escape:
df <- data.frame(x = 1:10)
names(df)[1] <- 'maxCpep (ng/mL) \u2020'
df %>% kbl()
Use intToUtf8 to get the special character.
names(df)[1] <- paste('maxCpep (ng/mL)', intToUtf8(8224))
The table prints nicely in markdown but is not present in the knitted html file. I noticed that it is classified as a list but don't know how to change it to an acceptable file type. The knitted output is not formatted as a table. I appreciate the help.
library("crosstable") #important package crosstable() function
library('dplyr')
library("flextable")
tbl1 = crosstable(mtcars2, c(1), by = 2) %>%
as_flextable(keep_id=FALSE)
print(tbl1)
According to ?print.flextable
Note also that a print method is used when flextable are used within R markdown documents. See knit_print.flextable().
Therefore, if we want to print in Rmarkdown, either use knitr::knit_print or remove the print as the ?knit_print.flextable documentation shows
You should not call this method directly. This function is used by the knitr package to automatically display a flextable in an "R Markdown" document from a chunk.
---
title: "Testing"
author: "akrun"
date: "09/12/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, echo = FALSE}
suppressPackageStartupMessages(library("crosstable")) #important package crosstable() function
suppressPackageStartupMessages(library('dplyr'))
suppressPackageStartupMessages(library("flextable"))
tbl1 = crosstable(mtcars2, c(1), by = 2) %>%
as_flextable(keep_id=FALSE)
# either use knit_print or remove the print wrapper
#knitr::knit_print(tbl1)
tbl1
```
-output
I am trying to modify the appearance of a table using kable and kableExtra packages in R Markdown knitting to PDF output.
In particular, I would like to remove all borders from the table and add only one horizontal line under the header and one vertical line next to the row names.
At the moment, I have this code:
---
header-includes: \setmainfont[Path = C:/Windows/Fonts/]{Arial}
\usepackage{colortbl}
\arrayrulecolor{white}
output:
pdf_document:
latex_engine: xelatex
word_document: default
---
```{r echo=FALSE, message=FALSE, warning=F, paged.print=TRUE}
library(kableExtra)
library(magrittr)
DATA<- data.frame(jen=c(1,2,3,4,5), feb=c(2,3,4,5,3), mar=c(0,2,4,1,2))
rownames(DATA)<-c("first","second","third","fourth","fifth")
kable(DATA, "latex")
```
Which generates this table:
Table
To delete all borders, I set default borders to be white as the answer to this question suggested.
Now, I would like to add an horizontal blue line under the header and a vertical blue line at the right side of the row names.
I can manage to get the horizontal line with the following:
---
header-includes: \setmainfont[Path = C:/Windows/Fonts/]{Arial}
\usepackage{colortbl}
\arrayrulecolor{white}
output:
pdf_document:
latex_engine: xelatex
---
```{r echo=FALSE, message=FALSE, warning=F, paged.print=TRUE}
library(kableExtra)
library(magrittr)
DATA<- data.frame(jen=c(1,2,3,4,5), feb=c(2,3,4,5,3), mar=c(0,2,4,1,2))
rownames(DATA)<-c("first","second","third","fourth","fifth")
kable(DATA, "latex") %>%
row_spec(0, extra_latex_after = "\\arrayrulecolor[rgb]{0,.275,.725}") %>%
row_spec(1:nrow(DATA), extra_latex_after = "\\arrayrulecolor{white}")
```
Obtaining this.
I would like to do the same with a vertical line but there is no such command as 'extra_latex_after' in the function 'column_spec'. It only accepts 'extra_css' commands which obviously do not work for a PDF output.
My goal is to get something like this.
I know I can get the same result with other packages for tables, but I am wondering if it is possible to get something like this using kable.
Does anyone know if there is a solution for this?
This is indeed not supported so far. You could make a feature request on github.
You can hack this using a regular expression to replace the alignment options in your tabular:
---
title: "Test"
date: 2019-02-13
output: pdf_document
---
```{r header, echo= FALSE, include = T, warning=F}
library(knitr)
tbl <- kable(mtcars[1:4, 1:3], format = "latex")
# here we search for the begin command and its options and replace them
gsub(pattern = "(begin\\{tabular\\})(\\{.*?\\})",
repl = "\\1{l|r !{\\\\color{red}\\\\vrule width 1pt} r|r|}",
x = tbl)
```
I am working on a project in R-markdown. I have been trying to get the 'n' to italicize but I keep getting error messages. Listed below is my latest attempt.
```{r, echo=FALSE, message=FALSE, warning=FALSE, results="asis"}
print(xtable(describe(PDS_Admin[3:15]), align="lccccccccccccc",
caption=paste("Descriptive Statistics for PDS Administrators", italic(n), "= 13")),
type="latex", caption.placement= "top", comment=F, latex.environments ="flushleft")
```
I know this seems like a mundane detail. Please help!
You can use LaTeX tags, such as \textit{} for italics, directly inside the caption:
```{r, results="asis"}
## Note the \\ that escapes the '\' character
xtable(mtcars[1:13,], align="lccccccccccc", type="latex",
caption="Built-in mtcars dataset for \\textit{n} = 13")
```