Re: [knitr] knitr "! LaTeX Error: File `figure/unnamed-chunk-1' not found."

2,109 views
Skip to first unread message

Yihui Xie

unread,
Nov 21, 2012, 11:11:04 AM11/21/12
to jrkrideau, kn...@googlegroups.com
Your example works well for me. Did you see my last comment on SO?
http://stackoverflow.com/q/13479641/559676 i.e. see what happens if
you remove cache=TRUE

BTW, although it is unlikely to be relevant, perhaps you can try to
update R first: http://cran.r-project.org/bin/linux/ubuntu/

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA


On Wed, Nov 21, 2012 at 9:58 AM, jrkrideau <jrkr...@gmail.com> wrote:
> Slightly more minimal description cross-posted to Stack Overflow (knitr)
>
> I'm totally new to knitr and to Latex so appologies if this is immediately
> obvious.
>
> I keep getting the error "! LaTeX Error: File `figure/unnamed-chunk-1' not
> found." The error message says:
>
>>! LaTeX Error: File `figure/unnamed-chunk-1' not found.
>> See the LaTeX manual or LaTeX Companion for explanation.
>> Type H <return> for immediate help.
> ...
>> l.77 ...[width=\textwidth]{figure/unnamed-chunk-1}
>> I could not locate the file with any of these extensions:
>>
>> .png,.pdf,.jpg,.mps,.jpeg,.jbig2,.jb2,.PNG,.PDF,.JPG,.JPEG,.JBIG2,.JB2,.eps
>
> I have a simple tex file
>
> \documentclass[12pt]{article}
> \usepackage[utf8]{inputenc}
> \usepackage[T1]{fontenc}
> \usepackage[english]{babel}
> \usepackage{graphicx}
> \usepackage[font={small}]{caption}
> \author{My Name }
> \title{knitr example}
> \begin{document}
> \maketitle
>
> <<loading,echo=F>>=
> library(ggplot2)
> @
>
> << message=F,fig.width=9,fig.height=6,out.width='\\textwidth',cache= TRUE>>=
> mydata <- data.frame(year = seq(1901: 1950), debt = rnorm(50))
> plot(mydata$year, mydata$debt, main = "Debt")
> @
> \end{document}
>
> ==========================Output log
> ==========================================
> 1> knit("/home/john/Latex/apaknitr1.Rnw")
>
>
> processing file: /home/john/Latex/apaknitr1.Rnw
> |>>>>>>>>>>>>>>>>>>>>>> | 33%
> ordinary text without R code
>
> |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | 67%
> label: unnamed-chunk-1 (with options)
> List of 5
> $ message : logi FALSE
> $ fig.width : num 9
> $ fig.height: num 6
> $ out.width : chr "\\textwidth"
> $ cache : logi TRUE
>
> |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>| 100%
> ordinary text without R code
>
>
> output file: /home/john/apaknitr1.tex
>
> ======================================================================================
>
> I cannot seem to locate anything that seems relevant. Clearly
> "figure/unnamed-chunk-1" should be showing up somewhere but I don't see
> anything in the knitr options document to suggest a specific way to save it
> and so far I don't understand enough about knitr to figure out a way to name
> a plot to be imported
>
> Any help would be gratefully received.
> -------------------------------------------------------------------------------------------------------------------------------
> sessionInfo()
> R version 2.15.1 (2012-06-22)
> Platform: i686-pc-linux-gnu (32-bit)
>
> locale:
> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
> [3] LC_TIME=en_CA.UTF-8 LC_COLLATE=en_US.UTF-8
> [5] LC_MONETARY=en_CA.UTF-8 LC_MESSAGES=en_US.UTF-8
> [7] LC_PAPER=C LC_NAME=C
> [9] LC_ADDRESS=C LC_TELEPHONE=C
> [11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats graphics grDevices utils datasets methods base
>
> other attached packages:
> [1] ggplot2_0.9.2.1 knitr_0.8
>
> loaded via a namespace (and not attached):
> [1] colorspace_1.2-0 dichromat_1.2-4 digest_0.5.2 evaluate_0.4.2
> [5] formatR_0.6 grid_2.15.1 gtable_0.1.1 labeling_0.1
> [9] MASS_7.3-22 memoise_0.1 munsell_0.4 plyr_1.7.1
> [13] proto_0.3-9.2 RColorBrewer_1.0-5 reshape2_1.2.1 scales_0.2.2
> [17] stringr_0.6.1 tools_2.15.1
>
>>Ubuntu 12.10 with texlive 2012.20120611-1
>
>

jrkrideau

unread,
Nov 21, 2012, 1:44:17 PM11/21/12
to kn...@googlegroups.com, jrkrideau

Hi,

My appologies—I had looked at Stack Overflow about 4 hours ago and for some reason did not see your reply.



I tried removing “cache = TRUE” with no results.



I then followed your suggestion and updated to R2.15.2 and updated all my installed packages.



Then I just wiped out anything that appeared connected to the .Rnw or .tex files and created a bare-bones example that works and then does not.



It appear that my problem is coming from what directory I output to.

knit("~/Latex/aknit1.Rnw") ===>> output file: /home/john/aknit1.tex

works just fine.

knit("~/Latex/aknit1.Rnw", output = "~/Latex/aknit1.tex") ===>>

output file: /home/john/Latex/aknit1.tex

does not work on a second compile.

So presumably I am deleting something ? I have no idea how I am doing it.



=============bare bones example==============

\documentclass[12pt,letterpaper]{apa6}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\author{John Kane}
\begin{document}
<<one.plot>>=
mydata  <-  data.frame(aa = 1:10, bb  <-  10:1)
plot(mydata$aa, mydata$bb)
@
\end{document}



sessionInfo()

R version 2.15.2 (2012-10-26)

Platform: i686-pc-linux-gnu (32-bit)

locale:

[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C

[3] LC_TIME=en_CA.UTF-8 LC_COLLATE=en_US.UTF-8

[5] LC_MONETARY=en_CA.UTF-8 LC_MESSAGES=en_US.UTF-8

[7] LC_PAPER=C LC_NAME=C

[9] LC_ADDRESS=C LC_TELEPHONE=C

[11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C

attached base packages:

[1] stats graphics grDevices utils datasets methods base

other attached packages:

[1] knitr_0.8

loaded via a namespace (and not attached):

[1] digest_0.5.2 evaluate_0.4.2 formatR_0.6 plyr_1.7.1 stringr_0.6.1

[6] tools_2.15.2

Yihui Xie

unread,
Nov 21, 2012, 3:59:21 PM11/21/12
to jrkrideau, kn...@googlegroups.com
Now you showed how you really called knit(), and that revealed the
problem: you are not supposed to specify the output file to another
directory.

Here is my first principle of working with R: set the working
directory before you working on anything; from then on, always use
relative paths instead of absolute paths.

setwd('~/Latex/')
library(knitr)
knit('aknit1.Rnw')
# or knit('aknit1.Rnw', output = 'aknit1.tex')

But I think you really need a better editor such as RStudio or
Emacs/ESS, which will take care of this automatically. It wastes time
to type setwd(), library(knitr) and knit() every time.

The reason for the failure in your case is, you wrote aknit1.tex to a
place which is not your working directory, but your figures were
written to the figure/ directory relative to the current working
directory (i.e. /home/john/figure/figure/unnamed-chunk-1.pdf), so the
tex file does not really know where are the figures.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA


jrkrideau

unread,
Nov 21, 2012, 4:41:25 PM11/21/12
to kn...@googlegroups.com, jrkrideau
Oh, okay that seems clear enough but is this documented anywhere?  I probably just missed it as I had assumed that specifying the output path for the tex file would affect the other components of the process. Somehow it seems sneaky :)


 I should have realize that I had a new figure directory in my home directory  from the error message but I did not clue in.

I personally prefer absolute paths, which may be bad practice but it hastended to work for me.  I'll just have to remember that knitr needs a change in the working directory.

I had not realised that EMACS reset the directory automatically.  Perhaps I should give it another look.

Thanks for all your trouble.

Now on to the next debacle!

Yihui Xie

unread,
Nov 21, 2012, 5:09:00 PM11/21/12
to jrkrideau, kn...@googlegroups.com
This is a hard decision for me -- I offend some users if I do not
write output relative to the current working directory, and offend
other users if I do. I have been thinking about what to do in the case
where the tex output is under a different directory. I can come up
with an automatic solution, but then I may have to come up with yet
another solution to cancel this solution if the user does not like it.

For Emacs/ESS, see these lines where they setwd():
https://github.com/emacs-ess/ESS-mirror/blob/trunk/lisp/ess-swv.el#L122-L126

You are right that this problem is not clearly documented, partly
because most users rarely use knitr that way (the output argument does
not need to be specified). You are the first one I see who was bitten
by this problem.

The unclear documentation is at http://yihui.name/knitr/options;
search for base.dir and that can solve your problem, e.g.

opts_knit$set(base.dir = '~/Latex/')

But as I said, it is better to set a working directory for your
project before you work on anything else. It is not only about knitr,
but also about reading data files and writing output, etc. Normally I
just

cd ~/Latex
R

Then you do not need to worry if you are under ~/Latex or ~/What/ever,
and you are free to copy and distribute the project to anywhere else
since relative paths are portable.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA


jrkrideau

unread,
Nov 22, 2012, 7:53:56 AM11/22/12
to kn...@googlegroups.com, jrkrideau


On Wednesday, November 21, 2012 5:09:21 PM UTC-5, Yihui Xie wrote:
This is a hard decision for me -- I offend some users if I do not
write output relative to the current working directory, and offend
other users if I do. I have been thinking about what to do in the case
where the tex output is under a different directory. I can come up
with an automatic solution, but then I may have to come up with yet
another solution to cancel this solution if the user does not like it.

Good idea but just a note saying that the figure directory is created in the working directly, not necessarilly the same as the output directory (feel free to use my beautiful example :) ) probably would do. I't not a hassle once one is aware of it. 

For Emacs/ESS, see these lines where they setwd():
https://github.com/emacs-ess/ESS-mirror/blob/trunk/lisp/ess-swv.el#L122-L126

You are right that this problem is not clearly documented, partly
because most users rarely use knitr that way (the output argument does
not need to  be specified). You are the first one I see who was bitten
by this problem.

I like to be a leader or at least a trend setter.

The unclear documentation is at http://yihui.name/knitr/options;
search for base.dir and that can solve your problem, e.g.

opts_knit$set(base.dir = '~/Latex/')

Oh I see, that looks nice indeed.  I will admit that to a layman like me it is a bit obscure but it there.  I just lack the knowledge to recognize  it.

But as I said, it is better to set a working directory for your
project before you work on anything else. It is not only about knitr,
but also about reading data files and writing output, etc. Normally I
just

cd ~/Latex
R

Then you do not need to worry if you are under ~/Latex or ~/What/ever,
and you are free to copy and distribute the project to anywhere else
since relative paths are portable.

Regards,
Yihui

I see your point but I am so use to absolute paths in a lot of work that I will have to think about this carefully especially as I often pull materals from more than one directory.

Thanks for all the time and very helpful explanations.

John
Reply all
Reply to author
Forward
0 new messages