I have an inline code enclosed with single backticks on a single line. However,
The cohort had r echo = FALSE load("../data/cohort.rda") nrow(cohort)
subjects.
is not executed and thus gives me this output in html and pdf:
The cohort had r echo = FALSE load("../data/cohort.rda") nrow(cohort)
subjects.
I want this output: The cohort had 477
subjects.
When I exclude echo=FALSE
, I get this message:
Quitting from lines 33-35 (Manuscript.Rmd) Error in base::parse(text = code, srcfile = NULL) : 1:25: unexpected symbol 1: load("../data/cohort.rda") nrow ^
Calls: ... inline_exec -> withVisible -> eval -> parse_only -> Execution halted
Code results can be inserted directly into the text of a . Rmd file by enclosing the code with `r ` . The file below uses `r ` twice to call colorFunc , which returns “heat.
A code chunk usually starts with ```{} and ends with ``` . You can write any number of lines of code in it. Inline R code is embedded in the narratives of the document using the syntax `r ` .
If a chunk works in R but not when you knit, it is almost always because you've changed a variable in your global working environment not using code in a chunk. Try restarting your R session and running each chunk sequentially to make sure all your variable values are up to date.
You use results="hide" to hide the results/output (but here the code would still be displayed). You use include=FALSE to have the chunk evaluated, but neither the code nor its output displayed.
The inline R code needs to be a single R statement, which you can achieve by surrounding the entire code chunk with brackets {} and separating commands with semicolons. I saved a 3-row data frame named tmp
to file tmp.rda
, rendered an Rmd file with this line
There are `r {load("tmp.rda"); nrow(tmp)}` observations
and got the expected output.
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