4

I use the code below to insert text in rmarkdown.

```{r, results='asis', echo=FALSE, warning=FALSE, message=FALSE} 
  cat("#", "We", "\n")
```

It worked well and gave me the output

# We

However, when I inserted some R code in this chunk like:

```{r, results='asis', echo=FALSE, warning=FALSE, message=FALSE} 
x <- 1:100
mean(x)
cat("#", "We", "\n") 
}
```

then it gave me the output:

# [1] 50.5 # We   

In this case, We was no longer a header.

4
  • It gave you the same output using cat(mean(x)) Commented Feb 10, 2016 at 1:53
  • OK, cat doesn't work. Commented Feb 10, 2016 at 2:13
  • Just put cat('\n') by itself between mean(x) and cat('#', ... ). I'm not wholly sure why it works, but it works. Commented Feb 10, 2016 at 2:20
  • It works for me as well. Thank you very much. Commented Feb 10, 2016 at 2:25

1 Answer 1

5

As opposed to print, cat doesn't start a new line. As # only indicates a section header when it is placed at the beginning of a line, an additional \n is required in front of #:

cat("\n# We\n") 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.