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