Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Markdown inline code not executed

Tags:

r

r-markdown

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

like image 399
N Stenfors Avatar asked Nov 10 '16 04:11

N Stenfors


People also ask

How do I add an inline code in r markdown?

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.

How do I add an inline code in r?

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 ` .

Why is my RMarkdown not knitting?

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.

How do I get RMarkdown to show no results?

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.


1 Answers

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.

like image 82
Ben Bolker Avatar answered Sep 21 '22 13:09

Ben Bolker