5

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.

Community
  • 1
  • 1
Nancy
  • 3,989
  • 5
  • 31
  • 49
  • 1
    try setting your working directory to `~/Desktop/thing` and running `knit2pdf` there rather than trying to redirect output ... ? – Ben Bolker Nov 05 '15 at 21:28
  • Why should that make a difference? (Genuine question, no catty tone implied) – Nancy Nov 05 '15 at 21:32
  • 1
    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 Nov 05 '15 at 21:37
  • 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 Nov 05 '15 at 21:42
  • 1
    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 Nov 05 '15 at 21:45
  • "/Users/me/Desktop/thing" – Nancy Nov 05 '15 at 21:46
  • I'm asking about changing knit2pdf so that in the context of a less trivial program knit2pdf will work properly. – Nancy Nov 05 '15 at 21:47
  • does `knit2pdf("test.Rnw")` work? How about `s <- setwd("~/Desktop/thing"); knit2pdf("test.Rnw"); setwd(s)` ? – Ben Bolker Nov 05 '15 at 21:47
  • Yes it does, so I suppose I can't complain. I was hoping that understanding this would help me understand how to produce images and PDF's iteratively with subsets of the data (using knit2pdf as a director) but I'm still unable to do it even with the explicitly set wd. – Nancy Nov 05 '15 at 21:59
  • It's late, but the explaination is (partly) here: http://stackoverflow.com/questions/34591487/difference-compile-pdf-button-in-rstudio-vs-knit-and-knit2pdf in case somebody is still interested... – Bastien May 18 '17 at 12:27

2 Answers2

3

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")
Nancy
  • 3,989
  • 5
  • 31
  • 49
0

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.