Conditionally display a block of text in R Markdown
You need a complete R expression, so you cannot break it into multiple blocks like you show, but if the results of a block are a text string then it will be included as is (without quotes), so you should be able to do something like:
`r if(show.text){"la la la"}`
and it will include the text if and only if show.text
is TRUE
.
Conditionally display block of markdown text using knitr
You could use the asis
engine to conditionally include/exclude arbitrary text in knitr, e.g.
```{asis, echo=FALSE}
Some arbitrary text.
1. item
2. item
Change echo=TRUE or FALSE to display/hide this chunk.
```
But I just discovered a bug in this engine and fixed it. Unless you use knitr >= 1.11.6, you can create a simple asis
engine by yourself, e.g.
```{r setup, include=FALSE}
library(knitr)
knit_engines$set(asis = function(options) {
if (options$echo && options$eval) paste(options$code, collapse = '\n')
})
```
If you want to include inline R expressions in the text, you will have to knit the text, e.g.
```{r setup, include=FALSE}
library(knitr)
knit_engines$set(asis = function(options) {
if (options$echo && options$eval) knit_child(text = options$code)
})
```
Conditionally display block of text with code inside string section
Using paste
you could do:
Also note that there was a bug in your condition which I fixed.
---
title: "Untitled"
output: html_document
date: '2022-07-22'
---
```{r}
df <- data.frame(
column = 1:2
)
```
`r if (nrow( subset(df,column == 1)) != 0) paste("Multiple choice variable with", nrow(subset(df, column == 1)), "missing values.") else ""`
Conditionally evaluate heading using knitr in .Rmd file
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}
```
Display Block of R Code in Knitr With Evaluation Turned Off
Is this what you want to do, or have I misunderstood? The eval = FALSE
is in one code chunk and the second chunk still plots.
---
title: "A Test Knit"
output: html_document
---
## Show code but don't run
```{r, eval = FALSE}
summary(cars)
```
## Run and render plot
```{r}
plot(pressure)
```
quarto rmarkdown code block to only display certain lines
This does not work because you are using YAML syntax for chunk options as recommended with Quarto but #| echo: c(3)
is not valid YAML. #| echo: 3
is.
You can use !expr
within YAML field to parse R code if necessary. #| echo: !expr c(3)
would work. It is explained here: https://quarto.org/docs/computations/r.html#chunk-options
However, know that knitr supports other way to specify chunk options:
- Usual header where option needs to be valid R code
```{r, echo = c(3)}
#| echo: c(3)
month <- "July"
str(month)
```
- And a multiline version of it, useful when having long option like
fig.cap
```{r}
#| rmdworkflow,
#| echo = FALSE,
#| fig.cap = "A diagram illustrating how an R Markdown document
#| is converted to the final output document.",
#| out.width = "100%"
knitr::include_graphics("images/workflow.png", dpi = NA)
```
See the announced in blog post for more about this new syntax: https://yihui.org/en/2022/01/knitr-news/
This question has also been asked in Github - so more detailed answer there: https://github.com/quarto-dev/quarto-cli/issues/863
Conditionally display markup formatted text based on shiny input
The the following in your Rmd file. You already are in a code block and don't need to have additional inline r formatting. You can use results = 'asis'
in your block and also add the header (#) for identical formatting if you'd like:
```{r, echo=FALSE, results='asis'}
if(BoxValue == TRUE){
cat("# Value for input 2 is", Input2Value)
}
```
Related Topics
Plotting Cumulative Counts in Ggplot2
Creating a Facet_Wrap Plot with Ggplot2 with Different Annotations in Each Plot
How to Not Run an Example Using Roxygen2
Sort a Factor Based on Value in One or More Other Columns
Excel Cell Coloring Using Xlsx
How to Align a Group of Checkboxgroupinput in R Shiny
Difference Between Pull and Select in Dplyr
Digging into R Profiling Information
Fit a No-Intercept Model in Caret
How to Rotate an Image R Raster
Geom_Point() and Geom_Line() for Multiple Datasets on Same Graph in Ggplot2
R Fails After Installing Gtk and Rgtk2
How to Change the Na Color from Gray to White in a Ggplot Choropleth Map
Replace Na with Zero in Dplyr Without Using List()
How to Split a Data Frame by Rows, and Then Process the Blocks