Rmarkdown setting the position of kable
You can replace "hold_position" from claudius answer with "HOLD_position":
```{r}
kable(cars %>% filter(cars$speed>=23), caption = "Speed vs distance") %>%
kable_styling(latex_options = "HOLD_position")
```
As mentionned in the kableExtra package:
if you find hold_position is not powerful enough to literally PIN your table in the exact position, you may want to use HOLD_position, which is a more powerful version of this feature. For those who are familiar with TeX, hold_position uses[!h] and HOLD_position uses [H] and the float package.
ref: https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf
You may also want to control figure position by adding fig.pos='H' to the figure chunk header.
Kable printing table above heading in Rmarkdown
I found that by using the information in this answer I was able to solve the problem. As a complete noob to kable/latex I wasn't aware of how tables are positioned within the document.
By adding kable_styling(latex_options = "HOLD_position")
to the kable statement everything worked as required. There was also the odd part around making sure to use '\n'
in the correct places when outputting latex statements in the R code else it gets a little upset.
R Markdown, kable_styling(latex_options=HOLD_position) not working
Ok, let's solve it with the pure LaTeX ;)
---
title: "Test Table"
date: '2021-11-11'
header-includes:
- \usepackage{float}
- \usepackage{caption}
output:
pdf_document
---
```{r}
library(kableExtra)
tab1<- head(mtcars) %>% kbl()
```
\begin{table}[H]
\captionof{table}{Table, stay here!}
`r tab1`
\end{table}
In RMarkdown, why is the kable table printing below the header here?
As far as I can tell everything is working as intented. The problem is float management in LaTeX.
You can change the behaviour of the kable output by setting the argument latex_options
in kable_styling()
. E.g.
mtcars %>%
slice(1) %>%
stack() %>%
select(ind, values) %>%
kable(col.names = NULL) %>%
#Use either "hold_position" or "HOLD_position"
kable_styling(latex_options = "hold_position") -> my_table
hold_position
corresponds to h
, while HOLD_position
is H
, (read more about float options here), where H
is stricter than h
and forces the position of the float output to be at the position where it is in the code. Also see ?kable_styling
(latex_options
entry)
See similar question with answer here
RMarkdown: Color largest percentage in each row of Kable?
You are trying to take the maximum of character vectors (i.e. c("12.7%", "35.6%", "6.9%")
) and in R,
max(c("12.7%", "35.6%", "6.9%"))
#> [1] "6.9%"
and from ?max
and ?comparison
,
Character versions are sorted lexicographically, and this depends on the collating sequence of the locale in use: the help for ‘Comparison’ gives details.
Character strings can be compared with different marked encodings (see Encoding): they are translated to UTF-8 before comparison.
sort(c("12.7%", "35.6%", "6.9%"), decreasing = TRUE)
#> [1] "6.9%" "35.6%" "12.7%"
So, we need to convert them to numbers before comparing using readr::parse_number()
and to print the cell values with percent format we can use formattable::percent()
function.
---
title: "Color Max Percentage"
output:
html_document: default
pdf_document: default
---
```{r setup, include = F}
library(tidyverse)
library(knitr)
library(kableExtra)
options(knitr.table.format = "html")
df = data.frame(
x = c(1, 2, 3, 4, 5),
a = c("12.7%", "14.0%", "49.2%", "20.4%", "23.2%"),
b = c("35.6%", "19.0%", "9.1%", "25.5%", "11.2%"),
c = c("6.9%", "54.1%", "31.3%", "15.4%", "17.5%")
)
df <- df %>%
# adorn_totals('row') %>%
mutate(across(a:c, ~ readr::parse_number(.x) / 100)) %>%
rowwise() %>%
mutate(across(
a:c,
~ cell_spec(
formattable::percent(.x, digits = 1),
format = "html",
color = ifelse(.x == max(c_across(a:c)), "red", "blue")
)
))
df %>%
kable(escape = F) %>%
kable_styling()
```
R Aligning table content to decimal points in kable
Here is a first try:
---
output: pdf_document
header-includes:
- '\usepackage{siunitx}'
- '\newcolumntype{d}{S[table-format=3.2]}'
---
```{r}
library(tidyverse)
library(knitr)
library(kableExtra)
tribble(
~Variable_1,~Variable_2,
"13.5","4.4**",
"12.7***","1.2*",
"0.4","0.3***",
"2.3**","11.5**"
)%>%
kable(format = "latex", booktabs=T, escape = T, align = "d")%>%
kable_styling(position = "center", latex_options = "hold_position")
```
Related Topics
Renaming Multiple Columns with Dplyr Rename(Across(
Compute All Pairwise Differences Within a Vector in R
How to Rename Element's List Indexed by a Loop in R
Warning: Replacing Previous Import 'Head' When Loading 'Utils' in R
Drawing Non-Intersecting Circles
Filled and Hollow Shapes Where the Fill Color = the Line Color
How to Sort Data by Column in Descending Order in R
Getting the Error "Level Sets of Factors Are Different" When Running a for Loop
Setting Individual Y Axis Limits with Facet Wrap Not with Scales Free_Y
Function Composition in R (And High Level Functions)
Return a List in Dplyr Mutate()
Merging Data.Tables Based on Columns Names
"Could Not Find Function" in Roxygen Examples During Cmd Check
Error When Plotting Sf Object --- Error: Could Not Find Function "Geom_Sf"
How to Show Every Second R Ggplot2 X-Axis Label Value
Substitute a for B and B for a in a String
Testthat Fails Within Devtools::Check But Works in Devtools::Test