Knitr and documentclass(exam): plots not showing

Hi everyone, I'm typing an exam using Knitr and documentclass(exam): everything works fine, except that plots are not shown. All I get is a dark rectangle instead of the plot. I used basic plots that work in documentclass(article), taken from there: Minimal examples - Examples for Rnw, Markdown, HTML and LaTeX - Yihui Xie | 谢益辉. I provide a MWE below. ggplot does not work either. Any thoughts ? Thank you !

% \documentclass[addpoints,12pt]{exam}
\documentclass[addpoints,12pt,answers]{exam}
\usepackage[left=1.5cm,right=1.5cm,top=1.5cm,bottom=1.8cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{lmodern}
\usepackage{graphicx}
\usepackage{textcomp}
\usepackage{amsmath, amsfonts, amssymb, amsthm}
\usepackage{multirow}
\usepackage{setspace}
\usepackage{enumitem}
\usepackage[T1]{fontenc}
\usepackage{epsfig}
\usepackage{float}
\usepackage{subfig}
%\usepackage{url}
% Allow linebreak in footnote for url's
\usepackage[hyphens]{url}

%\usepackage[demo]{graphicx}
\usepackage[export]{adjustbox}

\renewcommand\questionlabel{\textbf{\thequestion.}}
\renewcommand\partlabel{\textbf{\thepartno.}}
\renewcommand\subpartlabel{\textbf{\thesubpart.}}

\renewcommand\thepartno{\thequestion.\alph{partno}}
\renewcommand\thesubpart{\thepartno.\roman{subpart}}

\pagestyle{headandfoot}
\footer{}{Page \thepage\ / \numpages}{}

%\usepackage[hyphens,spaces,obeyspaces]{url}
%\urldef{\footurl}\url{http://www.this-is-a-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-long.url/}
\usepackage[colorlinks]{hyperref} % optional

\begin{document}

\begin{center}
	\fbox{\fbox{\parbox{5.5in}{\centering
				Exam}}}
\end{center}

Exam graded over \numpoints\ points with \numbonuspoints\ bonus point(s).

\begin{questions}

\section{Random questions}

\question[1] Who are you ?

\begin{solution}
Mary Poppins
\end{solution}

\question[1] Can you do plots ?

\begin{solution}

<>=
set.seed(1121)
(x=rnorm(20))
mean(x);var(x)
@

<<boring-plots, fig.width=4, fig.height=4, out.width='.4\linewidth'>>=

two plots side by side (option fig.show='hold')

par(mar=c(4,4,.1,.1),cex.lab=.95,cex.axis=.9,mgp=c(2,.7,0),tcl=-.3,las=1)
boxplot(x)
hist(x,main='')
@

\end{solution}

\end{questions}

\end{document}

I've been playing around with this for a while and don't see anything. I suspect you are running into a package conflict, maybe some but I don't see where. Ah, wait a minute. Move

\usepackage{graphicx}

to the bottom of the preamble. This does not cure the problem but rather than black boxes you should get the box-plot in the document. It may give you a clue about the rest of the problem

I have not used base graphics in so long that I have no idea if the missing histogram is a Latex or R problem.

BTW, I think you have a couple of typos in the mwe.

<>=
set.seed(1121)
(x=rnorm(20))
mean(x);var(x)
@

Should that not be

<<>>=
set.seed(1121)
(x=rnorm(20))
mean(x);var(x)
@

and I think

<<boring-plots, fig.width=4, fig.height=4, out.width='.4\linewidth'>>=

should be

<<boring-plots, fig.width=4, fig.height=4, out.width='.4\\linewidth'>>=

Also you have

\usepackage[T1]{fontenc}

twice.

Many thanks !! It is so nice from you to have looked into it.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.