Well I have a table in Rmarkdown but since I write the document in Greek I want also the Table word to be printed in Greek
```{r name, echo=FALSE,results='asis'}
kablegr %>%
kbl(caption = "**_Πίνακας_**") %>%
kable_classic(full_width = F, html_font = "Cambria")%>%
column_spec(1, bold = TRUE, border_right = TRUE, color = "black", background = "lightgrey")
```
what I want is instead o Table to print Πίνακας in each table of my document
Table: --> Πίνακας:
OPTION 1
This a solution that keeps the numbering automatically (following this answer. Nonetheless, I had to suppress the bold face.
---
title: "Untitled"
author: "bttomio"
date: "5/10/2021"
output: html_document
---
```{r, include=F}
library(captioner)
tables <- captioner(prefix = "Πίνακας")
```
```{r tab1, echo=FALSE}
library(kableExtra)
mtcars %>%
kbl(caption=tables("tab1", "Table Caption")) %>%
kable_classic(full_width = F, html_font = "Cambria")%>%
column_spec(1, bold = TRUE, border_right = TRUE, color = "black", background = "lightgrey")
```
```{r tab2, echo=FALSE}
library(kableExtra)
mtcars %>%
kbl(caption=tables("tab2", "Table Caption")) %>%
kable_classic(full_width = F, html_font = "Cambria")%>%
column_spec(1, bold = TRUE, border_right = TRUE, color = "black", background = "lightgrey")
```
-output
OPTION 2
You could try this (fig_caption: no in your YAML header):
---
title: "Untitled"
author: "bttomio"
date: "5/10/2021"
output:
html_document:
fig_caption: no
---
```{r name, echo=FALSE,results='asis'}
library(kableExtra)
mtcars %>%
kbl(caption = "**_Πίνακας_**") %>%
kable_classic(full_width = F, html_font = "Cambria")%>%
column_spec(1, bold = TRUE, border_right = TRUE, color = "black", background = "lightgrey")
```
-output
Related
My little example is as follows:
---
output:
word_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
```
```{r}
library(flextable)
set_flextable_defaults(
font.family = "Arial",
font.size = 9,
big.mark = "",
theme_fun = "theme_vanilla"
)
library(magrittr) ### for %>%
ft = head(mtcars[,1:5]) %>% flextable() %>%
set_caption(caption = "display a subset of columns for mtcars data") %>%
# align(part = "caption", align = "center") %>%
# font(part = "caption", fontname = "Calibri") %>%
# italic(italic = FALSE, part = "caption") %>%
add_footer_lines("also can be based on optional argument col_keys") %>%
font(part = "footer", fontname = "Consolas") %>%
bold(bold = TRUE, part = "footer")
ft
```
The Word Rmarkdown output shows the caption with left-alignment, Cambria-font and italic. I want to customize the caption part for the table with center-alignment, some-other-font and no-italic. However, the flextable package has no part for caption, with partname of the table (one of ’all’, ’body’, ’header’, ’footer’). How can I achieve the goal?
This is only available in the development version for now:
New features have been added recently:
paragraph settings
ability to define a complex paragraph with different formats for chunks
---
output:
word_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
library(flextable)
library(officer)
set_flextable_defaults(
font.family = "Arial",
font.size = 9,
big.mark = "",
theme_fun = "theme_vanilla"
)
```
```{r}
ft = head(mtcars[,1:5]) |> flextable() |>
set_caption(
caption = as_paragraph(
as_chunk("display a subset of columns",
props = fp_text_default(font.family = "Courier", bold = FALSE)),
as_chunk(" for mtcars data",
props = fp_text_default(color = "red", font.family = "Helvetica", bold = TRUE))
),
fp_p = fp_par(text.align = "center", padding = 5)
) |>
add_footer_lines("blah blah blah") |>
font(part = "footer", fontname = "Consolas") |>
bold(bold = TRUE, part = "footer")
ft
```
to install the current dev version:
remotes::install_github("davidgohel/officer")
remotes::install_github("davidgohel/flextable")
I would like to include formulas, with greek letters and subscripts, in the body of a flextable in an R Markdown HTML document. My code below is as close as I can get.
---
title: "html"
date: "`r Sys.Date()`"
output:
html_document:
df_print: paged
---
```{r setup, echo = FALSE, message=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(flextable)
library(dplyr)
```
```{r make-table}
epi <- data.frame(label= c("Exposured", "Not Exposed"),
Yes = c("π_11", "π_21"),
No = c("π_12", "π_22"))
theHeader <- data.frame(
col_keys = colnames(`epi`),
line1 = c("", rep("Has Outcome", 2)),
line2 = c("", "Yes", "No")
)
flextable(epi, col_keys = theHeader$col_keys) %>%
set_header_df(
mapping = theHeader,
key = "col_keys"
) %>%
theme_booktabs() %>%
autofit(part = "all") %>%
align(align = "center", part = "all") %>%
merge_h(part = "header") %>%
align_nottext_col(align = "center")
```
It produces this:
Is there a sane way to print formulas in the body of a flextable?
I am writing lecture notes. So references (suggested readings) to support a solution would be greatly appreciated.
Please see the rmd file below. I'd like to angle some column headers and have widths be respected.
In the first table without row_spec() the widths are respected but when row_spec() is added to angle a few column headers the widths no longer hold. Any idea how to angle and have the widths remain?
rmd file
---
title: "Untitled"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(kableExtra)
```
```{r table1, echo= FALSE, comment = FALSE, message= FALSE, warning = FALSE,results='asis'}
dt <- mtcars[1:5, 1:6]
rownames(dt) = NULL
colnames(dt)= c("long col 1","long col 12","long col 13","long col 14","long col 15","long col 16")
kable(
dt,
format ="latex",
caption = "No angle",
booktabs = T,
longtable = T,
escape = F,
align = "c",
linesep=""
) %>%
kable_styling(latex_options = c("repeat_header" ), font_size=8)%>%
collapse_rows(columns = 1, latex_hline = "major", valign = "middle") %>%
column_spec(c(1,2,3,4,5,6), width = c("8em","5em","6em","3em","3em","3em" ) )
```
```{r table2, echo= FALSE, comment = FALSE, message= FALSE, warning = FALSE,results='asis'}
kable(
dt,
format ="latex",
caption = "With Angled columns",
booktabs = T,
longtable = T,
escape = F,
align = "c",
linesep=""
) %>%
kable_styling(latex_options = c("repeat_header" ), font_size=8)%>%
collapse_rows(columns = 1, latex_hline = "major", valign = "middle") %>%
row_spec(0, angle = c(rep(0,2),rep(90,2),rep(0,2) ) )%>%
column_spec(c(1,2,3,4,5,6), width = c("8em","5em","6em","3em","3em","3em" ) )
```
I have a table below and would like to apply ROW-level heat maps.
(1) Any idea how? Right now the heat map is the same for both values in the single row below.
(2) Is there a way to make the header for the group column NOT be angled 90 degrees? Right now all headers are angled but for the group column it be better without angle=90.
here is the rmd file.
---
title: "Untitled"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
library(kableExtra)
#d = data.frame(group= c("A","b"),cat1=c(2,50),cat2=c(100,2))
d = data.frame(group= c("A"),cat1=c(2),cat2=c(NA))
d = d %>%
mutate_if(is.numeric, function(x) {
cell_spec(x, "latex", bold = F, background = spec_color(x,option="C", begin=.5, end = 0.99))
})
```
```{r table , echo= FALSE, comment = FALSE, message= FALSE, warning = FALSE, fig.height=3, fig.width = 8}
kable(
d, format ="latex",
caption = "",
booktabs = T,
longtable = T,
escape = F ,
align = "c"
) %>% kable_styling(latex_options = c(
"striped",
"repeat_header"
)
)%>% row_spec( 0,angle = 90)
```
Note: Ideally it'd be good to have this done with the kableExtra functionality so they color schemes match exactley to other kableExtra tables.
I'm actually not sure how to get the desired color mapping from spec_color. As an alternative, in the code below I've generated the color mapping using the colorRamp function and a palette-generating function, which can be adjusted as desired. The code also deals with missing values by coloring them light gray.
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(viridis)
library(dplyr)
library(kableExtra)
set.seed(2)
d = data_frame(group = sample(LETTERS[1:5], 10, replace=TRUE), cat1=runif(10, 0, 100), cat2=runif(10, 0, 100))
d[5,2] = NA
max.val = max(d[ , sapply(d, is.numeric)], na.rm=TRUE)
#pal.fnc = colorRamp(viridis_pal(option="C")(2))
pal.fnc = colorRamp(c("red", "yellow", "green"))
d = d %>%
mutate_if(is.numeric, function(x) {
cell_spec(round(x,1), "latex", bold = F, color=grey(.3),
background = rgb(pal.fnc(x/max.val) %>% replace(., is.na(.), 200), maxColorValue=255))
})
```
```{r table , echo= FALSE, comment = FALSE, message= FALSE, warning = FALSE, fig.height=3, fig.width = 8}
kable(
d, format ="latex",
linesep="",
caption = "",
booktabs = T,
longtable = T,
escape = F ,
align = "c"
) %>% kable_styling(latex_options = c(
"striped",
"repeat_header"
)
)
```
Following this example, here's what I have
# iris
This section is about the iris dataset
```{r, echo=FALSE, message=FALSE, warning=FALSE}
kable(head(iris[, 1:2]), format = "html") %>%
kable_styling(bootstrap_options = c("striped", "hover"), full_width = FALSE, position = "float_right")
```
# mtcars
This section is about the mtcars dataset
```{r, echo=FALSE, message=FALSE, warning=FALSE}
kable(head(mtcars[, 1:2]), format = "html") %>%
kable_styling(bootstrap_options = c("striped", "hover"), full_width = FALSE, position = "float_right")
```
But the output looks like this:
How do make the mtcars section appear below the iris section?
I guess kableExtra doesn't offer such functionality yet.
You can however resolve to html, and do the following with little effort:
---
title: "hi"
author: "me"
date: "March 23, 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
library(knitr)
library(kableExtra)
options(knitr.table.format = "html")
```
```{r}
kable(head(iris[, 1:2])) %>%
kable_styling(bootstrap_options = c("striped", "hover"), full_width = FALSE, position = "float_right")
```
# iris
This section is about the iris dataset.
This could be a whole paragraph.
<p style="clear: both"></p>
```{r}
kable(head(mtcars[, 1:2])) %>%
kable_styling(bootstrap_options = c("striped", "hover"), full_width = FALSE, position = "float_right")
```
# mtcars
This section is about the mtcars dataset
The relevant bit is <p style="clear: both"></p>
Put text after the table, and also add line breaks. Since the output is in html, it seems like it doesn't know how large the table is, and uses simple HTML tags to wrap around. One solution, wrap it in a table with 100% width:
<table style="width:100vw">
```{r, echo=FALSE, message=FALSE, warning=FALSE}
kable(head(iris[, 1:2]), format = "html") %>%
kable_styling(bootstrap_options = c("striped", "hover"), full_width = FALSE, position = "float_right")
```
# iris
This section is about the iris dataset.
</table>
<table style="width:100vw">
```{r, echo=FALSE, message=FALSE, warning=FALSE}
kable(head(mtcars[, 1:2]), format = "html") %>%
kable_styling(bootstrap_options = c("striped", "hover"), full_width = FALSE, position = "float_right")
```
# mtcars
This section is about the mtcars dataset.
</table>