yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.38k stars 873 forks source link

knitr + tikzDevice hangs when compiling on windows + r studio #1181

Closed jolars closed 8 years ago

jolars commented 8 years ago

This issue has been documented previously here and here but no solution has so far been offered. The issue does not seem related to tikzDevice since calling tikz() as device in R works flawlessly.

Below I've borrowed an example from one of the links above, along with the output I get.

\documentclass{article}
\begin{document}

<<setup, include=F, cache=F>>=
library(knitr)
@

Here is some text.
Next is a pdf plot:

<<test1, dev='pdf'>>=
plot(c(1,2),c(2,3))
@

Next is the same but using tikz:

<<test2, dev='tikz'>>=
plot(c(1,2),c(2,3))
@

\end{document}
Loading required package: knitr

processing file: tikztest.Rnw
  |.........                                                        |  14%
  ordinary text without R code

  |...................                                              |  29%
label: setup (with options) 
List of 2
 $ include: symbol F
 $ cache  : symbol F

  |............................                                     |  43%
  ordinary text without R code

  |.....................................                            |  57%
label: test1 (with options) 
List of 1
 $ dev: chr "pdf"

  |..............................................                   |  71%
  ordinary text without R code

  |........................................................         |  86%
label: test2 (with options) 
List of 1
 $ dev: chr "tikz"

And it will just hang there until I decide to terminate.

Here's my sessionInfo():

R version 3.2.4 (2016-03-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] tikzDevice_0.10-1 knitr_1.12.3     

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.3         nloptr_1.0.4        RColorBrewer_1.1-2  plyr_1.8.3          iterators_1.0.8     tools_3.2.4         rpart_4.1-10        lme4_1.1-11         nlme_3.1-125        gtable_0.2.0       
[11] lattice_0.20-33     mgcv_1.8-12         filehash_2.3        Matrix_1.2-3        foreach_1.4.3       parallel_3.2.4      SparseM_1.7         mvtnorm_1.0-5       coin_1.1-2          stringr_1.0.0      
[21] MatrixModels_0.4-1  stats4_3.2.4        grid_3.2.4          caret_6.0-64        nnet_7.3-12         survival_2.38-3     multcomp_1.4-4      TH.data_1.0-7       latticeExtra_0.6-28 minqa_1.2.4        
[31] ggplot2_2.1.0       reshape2_1.4.1      car_2.1-1           magrittr_1.5        scales_0.4.0        codetools_0.2-14    modeltools_0.2-21   MASS_7.3-45         splines_3.2.4       pbkrtest_0.4-6     
[41] strucchange_1.5-1   colorspace_1.2-6    quantreg_5.21       sandwich_2.3-4      stringi_1.0-1       party_1.0-25        munsell_0.4.3       zoo_1.7-12   
jolars commented 8 years ago

It seems like adding external=TRUE to the chunk options and \usepackage{tikz} to the latex preamble makes the document compilation finish, although this means the tikz compiled plot seems to ignore out.width calls.

Imgur

yihui commented 8 years ago

Sorry I cannot reproduce it on Windows. external = TRUE is just the default. \usepackage{tikz} should be automatically added to the tikz file as well. So I have no idea why the problem could be fixed by that.

jolars commented 8 years ago

Sorry, I meant the opposite. Adding external=FALSE makes it work. I understand; I guess there's something on my end then. I'm using Tex Live 2015 is that makes any difference.

yihui commented 8 years ago

I see. This is probably a little hard to diagnose. I have MikTeX on Windows.

You can find the .tikz file under the figure/ directory, and try to run pdflatex on it and see what happens.

jolars commented 8 years ago

Thanks. I located the file and it compiles perfectly fine when I run pdflatex. It hangs on XeLaTeX too.

jolars commented 8 years ago

I tried running knit() from the console. It stops at the same place but instead throws an error.

Quitting from lines 18-19 (fonts_plots.Rnw) 
Error: failed to compile figure/test2-1.tikz to PDF
In addition: Warning message:
running command '"C:\texlive\2015\bin\win32\pdflatex.exe" "test2-1.tikz"' had status 1 
yihui commented 8 years ago

Sorry, I have no idea about what could be wrong. All I can say for now is that if you want to debug the issue by yourself, here is where you need to look at: https://github.com/yihui/knitr/blob/c3085229/R/plot.R#L127-L149 Maybe TEXINPUTS was the culprit; or getOption('tikzLatex') points to the wrong pdflatex.exe; ... It is difficult for me to fix it without being able to reproduce it first.

jolars commented 8 years ago

That sounds reasonable, thank. I'll play around with the settings and see if I can figure something out.

stla commented 7 years ago

I have the same problem (and I am not the only one).

It seems to be related to my misconfigured network drive. See this SO thread about another unfortunate consequence of this misconfiguration (I am the author of this SO thread).

Indeed, after moving the Rnw file to my C drive, I can compile it without any issue.

But, contrary to the issue described in the SO thread, this has nothing to do with file.access (I think).

stla commented 7 years ago

Another information possibly helpful.

I modified knitr:::plot2dev like this: system2(latex, shQuote(basename(path)), stdout = "out.txt", stderr="err.txt").

Then (when the compilation does not work, on my network drive), I get that in err.txt:

Sorry, but C:\PortableApps\MikTeX\texmfs\install\miktex\bin\pdflatex.exe did not succeed.

The log file hopefully contains the information to get MiKTeX going again:

  C:/PortableApps/MikTeX/texmfs/data/miktex/log/pdflatex.log

You may want to visit the MiKTeX project page, if you need help.

And in pdflatex.log:

2017-02-03 20:42:59,947+0100 FATAL pdflatex - Info: path="U:/Data/Slides/SlidesMaria3:"
2017-02-03 20:42:59,947+0100 FATAL pdflatex - Source: Libraries\MiKTeX\Core\Directory\win\winDirectory.cpp
2017-02-03 20:42:59,947+0100 FATAL pdflatex - Line: 82
2017-02-03 20:44:03,901+0100 FATAL pdflatex - Windows API error 1251: This operation is supported only when you are connected to the server.

I've found this thread about this error, but this didn't help me (note that I don't have admin rights hence I can't try every proposal): http://tex.stackexchange.com/questions/51799/windows-api-error-5-access-is-denied-when-trying-to-compile-tikz-picture

stla commented 7 years ago

This operation is supported only when you are connected to the server.

I'm pretty sure now. This message occurs when there's a relative path from a hard drive to another one.

@yihui , Is there a relative path from the Rnw file to MikTeX ? In this case, that's the problem.

kkmann commented 7 years ago

hm, I also just ran into this problem - it's a pity as it seems to me that tikzDevice should be the preferred solution for reproducible reports

kkmann commented 7 years ago

Okay, so I am kind of out of ideas now. I can confirm that the issues seems to be https://github.com/yihui/knitr/blob/c3085229/R/plot.R#L127-L149, LaTeX just does not find the .tikz file. I am running Win 10 with texlive 2017. Manually compiling the figure works fine - it is definately just about pointing LaTeX to the right file. Any ideas?

stla commented 7 years ago

@kkmann For me, this error occurs only when I work on my network drive and when this network drive is offline. When I work on the C drive (where MikTeX is installed), there's no problem. There's no problem as well when I work on my network drive and when this one is online. Are you using two different hard drives ? If not, that would mean there are different possible causes of this issue...

kkmann commented 7 years ago

Well, I guess my fix can only solve the initial problem. Your's seems to be way out of my league ;) Are you sure its knitr related? The error looks more like a TeX problem to me... Can you compile the .tikz file manually?

stla commented 7 years ago

Yes, I can compile the tikz file manually (and I recall that everything works on the C drive anyway, knitr+LaTeX). I'll try your fix but I would be surprised that it solves my case. But who knows...

kkmann commented 7 years ago

Maybe you should open a new issue as your case is different from @jolars and mine.

yihui commented 7 years ago

I have merged #1420. Please devtools::install_github('yihui/knitr') and see if it fixes your issue.

stla commented 7 years ago

Yes, it works !! Big thanks @kkmann !

stla commented 7 years ago

Look at the log I posted, it contained the smoking gun !! : path="U:/Data/Slides/SlidesMaria3:" I didn't notice the colon !!!

kkmann commented 7 years ago

took me two hours too, win/linux is always good for a surprise x)

@yihui thx for the quick merge and keep up your awesome work!

yihui commented 7 years ago

I'm so glad that this old issue has been resolved. And lessons learned. Thanks everyone!

github-actions[bot] commented 3 years ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.