I have the following R Markdown document:
---
title: "Test"
output: html_document
---
```{r cars, echo=FALSE}
myCondition <- TRUE
if(myCondition) {
print("## Car Summary")
}
summary(cars)
```
When I Knit it to HTML, the "Car Summary" header is rendered in "terminal-like" monospaced font as this:
## [1] "## Car Summary"
But I want it rendered as a header. How do I achieve this?
In fact, you can take any R script and compile it into a report that includes commentary, source code, and script output. Reports can be compiled to any output format including HTML, PDF, MS Word, and Markdown.
include = FALSE prevents code and results from appearing in the finished file. R Markdown still runs the code in the chunk, and the results can be used by other chunks. echo = FALSE prevents code, but not the results from appearing in the finished file.
If you prefer to use the console by default for all your R Markdown documents (restoring the behavior in previous versions of RStudio), you can make Chunk Output in Console the default: Tools -> Options -> R Markdown -> Show output inline for all R Markdown documents .
This should work for you:
```{r cars, echo=FALSE, results='asis'}
myCondition <- TRUE
if(myCondition) {
cat("## Car Summary")
}
```
```{r, echo=FALSE}
summary(cars)
```
Note that the option results = 'asis'
is important to print the header. Also note that print()
will not work, but cat()
.
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