knitr example doesn't produce graphics

1,320

RStudio by default uses Sweave for .Rnw files and adds the line \SweaveOpts{concordance=TRUE} after \begin{document}. Set "Sweave" from Global or Project Options (from the Tool menu) to specify knitr processing instead of Sweave.

Share:
1,320

Related videos on Youtube

Simon Kuang
Author by

Simon Kuang

Updated on August 01, 2022

Comments

  • Simon Kuang
    Simon Kuang over 1 year

    I tried compiling the given MWE for testing knitr:

    %% LyX 2.1.3 created this file.  For more info, see http://www.lyx.org/.
    %% Do not edit unless you really know what you are doing.
    \documentclass{article}
    \usepackage[sc]{mathpazo}
    \usepackage[T1]{fontenc}
    \usepackage{geometry}
    \geometry{verbose,tmargin=2.5cm,bmargin=2.5cm,lmargin=2.5cm,rmargin=2.5cm}
    \setcounter{secnumdepth}{2}
    \setcounter{tocdepth}{2}
    \usepackage{url}
    \usepackage[unicode=true,pdfusetitle,
     bookmarks=true,bookmarksnumbered=true,bookmarksopen=true,bookmarksopenlevel=2,
     breaklinks=false,pdfborder={0 0 1},backref=false,colorlinks=false]
     {hyperref}
    \hypersetup{
     pdfstartview={XYZ null null 1}}
    \begin{document}
    \SweaveOpts{concordance=TRUE}
    <<setup, include=FALSE, cache=FALSE>>=
    library(knitr)
    # set global chunk options
    opts_chunk$set(fig.path='figure/minimal-', fig.align='center', fig.show='hold')
    options(formatR.arrow=TRUE,width=90)
    @
    
    
    \title{A Minimal Demo of knitr}
    
    
    \author{Yihui Xie}
    
    \maketitle
    You can test if \textbf{knitr} works with this minimal demo. OK, let's
    get started with some boring random numbers:
    
    <<boring-random>>=
    set.seed(1121)
    (x=rnorm(20))
    mean(x);var(x)
    @
    
    The first element of \texttt{x} is \Sexpr{x[1]}. Boring boxplots
    and histograms recorded by the PDF device:
    
    <<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='')
    @
    
    Do the above chunks work? You should be able to compile the \TeX{}
    document and get a PDF file like this one: \url{https://github.com/yihui/knitr/releases/download/doc/knitr-minimal.pdf}.
    The Rnw source of this document is at \url{https://github.com/yihui/knitr/blob/master/inst/examples/knitr-minimal.Rnw}.
    \end{document}
    

    I copied the code into a Sweave document in RStudio and clicked "Compile PDF." The resulting PDF looks kind of like this (2 pages placed side-by-side):

    both pages

    What's wrong?

    • R. Schumacher
      R. Schumacher over 8 years
      This is the hazard of using a template without reading the supporting manuals. The code knitr-minimal.Rnw is written in knitr complaint syntax and it preprocesses-compiles-views correctly using the knit-command then pdflatex then view-pdf. However, you added line 18, \SweaveOpts{concordance=TRUE} which is a Sweave specific command requiring the Sweave preprocessor. The end result is not the expected output. Remove line 18.
    • sandro
      sandro over 8 years
      You have to compile it with knitr and not with Sweave. With Sweave I get your result and with knitr (if you omit the line with sweaveopts), you should obtain the desired result
  • R. Schumacher
    R. Schumacher over 8 years
    Go here and see how to change the RStudio default. yihui.name/knitr/demo/rstudio. Or, you will have to do a bit of rewriting as Sweave does not handle graphics as cleanly as knitr. In Sweave, to get two graphics side by side you have to set up a dual pane graphic window in R and manage all the labels, etc in R. Knitr allows you to take more advantage of the control of structure offered by LaTeX.