How can I create a conditional flexdashboard layout - r

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

Related

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

Putting Italics into an xtable caption

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

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

Showing code chunk name in output in RMarkdown

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.

Conditionally evaluate heading using knitr in .Rmd file

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

Resources