SCM

Forum: support

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

barplot_knitr.Rnw (4) 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
[forum:43181]

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

########## begin of R code
require(knitr)
require(exams)

## replace Sweave by knit
env <- environment(xweave)
unlockBinding("xweave", env=env)
assign("xweave",
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)
},
env=env)
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")
]
knit_patterns$restore()
knit_patterns$set(knitr_new_patterns)

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

## others option for chunks
opts_chunk$set(
#
fig.width=3.5,
fig.height=2.5,
#
echo=FALSE,
results='hide'
)

## 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="\\\\,",
digits=4,
format="f",
decimal.mark="{,}")
})
}
## for META-INFO
Sexpr_plain <- function(){
knit_hooks$set(inline = function(x) {
prettyNum(x, format="f", digits=4)
})
}

exams2pdf("barplot_knitr.Rnw")
########## end of R code

RE: exams2pdf + figures [ reply ]
By: Achim Zeileis on 2016-05-02 22:34
[forum:43176]
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
[forum:43175]
Achim,
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
Marek

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

barplot.Rnw (4) 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 https://r-forge.r-project.org/R/?group_id=1337. (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
[forum:43168]
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))

instead.

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

plain1.pdf (7) 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
Marek

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
[forum:43166]
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
[forum:43165]
Achim,
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
Marek

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

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 = "http://R-Forge.R-project.org")

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
[forum:43162]

plain1.pdf (11) downloads
Hi,
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
Marek

file hist.Rnw:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
<<echo=FALSE, resuls=hide>>=
x <- rnorm(50)
@
\begin{question}
Display data:
<<fig=TRUE, width=5, height=3>>=
hist(x)
@
Another view of data:
<<echo=FALSE, results=verbatim>>=
stem(x)
@
\end{question}

%% META-INFORMATION
%% \extype{num}
%% \exsolution{0}
%% \exname{hist}
%% \extol{0}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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