将R放入块中

问题描述 投票:1回答:1

我是exams的新手,所以也许这个问题非常新手。

我无法将source外部R文件(包含可重复使用的功能)放入我的.Rnw

MWE:

functions.r

x <- 10

question.Rnw

<<echo=FALSE>>=
source('functions.r')
@
\begin{question}
  $x=\Sexpr{x}$
\end{question}

generate.r

library('exams')

exams2moodle('question.Rnw')

当我尝试Rscript generate.r时:

Loading required namespace: rmarkdown

Error:  chunk 1 
Error in file(filename, "r", encoding = encoding) : 
  cannot open the connection
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
  cannot open file 'functions.r': No such file or directory
Execution halted

在某些问题中如何重用自己的R功能?

r knitr sweave r-exams
1个回答
0
投票

所有练习均被复制到一个临时目录中,并在其中进行处理。因此,进行source()调用时,您位于其他目录中。因此,您需要将其包含在完整路径source("/path/to/functions.r")中-或者可以将文件复制到临时目录中。有一个便利功能include_supplement()用于执行后者。如果functions.rquestion.Rnw位于同一目录中,则只需执行以下操作:

include_supplement("functions.r")
source("functions.r")

question.Rnw开头的代码块中。

© www.soinside.com 2019 - 2024. All rights reserved.