Format text inside R code chunk
You are using results='asis', hence, you can simply use print()
and formatting markup. If you want your text to be red, simply do:
```{r,results='asis', echo=FALSE}
print("<div class='red2'>")
rd <- sample(x=1e6:1e7, size = 10, replace = FALSE)
cat(rd, sep = "\n")
print("</div>")
```
Hope it helps.
r markdown - format text in code chunk with new lines
The trick is to use two spaces before the "\n"
in a row:
So replace "\n"
by " \n"
Example:
```{r, results='asis'}
text <- "this is\nsome\ntext"
mycat <- function(text){
cat(gsub(pattern = "\n", replacement = " \n", x = text))
}
mycat(text)
```
Result:
P.S.: This is the same here on SO (normal markdown behaviour)
If you want just a linebreak use two spaces at the end of the line you want to break
Simple question: how to add text to r chunk in rmd format?
Here is one way of getting the same output in-line and from a chunk. Probably not the most efficient coding but proves the concept.
---
title: "code inline and in chunk"
output: html_document
---
in-line
`r length(mtcars$gear)` participants
`r length(which(mtcars$gear == 3))` male,
`r length(which(mtcars$gear == 4))` female,
`r length(which(mtcars$gear == 5))` other
in chunk
```{r, results='asis'}
cat(paste(length(mtcars$gear), "participants \n"))
cat(paste(length(which(mtcars$gear == 3)), "male, \n"))
cat(paste(length(which(mtcars$gear == 4)), "female, \n"))
cat(paste(length(which(mtcars$gear == 5)), "other \n"))
```
Link to image of rmarkdown html output
Option for plain text formatting of Rmarkdown chunk output?
Setting knitr::opts_chunk$set(comment = "")
prevents knitr
and highlight.js
from formatting the chunk output as comments and therefore no comment colouring is applied.
Chunks in text Rmarkdown - Xaringan
Following the example in the "Ninja" presentation template, you can produce "raw chunks" by using the markdown
formatting with some tricks:
# Test
````markdown
`r ''````{r, echo = TRUE, eval=TRUE, out.width = '50%', fig.align = 'center'}
url <- 'https://media.springernature.com/full/springer-static/\nimage/art:10.1186%2Fs13059-020-02088-y/MediaObjects/13059_2020_2088_Fig1_HTML.png'
knitr::include_graphics(url) #<<
```
````
Output:
```{r, echo = TRUE, eval=TRUE, out.width = '50%', fig.align = 'center'}
url <- 'https://media.springernature.com/full/springer-static/\nimage/art:10.1186%2Fs13059-020-02088-y/MediaObjects/13059_2020_2088_Fig1_HTML.png'
knitr::include_graphics(url) #<<
```
Line highlighting is done with #<<
. For this, you need to enable the highlighting in the yaml header (again, I've taken the default from the Ninja template):
output:
xaringan::moon_reader:
lib_dir: libs
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
Format Code As a Code Block in R Markdown
One can use backticks or indenting by 4 spaces to generate the equivalent of the HTML <code>
</code>
tag block. Here is an Rmd document illustrating this behavior.
---
title: "Sample Document"
author: "bigNumber"
date: "12/5/2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
here is some example code written in R Markdown. If we want to enter a code block we use the backtick, such as `inc ~ ed + occ`.
If we want a code block with multiple lines, indenting by 4 spaces also works
# first line of code
for(i in 1:100) {
# do something here
}
More text after the code block ends.
...and the output. Unfortunately I have to include an image to show that it renders correctly.
output markdown in r code chunk
Why build the header markup (either in markdown or HTML) manually? Try inline R expressions or some helper functions in pander
(to generate markdown programatically):
---
title: "test"
author: "johndoe"
date: "September 5, 2015"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## `r 'This is a Heading in Code'`
```{r title, results='asis'}
library(pander)
pandoc.header("This is a Heading in Code", level = 2)
```
```{r cars, results='asis'}
summary(cars)
```
Related Topics
Configuration Failed Because Libcurl Was Not Found
Geom_Density to Match Geom_Histogram Binwitdh
Dual Y Axis in Ggplot2 for Multiple Panel Figure
Update a Column of Nas in One Data Table with the Value from a Column in Another Data Table
How to Calculate Mean of All Columns, by Group
Reduce File Size of R Markdown HTML Output
Convert Roman Numerals to Numbers in R
Gsub in R with Unicode Replacement Give Different Results Under Windows Compared with Unix
Display a Summary Line Per Facet Rather Than Overall
Convert/Export Googleway Output to Data Frame
"Adding Missing Grouping Variables" Message in Dplyr in R
Merge Dataframes on Matching A, B and *Closest* C
Format Text Inside R Code Chunk
Extract the Coefficients for the Best Tuning Parameters of a Glmnet Model in Caret
Any Way to Force Fread() of Data.Table Not to Stop on Empty Lines