I looked this question but the given method does not seem work with footnotes (See MWE). I wonder how to add hyperlink in table footnotes using kableExtra package.
knitr::kable(
x = mtcars[1:4, 1:5]
, format = "latex"
, caption = "Table Caption with hyperlink[note]"
, escape = FALSE
) %>%
kableExtra::add_footnote("\\href{https://github.com/haozhu233/kableExtra}{kableExtra}")
With kableExtra > 0.5.0, you can use escape in footnote.
library(kableExtra)
knitr::kable(mtcars[1:4, 1:5],
format = "latex",
caption = "Table Caption with hyperlink[note]",
escape = FALSE
) %>%
add_footnote("\\href{https://github.com/haozhu233/kableExtra}{kableExtra}",
escape = F)
Related
I'm using kableExtra with a github_document and my underscore is not outputting correctly.
Before I tried to escape it, the table was outputting as so:
I then tried to see if I could get rid of the backslashes by escaping the underscore with no luck.
I've tried to escape with \_ \\_ and \\\\_ all of which don't give me the desired result. Using:
\_ R says that it isn't a valid escape command
\\_ prints \_ in my table
\\\\_ prints \\\_ in my table.
My YAML:
---
output: github_document
always_allow_html: true
---
Chunk options:
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
sample code:
names_table <- data.frame(name = c("Heat Index", "Heat Index Extreme Caution", "Heat Index Dangerous", "Temperature over the 95th percentile"), df_name = c("heat\\_index", "heat\\_index\\_ec", "heat\\_index\\_dan", "temp\\_over\\_95\\_pctl"), calculation = c("Heat index calculation", "Temperature between 90-102F", "Temperature between 103-124F", "Temperature exceeds historic 95th pctl"))
names_table %>%
knitr::kable(col.names = c("Variable Name",
"Column Name",
"Variable Calculation"),
escape = TRUE,
format = "html") %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"),
full_width = F,
position = "center") %>%
column_spec(3, width = "20em")
If I run the code without kable_styling, it runs correctly (i.e. the output is what I want) but if I knit the document, my table still prints with an unwanted backslash.
I've scoured SO with no luck of finding anything to help, but maybe I'm misunderstanding some help somewhere.
Edit: added image of problem, added more information about how the problem arose.
I was able to find a work around for my problem by saving the table as an image and inserting the table into my markdown as an image. Doesn't solve the issue of the output of github_document adding backslashes in front of underscores. Continuing from above:
kable_out <- names_table %>%
knitr::kable(col.names = c("Variable Name",
"Column Name",
"Variable Calculation"),
escape = TRUE,
format = "html") %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"),
full_width = F,
position = "center") %>%
column_spec(3, width = "20em")
save_kable(kable_out, "man/figures/kable_table.png", zoom = 5)
# zoom preserves image quality
knitr::include_graphics("man/figures/kable_table.png")
I've created a table in RMarkdwon (PDF) with the knitr and kableExtra packages.
I can choose in the kable-function between left,center or right- alignments.
But I want that the content of the table cells are aligned to the decimal points.
In this case it is difficult since there are asterisks behind the numbers indicating the significance.
Here is a reproduceable example:
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)%>%
kable_styling(position = "center", latex_options = "hold_position")
Which produces this table:
Can someone give me an easy solution directly in R?
If this should not be possible, how would an edit in the latex code look like?
Many thanks in advance!
Here is the table provided by bttomio:
One solution could be intersing the variable names in {} recommended by https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf page 28.
The code then looks like this:
tribble(
~Variable_1,~Variable_2,
"13.5","4.4**",
"12.7***","1.2*",
"0.4","0.3***",
"2.3**","11.5**"
)%>%
rename_all( ~ str_c("{ ",.," }"))%>%
kable(format = "latex", booktabs=T, escape = T)%>%
kable_styling(position = "center", latex_options = "hold_position")
And the table corresponds to the desired output:
But the problem is the full-width-option in the kable_styling-function. If I set this option to TRUE the linebreak of large column names disappears.
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")
```
When I include the £ sterling symbol in text when using knitr and kableExtra this displays as <U+00A3> in the output.
Is there any way I can get £ symbol to display with kableExtra and knitr?
I am using R 3.6.0 and package versions knitr_1.29 and kableExtra_1.1.0.
Example code below:
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=FALSE, warning=FALSE, message=FALSE)
library(rmarkdown)
library(knitr)
library(tidyverse)
library(kableExtra)
library(xtable)
```{r make a table, results='asis'}
# input metric
MI_input1 <- data.frame(cbind(c("£15.5m",""),c("£32.3m","157%"),c("","")))
# create MI table
MI_table1 <- kableExtra::kable(MI_input1, col.names = NULL, align = "c") %>%
kable_styling("bordered", full_width = F) %>%
pack_rows("Value in £", 1, 2, label_row_css = "text-align: center") %>%
column_spec(1:3, width = "1.5cm") %>%
row_spec(1:2, align = "c")
MI_table1
I want co change column width in pdf with kable ( , 'latex') but the fucntion doesn't work. Anybody know why? here is my code:
table = knitr::kable(jeden, "latex" , row.names = F , align = "llrrrrrrrrrr" , escape = F, booktabs = F, caption = '1. Sprzedaz uslug i towarow razem')
kableExtra::column_spec(table, 1, width = "1cm", bold = TRUE, italic = TRUE)
It's not a bug but rather a relatively strange setting for align in knitr::kable(). In xtable you can put align in a string but for kable, you will have to provide a vector. In your case, if you put things like align = c(rep("l", 2), rep("r"), 2), you should be fine.
It seems that align breaks your column_spec, but only for LaTeX/PDF output.
Here are two minimal & reproducible examples.
PDF output
---
title: "Untitled"
output:
pdf_document: default
---
```{r}
library(knitr)
library(kableExtra)
x <- kable(head(mtcars[, 1:4]), "latex", row.names = F, align = "llrr")
column_spec(x, 1:2, width = "4cm", bold = TRUE, italic = TRUE)
```
If you remove align from the PDF RMarkdown document, column_spec works as expected.
HTML output
---
title: "Untitled"
output:
html_document: default
---
```{r}
library(knitr)
library(kableExtra)
x <- kable(head(mtcars[, 1:4]), "html", row.names = F, align = "llrr")
column_spec(x, 1:2, width = "4cm", bold = TRUE, italic = TRUE)
```
This seems like a bug to me, and I would suggest opening an issue on the kableExtra GitHub site. If you do, you should reference this post, and include a minimal & reproducible example (similar to what I did).
I'm trying to produce table to a Latex-pdf document. I'm using kableExtra and knitr in R.
I have a table that has long column names. When I rotate the header row by 90 degrees linebreaks won't work. Does anyone have an idea how I could achieve both rotated row and linebreaks?
My example is the same as in Hao's Best Practice for newline in Latex table, but I added piped row_spec to the end of the code.
\documentclass[10pt,a4paper]{article}
\usepackage[table]{xcolor}
\begin{document}
\begin{table}
<<global_options, echo=FALSE>>=
library(kableExtra)
library("dplyr")
dt_lb <- data.frame(
Item = c("Hello\nWorld", "This\nis a cat"),
Value = c(10, 100)
)
dt_lb %>%
mutate_all(linebreak) %>%
kable("latex", booktabs = T, escape = F,
col.names = linebreak(c("Item\n(Name)", "Value\n(Number)"))) %>%
row_spec(0, angle = 90, align='l', monospace=T)
#
\end{table}
\end{document}
What I get is this, but the [l] tags hint that there's something else wrong with the tags as well:
On StackExchange TEX I found a question about rotation and linebreaks, this is what I'm trying achieve: https://tex.stackexchange.com/questions/14730/big-table-with-rotated-column-labels-using-booktabs
You could use tableHTML:
library(tableHTML)
Replace the new line character "\n" with the HTML tag <br>:
headers <- c("Item\n(Name)", "Value\n(Number)") %>%
stringr::str_replace_all(pattern = "\\n", replacement = "<br>")
Create a tableHTML object and rotate the headers using add_css_header():
dt_lb %>%
tableHTML(rownames = FALSE,
headers = headers,
escape = FALSE,
widths = c(100, 100),
theme = 'scientific') %>%
add_css_header(css = list(c('transform', 'height', 'text-align'),
c('rotate(-45deg)', '70px', 'center')),
headers = 1:2)
The result looks like this:
Note: you can add more css or default themes. Check out the vignettes for more details:
If you want to create a pdf document, you could use RMarkdown:
---
title: "tableHTML2pdf"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
\
```{r}
library(tableHTML)
dt_lb <- data.frame(
Item = c("Hello\nWorld", "This\nis a cat"),
Value = c(10, 100)
)
headers <- c("Item\n(Name)", "Value\n(Number)") %>%
stringr::str_replace_all(pattern = "\\n", replacement = "<br>")
dt_lb %>%
tableHTML(rownames = FALSE,
headers = headers,
escape = FALSE,
widths = c(100, 100),
theme = 'scientific') %>%
add_css_header(css = list(c('transform', 'height', 'text-align'),
c('rotate(-45deg)', '70px', 'center')),
headers = 1:2)
```
\
It will then create an HTML file. This file can then be converted to PDF using wkhtmltopdf.
I think you probably rendered your rmarkdown document into HTML... In the rmarkdown yaml header, does it say html_document right now? If so, you can try to change it to pdf_document.....
I rendered a pdf_document with exactly the same code and I think I got what you were looking for...
---
title: "Table Sample"
output: pdf_document
---
``` {r, include = FALSE}
library(tidyverse)
library(kableExtra)
```
```{r}
dt_lb <- data.frame(
Item = c("Hello\nWorld", "This\nis a cat"),
Value = c(10, 100)
)
dt_lb %>%
mutate_all(linebreak) %>%
kable("latex", booktabs = T, escape = F,
col.names = linebreak(c("Item\n(Name)", "Value\n(Number)"), align = "c")) %>%
row_spec(0, angle = 90)
```
Latex package makecell was missing.
Hao pointed out that Page 3 of this manual lists some of the necessary Latex packages: haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf