R Markdown inline code not executed

拜拜、爱过 提交于 2019-12-06 02:13:28

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/40519890/r-markdown-inline-code-not-executed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!