Evaluating code but suppressing output in RMarkdown - r

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

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

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

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.

R inline markdown

I’m using R Markdown in RStudio to create a report that mixes Markdown and R output. I know how to use inline R expressions in the Markdown, but I’m wondering how to do the converse, i.e., use Markdown within the R code. I want to loop through a series of calculations and get Markdown headings for each one. I want headings for formatting purposes (e.g., bold titles etc) and also to be able to specify (sub)sections in the resulting PDF (which is a nice feature of the way RMarkdown handles # and ## etc).
I know I can do the following:
---
title: "test"
output: pdf_document
---
#Section 1
```{r, echo=FALSE}
print(1+1)
```
#Section 2
```{r, echo=FALSE}
print(2+2)
```
#Section 3
```{r, echo=FALSE}
print(3+3)
```
Which gives something looking (roughly) like this:
Section 1
## [1] 2
Section 2
## [1] 4
Section 3
## [1] 6
Is it possible to achieve the same output using something along the lines of this:
---
title: "test2"
output: pdf_document
---
```{r, echo=FALSE}
for (i in 1:3)
{
print(paste("#Section",i))
print(i+i)
}
```
As #scoa pointed out, you have to set the chunk option results='asis'. You should also place two \n both before and after your header.
---
title: "test"
output: pdf_document
---
```{r, echo=FALSE, results='asis'}
for (i in 1:3) {
cat(paste0("\n\n# Section", i, "\n\n"))
print(i+i)
cat("\n\n\\newpage")
}
```
As a more general answer, it could be useful to a look at the markdownreports package that parses markdown code (and output) from your R variables.

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