使用考试文档类在 R-markdown 中运行代码块 [英] Running code chunk in R-markdown using exam document class
本文介绍了使用考试文档类在 R-markdown 中运行代码块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在考试文档类中的问题级别下运行代码块,但我一直收到错误消息.我假设这是因为它认为 R 代码的输出是 Latex 代码.
I would like to run a code chunk under a question level in the exam document class, but I keep receiving errors. I am assuming this is because it believes the output from the R-code is Latex code.
---
output: pdf_document
documentclass: exam
header-includes: \usepackage{float}
---
\begin{questions}
\question Answer question...
```{r}
iris%>%
group_by(Species)%>%
summarize(Total=n())
```
\end{questions}
推荐答案
有时 \begin{"some environment") ... \end{"some environment")
与R 块.一种解决方法是定义一个新环境.
Sometimes \begin{"some environment") ... \end{"some environment")
doesn't play well with R chuncks. One work around is to define a new environment.
例如,我定义了一个文件 preamble.tex,其中包含以下信息:
For example, I defined a file preamble.tex with the following information:
preamble.tex
\usepackage{float}
\newcommand{\bQ}{\begin{questions}}
\newcommand{\eQ}{\end{questions}}
然后,我运行了以下内容.
Then, I ran the following.
exam.Rmd
---
documentclass: exam
geometry: margin=.5in
output:
pdf_document:
highlight: haddock
includes:
in_header: preamble.tex
before_body: doc-prefix.tex
after_body: doc-suffix.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\bQ
## Including Plots
\question You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
```{r cars}
summary(cars)
```
\question We can keep the pound signs.
\eQ
这是结果输出.
这篇关于使用考试文档类在 R-markdown 中运行代码块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文