I have created a table using the kableExtra package. Is there a way in which I can italicize the second sentence in the caption
library(tidyverse)
library(kableExtra)
head(iris) %>% kable() %>% kable_styling() %>%
footnote(general= 'Here is my footnote. I want this sentence in italics')
We can use the <i>/</i>
library(dplyr)
library(kableExtra)
head(iris) %>%
kable() %>%
kable_styling() %>%
footnote(general="Here is my footnote. <i>I want this sentence in italics</i>", escape = FALSE)
-output
Related
The {{gt}} package now includes a function as_word, which outputs the table as an OOXML string.
I would like to use this to insert tables into the word output of a Quarto document.
I thought this would work.
---
title: "Untitled"
format: docx
---
```{r}
library(gt)
gtcars %>%
dplyr::select(mfr, model) %>%
dplyr::slice(1:2) %>%
gt() %>%
tab_header(
title = md("Data listing from **gtcars**"),
subtitle = md("`gtcars` is an R dataset")
) %>%
as_word()
```
But the word output is just the OOXML string, like this. How can I make the table appear as a table in the quarto word output?
No need to print an OOXML string for this purpose I guess – it works with the print() method for docx output:
```{r, echo=F}
library(gt)
gtcars %>%
dplyr::select(mfr, model) %>%
dplyr::slice(1:2) %>%
gt() %>%
tab_header(
title = "Data listing from gtcars",
subtitle = "gtcars is an R dataset"
)
```
(There's some trouble with the markdown code though, probably a bug. I've removed md().)
How to insert escape characters into dataframe?
It will be useful for the formatting of text data into table packages.
We could be use one universal solution for every table package (my hope).
But now we should employ a lot of approaches for the every case (depends on libraries and output formates: pdf, html).
It is really uncomfortable!
For example, we have next data
df <- data.frame("names" = c("a long long long long long ... pretty long name of the our story","Here is also long, but a little shorter"))
We see a normal output
names
1 a long long long long long ... pretty long name of the our story
2 Here is also long, but a little shorter
But how we can achieve this output ("\n")?
names
1 a long long long long long
... pretty long name of the our story
2 Here is also long, but a little shorter
I promised to add a simple example:
knit to HTML, in PDF doesn't work
```{r warning = FALSE, echo = FALSE, message=FALSE, include=FALSE,}
library(flextable)
library(gt)
#data
df <- data.frame("names" = c("a long long long \n long long ... <br> pretty long name of the our story","Here is also long, but a little shorter"))
#table one
table3 <- flextable(df)
table3 <- set_table_properties(table3, layout = "autofit", width = .5)
table3
#table two
table4 <- gt(df) %>% fmt_markdown(columns = everything())
table4
```
<center>
<caption>Table in flextable </caption>
</center>
`r table3`
<center>
<caption>Table in gt </caption>
</center>
`r table4`
In the first case - works \n, in the second - /br/ and there are only two packages!
Don't add any line breaks to your text!! This is absolutely unnecessary. All you need is the skillful use of the kableExtra package. Note the code below is the content from the RMarkdown.
The document created with it can be found here.
Add everything from ## RMarkdown Start to ## RMarkdown End to RMarkdown
Of course, don't forget to remove the comment characters before the code blocks (#```)!!
## RMarkdown Start
---
title: "LongText"
author: "Marek Fiołka"
date: "30 11 2021"
output:
html_document: default
pdf_document: default
---
#```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
#```
Prepare data frame with long text
#```{r echo=TRUE, message=FALSE, warning=FALSE}
library(stringi)
library(tidyverse)
library(kableExtra)
n=50
df = tibble(
id = 1:n,
txt = stri_rand_lipsum(n)
)
#```
See the data frame in a typical display
#```{r}
print(df)
#```
View data frame formatted with Kable Extra for HTML
#```{r}
df %>% kbl() %>%
kable_styling()
#```
View data frame formatted with Kable Extra for PDF
#```{r}
df %>% kbl() %>%
kable_paper(full_width = F) %>%
column_spec(1, bold = T, border_right = T) %>%
column_spec(2, width = "30em")
## RMarkdown End
This way you can create not only html documents but also corresponding pdf documents. Below is a fragment of a document created in this format. Note that in this case you have to use kable_paper with column_spec instead of kable_styling.
Hope this is what you were looking for.
Update
## RMarkdown Start
---
title: "LongText"
author: "Marek Fiołka"
date: "30 11 2021"
output:
pdf_document: default
html_document: default
---
#```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
#```
Prepare data frame with long text
#```{r echo=TRUE, message=FALSE, warning=FALSE}
library(stringi)
library(tidyverse)
library(kableExtra)
n=10
df = tibble(
id = 1:n,
txt = stri_rand_lipsum(n)
)
#```
See the data frame in a typical display
#```{r echo=TRUE}
print(df)
#```
View data frame formatted with [kableExtra](http://haozhu233.github.io/kableExtra/) for HTML
#```{r}
df %>% kbl() %>%
kable_styling()
#```
View data frame formatted with [kableExtra](http://haozhu233.github.io/kableExtra/) for PDF
#```{r}
df %>% kbl() %>%
kable_paper(full_width = F) %>%
column_spec(1, bold = T, border_right = T) %>%
column_spec(2, width = "30em")
#```
View data frame formatted with [gt](https://gt.rstudio.com/index.html) (only for HTML)
#```{r}
library(gt)
df %>% gt %>%
fmt_markdown(columns = everything()) %>%
tab_options(table.width = px(400))
#```
View data frame formatted with [flextable](https://davidgohel.github.io/flextable/articles/overview.html) HTML & PDF
#```{r message=FALSE, warning=FALSE}
library(flextable)
df %>% flextable %>% width(2, width = 6, unit = "in")
#```
View data frame formatted with [huxtable](https://hughjonesd.github.io/huxtable/) HTML & PDF
#```{r message=FALSE, warning=FALSE}
library(huxtable)
df %>% as_hux %>%
set_width(1) %>%
set_right_border(everywhere, 1, brdr(3, "double", "grey"))
#```
## RMarkdown End
I'm trying to generate a report with each row of a tibble printing vertically in a table on its own page using a for loop. The tables are printing fine, but I want to put headers at the top of each page (and eventually, text). Since the line of code to print the header (which appears as a header in my own document but not in my reprex, not sure why) is above the line of code to print the table, using kable, I expect the header to print above the table. I suspect kable is doing something I don't understand behind the scenes but I cannot determine what.
What am I doing wrong, and how may I print headers at the top of each page?
I've provided below: a screenshot of the current output.
Relevant portion:
---
title: "kable-order-reprex"
author: "Rob Creel"
date: "6/4/2021"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(magrittr)
library(kableExtra)
```
```{r chunk1, echo=FALSE, results='asis'}
for (i in 1:nrow(mtcars)) {
# Generate Table
mtcars %>%
slice(1) %>%
stack() %>%
select(ind, values) %>%
kable(col.names = NULL) %>%
kable_styling() -> my_table
cat("\n\n\\pagebreak\n")
# Print Header
cat(paste0("## ", mtcars %>% rownames() %>% extract2(i)))
# Print Table
print(my_table)
cat("\n\n\\pagebreak\n")
}
```
Pic of mis-ordered output.:
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
I have drafted some dataframes with text inside that linebreaks are present in each cell, the dataframe is converted to flextable and then inserted into a powerpoint slide using officer. I found the line height a bit too much, I tried using the height_all function in flextable to make reduce the line height, but it is not working. Please find the sample code as below:
library(officer)
library(dplyr)
pptx.output.st00 <- read_pptx()
data(iris)
data.df <- head(iris) %>%
as_tibble %>%
mutate_all(.,as.character) %>%
mutate_all(.,~paste0(.,'\ntesting'))
pptx.tbl <- data.df %>%
flextable %>%
height_all(height = 0.01) # this line is not working
pptx.output.st01 <- pptx.output.st00 %>%
add_slide(.,layout = 'Title and Content',master = 'Office Theme') %>%
ph_with(.,value=pptx.tbl,location=ph_location(type='body'))
print(pptx.output.st01,'presentation.output.pptx')
Currently I need to manually change the paragraph settings for table as the screen capture below:
Is there a way in officer of flextable to set up line height for table? Thanks!
I didn't satisfied by this ad hoc way, but padding(padding.top = 0, padding.bottom = 0.5) and height_all(0.45) gives a bit better output.
pptx.output.st01 <- pptx.output.st00 %>%
add_slide(.,layout = 'Title and Content',master = 'Office Theme') %>%
ph_with(.,
value=pptx.tbl %>%
padding(padding.top = 0, padding.bottom = 0.5) %>%
height_all(0.45),
location=ph_location(type='body'))
If I knit a document to md using kable("html") for a table and load it up to GitHub the bold header will not be rendered after the chunk.
```{r}
library(knitr)
library(dplyr)
iris %>%
group_by(Species) %>%
summarise(n = n()) %>%
kable("html")
```
Some Header (This is not bold in the final document: used ** **)
... next chunk.
```[r}
iris %>%
distinct(Species)
````
Some Header 2 (This is bold in the final document: used ** **)
You mean a code chunk like this:
knitr::kable(head(iris),caption = "iris dataset",format = "html")
bold
Also:
-kable is part of knitr (or perhaps kableExtra), not a package in itself. Please check.
-Check extra ")" in summarise(n = n))