R Markdown 内联代码未执行 [英] R Markdown inline code not executed
问题描述
我有一个内联代码,在一行中用单个反引号括起来.然而,
I have an inline code enclosed with single backticks on a single line. However,
队列有 r echo = FALSE load("../data/cohort.rda") nrow(cohort)
subjects.
未执行,因此以 html 和 pdf 格式为我提供此输出:
is not executed and thus gives me this output in html and pdf:
队列有 r echo = FALSE load("../data/cohort.rda") nrow(cohort)
subjects.
我想要这个输出:该队列有 477
个主题.
I want this output: The cohort had 477
subjects.
当我排除 echo=FALSE
时,我收到此消息:
When I exclude echo=FALSE
, I get this message:
从第 33-35 行退出(Manuscript.Rmd)base::parse(text = code, srcfile = NULL) 中的错误:1:25:意外的符号1:加载(../data/cohort.rda")nrow^
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 ^
调用:... inline_exec -> withVisible -> eval -> parse_only ->执行停止
Calls: ... inline_exec -> withVisible -> eval -> parse_only -> Execution halted
推荐答案
内联 R 代码需要是单个 R 语句,您可以通过用括号 {} 将整个代码块括起来来实现并用分号分隔命令.我将一个名为 tmp
的 3 行数据框保存到文件 tmp.rda
中,用这一行渲染了一个 Rmd 文件
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
并得到预期的输出.
这篇关于R Markdown 内联代码未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!