Insert text with R-code in markdown

I am using the following code to insert text in rmarkdown.

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

He worked well and gave me a way out

# We

However, when I inserted some R code into this snippet, for example:

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

then he gave me the conclusion:

# [1] 50.5 # We   

In this case, is Weno longer the title.

+4
source share
1 answer

In contrast print, catdoes not start a new line. Since #only the section heading indicates when it is placed at the beginning of the line, #an additional one is required before \n:

cat("\n# We\n") 
+5
source

Source: https://habr.com/ru/post/1628050/


All Articles