josefc95
josefc95

Reputation: 65

Simple question: how to add text to r chunk in rmd format?

I have this code outside of a chunk. This gives me and html output of the number of participants, number of male participants, number of female, and other in a neat html output.

`r length(Participant_Overview_Sequential$participant_id)` participants
`r length(which(Participant_Overview_Sequential$SEX == "1"))` male, 
`r length(which(Participant_Overview_Sequential$SEX == "2"))` female,
`r length(which(Participant_Overview_Sequential$SEX == "3"))` other

I've been trying to put this inside of an r chunk in rmd format, so this would be done by removing the backticks but I can't get the html output to be the same because I dont know how to add the text and the output of the numbers comes with a [1].

I would basically like to know how to get the same html output by putting that into a chunk!

Thank you

Upvotes: 2

Views: 848

Answers (1)

Peter
Peter

Reputation: 12699

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

Upvotes: 1

Related Questions