Putting Italics into an xtable caption - r

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")
```

Related

How can I create a conditional flexdashboard layout

The aim is to create a flexdashboard layout in an markdown file. File is in rows orientation and then the layout contains multiple rows one under another. The goal is to have one of the layout section and accompanying chunk not executed/displayed based on a predefined boolean condition. I was able to incorporate a boolean to not include the chunk code in output but can't find any results on a conditional layout.
Things to note the final result is a standalone file so shiny solutions are not possible. AFAIK
Current Result and needed result
What I've come up with so far just keeps the layout there with the title instead of removing everything.
the variable series35 is what is being used as a boolean to make the chunk not produce results. How can the
`row`
`--------------------------------------`
lines also be conditionalised (if that's a word) aka not create a new layout section when series35 is FALSE
row
-------------------------------------
###`r Title 1`
```{r, echo=FALSE, results='asis'}
chunk code
```
row
-------------------------------------
###`r Title 2`
```{r, echo=FALSE, results='asis', eval = series35}
chunk code (suppressed when series35 is FALSE)
```
row
-------------------------------------
###`r Title 3`
```{r, echo=FALSE, results='asis'}
chunk code
```
row
-------------------------------------
###`r Title 4`
```{r, echo=FALSE, results='asis'}
chunk code
```
row {data-height=50}
-------------------------------------
I ended up after much more searching finding a knitr question that gave me a solution
```{r, eval = series35}
asis_output("row")
asis_output("-------------------------------------")
asis_output("###`Title 2`")
```
```{r, echo=FALSE, results='asis', eval = series35}
chunk code (suppressed when series35 is FALSE)
```
It worked nicely as a quick addition. I didn't try joshpk's method as yet but it does seem like a potentially clean option.
You can wrap that section in comments that are controlled by your series35. Something like this (if you can provide a reproducible example then it will be easier to provide something that meets your requirements but hopefully this helps).
`r if(series35 == FALSE) {"\\begin{comment}"} else {NULL}`
###`r Title 2`
```{r, echo=FALSE, results='asis', eval = series35}
chunk code (suppressed when series35 is FALSE)
```
`r if(series35 == FALSE) {"\\end{comment}"} else {NULL}`

Is there a way to add latex specification in 'column_spec' function in R kable?

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)
```

How to generate rmarkdown chunk within a chunk

I'm trying to generate a rmarkdown chunk using code. I've read similar questions and their solutions, such as using pander or using cat. I just can't seem to generate it. I also tried knitting the output manually. Here's an example of a Rmd file:
---
title: "test"
output: pdf_document
---
## R Markdown
```{r, results='asis',echo=FALSE}
txt <- paste("```{r}",
"2+2",
"```")
pander::pander(txt)
```
When I knit this, I get a verbatim {r} 2+2. I would like to see the number four instead. In my real example, I'm using bookdown and trying to generate a block2 chunk.
Any ideas how to generate this chunk that gets evaluated as R code?
Does this do what you want?
## R Markdown
```{r, results='asis',echo=FALSE}
txt <- paste("```{r}",
2+2,
"```")
pander::pander(txt)
```
This evalutates to
```{r} 4 ```
in your markdown document.
You using a string literal "2+2" as opposed to the expression 2+2. This is the first issue, I guess.
If you want it correctly parsed you need to add an sep = "\n" argument to paste or manually add the newline breaks.
I.e.
## R Markdown
```{r, results='asis',echo=FALSE}
txt <- paste("```{r}\n",
2+2,
"\n```", sep = "")
pander::pander(txt)
```
This evalutates to
```{r}
4
```
which is interpreted as R code in the markdown document.

Formula's or symbols in footnotes using knitr and kableExtra

Does anyone know how to place a formula, a (weird) character, or words in italic within a sentence of a footnote of a table?
I'm creating a pdf file with Rmarkdown and kableExtra. But stuff like $Y_{t-1}$ or $p < .001$ (since I want the p to be italic) does not work. Or should I really learn xtable?
The trick is 1. to escape latex code and special characters four times, e.g. \\\\frac, 2. to set option escape=FALSE in footnote().
---
title: "Untitled"
output: pdf_document
---
```{r tab}
library(knitr)
library(kableExtra)
df <- data.frame(v1=rnorm(6), v2=runif(6), v3=rbinom(6, 1, .33),
row.names=LETTERS[1:6])
kable(df, "latex", align="c", booktabs=TRUE) %>%
footnote(general=c("$a^2+b^2=c^2,$",
"$\\\\sigma^2=\\\\frac{1}{n-1}\\\\sum_{i=1}^n(x_i-\\\\bar{x})^2;$",
"1,000 \\\\$;", "100\\\\%."),
number=c("Hello\ there! \\\\textit{Hello\ there!}"),
footnote_as_chunk=TRUE,
escape=FALSE)
```
Yields:

Evaluating code but suppressing output in RMarkdown

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

Resources