5
votes

I have a .Rnw file that I am able to compile into a PDF using the "Compile PDF" button in RStudio (or Command+Shift+k). However, when I use knit2pdf the graphics are not created and the complete PDF is not created. Why would this happen? How do you specifically set where the images will be stored so that pdflatex can find them?

Here is an example. I am aware that this question that I posted a few days ago has a similar example, but in my mind these are two different questions.

This file will run just fine and produce a PDF if I hit "Compile". I don't get any errors, the figure is produced in the /figure directory, and all is well.

%test.Rnw
\documentclass{article}
\usepackage[margin=.5in, landscape]{geometry}
\begin{document}

This is some test text!

<<setup, include=FALSE, results='hide', cache=FALSE>>=
opts_chunk$set(echo=FALSE, warning = FALSE, message = FALSE,
 cache = FALSE, error = FALSE)
library(ggplot2)
@

<<printplotscreen, results='asis'>>=
ggplot(diamonds) + 
  geom_bar(aes(x = color, stat = "bin"))
@

\end{document}

However, when I run this script that is intended to do exactly the same thing as hitting "Compile" (is it?) the figure is not created and I get the not-surprising error below about not being able to find it.

#test.R
library("knitr")

knit2pdf(input = "~/Desktop/thing/test.Rnw",
         output=paste0('~/Desktop/thing/test','.tex'))

Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet,  : 
  Running 'texi2dvi' on 'test.tex' failed.
LaTeX errors:
! LaTeX Error: File `figure/printplotscreen-1' not found.

NOTE: If you are trying to reproduce this (and thanks!) then make sure you run the knit2pdf script FIRST to see that it doesn't create the figures. If you hit "Compile" first then the figures will be there for knit2pdf to use, but it will not accurately represent the situation.

2
try setting your working directory to ~/Desktop/thing and running knit2pdf there rather than trying to redirect output ... ? - Ben Bolker
Why should that make a difference? (Genuine question, no catty tone implied) - Nancy
because it looks like knit2pdf is not looking in the right place for the figures -- it's assuming they're in a subdirectory figure/, which might not be true if the LaTeX compilation step is assuming all file references are relative to the location of the .tex file? - Ben Bolker
I'm not sure I understand: the figures ARE all in the figure/ subdirectory, and "Compile PDF" has no problem creating and finding them. Is there a way that I can change the knit2pdf(...) so that it will be able to create and find them too? I find it odd that knit2pdf is able to find the figures if they were already created (and in the figure/ subdir). - Nancy
what is your working directory (i.e. getwd()) ? The point is that "Compile PDF" builds in a temporary working directory, where it knows exactly where everything is. I'm not saying that I understand exactly why knit2pdf() isn't working, but I think it's something about building an Rnw file that isn't in the current working directory ... - Ben Bolker

2 Answers

3
votes

The solution: Make sure to set the working directory to the project directory before using knit2pdf, then shorten the "input" path to just the .Rnw file. Thus...

test.R
library("knitr")
diamonds = diamonds[diamonds$cut != "Very Good",]

setwd("/Users/me/Desktop/thing")
knit2pdf(input = "test.Rnw", output = "test.tex")
0
votes

Here are some references on this issue:
Changing working directory will impact the location of output #38 ; make sure the output dir is correct (#38) It seems that when using knit2pdf(), it automatically set your output files to the directory where your input file in. And the author doesn't recommend us changing work-directory during the middle of a project.

So the current solution for me is to save the working directory as old one(getwd()), change the working directory to where you want to save the output files, use knit2pdf() to output files, and change the working directory to the original one finally.