Home > front end >  quarto rmarkdown code block to only display certain lines
quarto rmarkdown code block to only display certain lines

Time:05-13

I have a .qmd / .rmd file that wants to show the output of block of code. The code block has a lot of lines at the beginning that I'd like to hide, in the example below I'd like the output to be the third line of code str(month) and output the result of str(month). I've attempted to edit the code block parameters, but it's giving me an error:

---
format:
  html: default
---

```{r}
#| echo: c(3)
month <- "July"

str(month)
```

Error:

7: #| echo: c(3)
            ~~~
8: month <- "July"
x The value c(3) is string.
i The error happened in location echo.

rmarkdown support files suggest something like this might be possible

CodePudding user response:

I don't know if I understood the question correctly. But you can choose to display only the code you want based on the index of the line inside specific chunk. Insert the number of index line you want to show inside the c() {r, echo = c()}

Your specific case

---
format:
  html: default
---

```{r, echo = c(2)}
month <- "July"
str(month) # line 2
```

Other Example:

---
format:
  html: default
---

```{r, echo = c(5,8)}
# Hide
month <- "July"

## Show code and output
str(month) # Line 5

## Show code and output
1 1 # Line 8

## Show just output
2 2

```
  • Related