Home > database >  How can I adjust the style of the output from a .Rmd chunk?
How can I adjust the style of the output from a .Rmd chunk?

Time:01-19

Here is an example code chunk and it's output.

`{r example, message = F}
for (i in 1:5) {
  print(i)
}
`

enter image description here

I would like this to render in my output file without the border box, and without the leading ## [1]. Is that possible?

CodePudding user response:

This solution also removes the border box.

  • Use the r chunk comment option to remove the ## character.
  • Use cat() instead of print() to display the output without the R formatting [1]. You need to specify in cat() that you want newlines added.
  • One method to remove the border box would be to use css. You can use an external css file, or make a dedicated hidden chunk to specify it.
    • I noticed the code chunks and output chunks were both specified by the <pre> tag, with the code chunk being of class .r, and the output chunk being .hljs. These might change with different themes, but this selector worked for me. pre.hljs might work alternatively as a selector.

Below is a complete .Rmd file that can be knit to an html document

---
title: example.Rmd
output: html_document
---

```{css, echo = FALSE}
pre:not(.r) {
    border: 0px;
}
```

```{r, comment = ""}
for (i in 1:5) {
  cat(i, "\n")
}
```

CodePudding user response:

Use the following code

{r example, message = FALSE, comment = ''}
 for (i in 1:5) {
   cat(i, '\n')
}
  • Related