Forum: support

Monitor Forum | Start New Thread Start New Thread
RE: exams2pdf + figures [ reply ]
By: Achim Zeileis on 2016-05-03 22:13

barplot_knitr.Rnw (3) downloads
Marek, thanks for this. I just committed a new version xweave() to the SVN that gained an engine=NULL argument. This can be changed to engine="knitr" to obtain rendering by knitr::knit(). With the attached updated barplot_knitr.Rnw you can then do:

exams2pdf("barplot_knitr.Rnw", engine = "knitr")

If we decide that this is a useful way forward, I will also enable the 'engine' argument in exams2html(), exams2moodle(), etc.

Personally, I always found this knitr hook business overkill for what you need in exercises. Rather than setting knit_hooks$set(inline = ...) inside the .Rnw I would just use a small convenience function, say, pn() that call prettyNum(...) with suitable arguments. And then you can do: \Sexpr{pn(so)}. For a very similar purpose the exams package provides the fmt() function that calls format(...) with custom parameters. The advantage of having a function rather than a hook is that it is somewhat easier to test outside knitr.

My updated barplot_knitr.Rnw file uses this fmt() function instead of your Sexpr_plain() hook-setting function.

Also, I simply uncommented the \ex*{...} metainformation. This makes all this nasty inline comment pattern business unnecessary. The comments in front of the metainformation commands are optional in exams, version 2.

RE: exams2pdf + figures [ reply ]
By: Marek Pomp on 2016-05-03 09:08

barplot_knitr.Rnw (4) downloads
there is some "minimal" R code, which redefine function xwave for using with knitr *.Rnw file (Rnw file is enclosed).
Best wishes

########## begin of R code

## replace Sweave by knit
env <- environment(xweave)
unlockBinding("xweave", env=env)
function (file, quiet = FALSE, encoding = NULL,
#envir = new.env(),
#pdf = TRUE, png = FALSE,
#height = 6, width = 6, resolution = 100,
ext <- tolower(tools:::file_ext(file))
if (is.null(encoding)) encoding <- ""
knit(file, encoding = encoding, quiet = quiet)
lockBinding("xweave", env=env)

## exclude pattern inline.comment from \Sexpr (necessary for META-INFO)
if (!exists("apats")) apats <- all_patterns
knitr_new_patterns <- apats[["rnw"]]
knitr_new_patterns <- knitr_new_patterns[
- which(names(knitr_new_patterns)=="inline.comment")

## necessary option for chunks
## redefine path for figures
fig.path=paste("", sep=""),
## write tex like Sweave

## others option for chunks

## special behavior of \Sexpr (one of my reason using knitr)
## for text
Sexpr_special <- function() {
knit_hooks$set(inline = function(x) {
prettyNum(x, big.mark="\\\\,",
## for META-INFO
Sexpr_plain <- function(){
knit_hooks$set(inline = function(x) {
prettyNum(x, format="f", digits=4)

########## end of R code

RE: exams2pdf + figures [ reply ]
By: Achim Zeileis on 2016-05-02 22:34
Marek, thanks for the testing.

As for xweave() and knit(): We still use Sweave() for .Rnw files because we always did and I suspected that in the context of exam generation the extra features offered by knit() over Sweave() are not that important. But apparently there are users like you that would like to use knit() for the .Rnw exercise files.

Could you (a) share the code you use for your xweave() - and (b) an .Rnw example exercises that requires knit() and doesn't work with Sweave()?

Then I'll have a look whether I can easily add an option in xweave() that supports this.

RE: exams2pdf + figures [ reply ]
By: Marek Pomp on 2016-05-02 10:57
thank you for yours works! All of that's going right, now.

Finally, I tested it with my redefinition of xweave <- function() {... Sweave...} to function(){...knit...} and everything is OK.
Best wishes

RE: exams2pdf + figures [ reply ]
By: Achim Zeileis on 2016-04-29 07:54

barplot.Rnw (3) downloads
OK, I tracked it down. It was a rather nasty and not very obvious bug which potentially mismatched the question texts with the duplicated files. I have committed a fix just now to the SVN here on R-Forge. You can either check out the SVN and rebuild the package by hand or you can wait until R-Forge has updated its packages at (Revision 897 is required.)

Starting from your hist.Rnw example, I put together barplot.Rnw (attached) where it is even more immediately visible whether table and figure match. Doing:

exams2pdf("barplot.Rnw", nsamp = 2)

should yield the desired output.

RE: exams2pdf + figures [ reply ]
By: Achim Zeileis on 2016-04-29 07:18
Thanks again for spotting this. I have to check what is going on there. A quick workaround is to use

exams2pdf(rep("hist.Rnw", 2))


RE: exams2pdf + figures [ reply ]
By: Marek Pomp on 2016-04-29 06:32

plain1.pdf (6) downloads
Dear Achim,
I have to apologize ... :-)

Yesterday, when I (quickly) tested yours new code, I concentrated only to check that figures are different in different Problems.
But (I thing) there is reverse bug. The text of Problems are the same for each Problem.
Best wishes

R code:
> sessionInfo()
other attached packages:
[1] exams_2.2-0

> exams2pdf("hist2.Rnw", nsamp=2)

Results are enclosed.

RE: exams2pdf + figures [ reply ]
By: Achim Zeileis on 2016-04-28 09:34
Good, we're glad if it's useful for you! Let us know if you encounter more problems.

RE: exams2pdf + figures [ reply ]
By: Marek Pomp on 2016-04-28 08:09
thank you for immediate reply, witch solved my request. (Please, you don't have to apologize, for me it is a pleasure to have the possibility to work with yours code.)
Best wishes

RE: exams2pdf + figures [ reply ]
By: Achim Zeileis on 2016-04-27 15:41

thanks for your message and apologies for the confusion/inconvenience. This is/was a bug that we discovered and fixed two weeks ago but haven't released the fixed version on CRAN yet. So if you use

install.packages("exams", repos = "")

everything should work as desired.

Background: In exams2html and exams2moodle the problems do not exist because the graphics files are turned into Base64 strings and embedded directly into the HTML code. However, when producing the .pdf from the .tex we copy all required graphics to the same temporary directory. And this leads to overwriting if there are multiple files with the same name. It is avoided now by renaming the files if necessary.

exams2pdf + figures [ reply ]
By: Marek Pomp on 2016-04-27 07:48

plain1.pdf (10) downloads
it seems to me that the driver exams2pdf don't work properly with
figures, when the number of exercises is less then nsamp (in case of
sampling with replacement).

R session:
> exams2pdf("hist.Rnw", nsamp=3)

The supplement directory contains correct figures for each exercise

/tmp/ ... /exam1/exercise1/hist-002.pdf
/tmp/ ... /exam1/exercise2/hist-002.pdf
/tmp/ ... /exam1/exercise3/hist-002.pdf

But in the final pdf file there is in each Problem the same figure.

Drivers exams2moodle and exams2html both work fine.

Is there my misunderstanding somewhere?

Thanks for your answer

file hist.Rnw:
<<echo=FALSE, resuls=hide>>=
x <- rnorm(50)
Display data:
<<fig=TRUE, width=5, height=3>>=
Another view of data:
<<echo=FALSE, results=verbatim>>=

%% \extype{num}
%% \exsolution{0}
%% \exname{hist}
%% \extol{0}

Thanks to:
Vienna University of Economics and Business University of Wisconsin - Madison Powered By FusionForge