user2702330
user2702330

Reputation: 321

insert text with R Code in markdown

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.

Upvotes: 4

Views: 2550

Answers (1)

CL.
CL.

Reputation: 14997

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") 

Upvotes: 5

Related Questions