how do i display the line numbers of my code chunk with rmarkdown?
```{r}
x <- 1:10
y <- x^2
plot(x,y)
```
and i would like the echo to be something like
1 x <- 1:10
2 y <- x^2
3 plot(x,y)
Preferably like it is on Github...
Would be glad for any help
To break a line in R Markdown and have it appear in your output, use two trailing spaces and then hit return .
You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I ( Cmd + Option + I on macOS).
To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.
You can produce two code blocks: one for the presentation and another, hidden, for execution.
---
output:
pdf_document:
highlight: haddock
---
```{#numCode .R .numberLines}
x <- 1:10
y <- x^2
plot(x,y)
```
```{r results='asis', echo=FALSE}
x <- 1:10
y <- x^2
plot(x,y)
```
Note: If you replace pdf_document with html_document, you must provide the metadata "highlight".
Use the chunk option attr.source='.numberLines'
:
```{r, attr.source='.numberLines'}
if (TRUE) {
x <- 1:10
x + 1
}
```
This works for HTML and PDF.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With