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")
```
I have a chuck
```{r}
Item <- melt(Item)
```
Which creates an output :
## Using as id variables
How do I evaluate the code but suppress this output.
{r, warning=FALSE, message=FALSE}
Item <- melt(Item)
Details are documented here : https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf
As known in RMarkdown code chunks can be named like this:
```{r chunkname}
plot(x,y)
```
Is it possible to showing chunkname in output document?
You can use knitr::opts_current$get()$label
example:
```{r cars}
library(knitr)
opts_current$get()$label
plot(cars)
```
It will also work outside of a chunk, in an inline r code. It will then output the label of the last chunk.
You can of course save the labels in a vector to use them later, for instance with a custom hook:
```{r knitr_setup}
library(knitr)
ll <- opts_current$get()$label
knit_hooks$set(label_list = function(before, options, envir) {
if(before) ll <<- c(ll,opts_current$get()$label)
})
opts_chunk$set(label_list=TRUE)
```
ll will then contain the list of chunk labels. However, you cannot access the names of chunks not yet ran.
Is it possible to conditionally evaluate a code chunk and its associated heading using R Markdown and knitr? For example, if eval_cell is TRUE include the chunk and its heading, but don't include either if eval_cell is FALSE.
```{r}
eval_cell = TRUE
```
# Heading (would like to eval only if eval_cell is TRUE)
```{r eval = eval_cell}
summary(cars)
```
You can put the heading in an inline R expression:
```{r}
eval_cell = TRUE
```
`r if (eval_cell) '# Heading (would like to eval only if eval_cell is TRUE)'`
```{r eval = eval_cell}
summary(cars)
```
This will become cumbersome if you have large blocks of text/code that need to be conditionally included, in which case you are recommended to put them in a separate child document, say, child.Rmd:
# Heading (would like to eval only if eval_cell is TRUE)
```{r}
summary(cars)
```
Then in the original (parent) document, you just need
```{r}
eval_cell = TRUE
```
```{r child='child.Rmd', eval=eval_cell}
```