R4ds: Book will not compile to PDF

Created on 21 Aug 2016  路  24Comments  路  Source: hadley/r4ds

I just downloaded the repository via ZIP r4ds-master.zip. After extracting the files, opening the project in RStudio (preview version 0.99.1285), I installed the package devtools::install_github("hadley/r4ds"). Then I ran Build book via bookdown::gitbook which worked great. However, bookdown::pdf_book had the following errors (I'm including the last few lines of the build log plus the error messages at the end):

label: unnamed-chunk-67 (with options) 
List of 1
 $ indent: chr "    "

  |.................................................................| 100%
   inline R code fragments


output file: strings.knit.md

Error: Functions that produce HTML output found in document targeting latex output.
Please change the output type of this document to HTML. Alternatively, you can allow
HTML output in non-HTML formats by adding this option to the YAML front-matter of
your rmarkdown file:

  always_allow_html: yes

Note however that the HTML output will not be visible in non-HTML formats.

Execution halted
Error in Rscript_render(f, render_args, render_meta) : 
  Failed to compile strings.Rmd
Calls: <Anonymous> ... render_new_session -> tryCatch -> tryCatchList -> Rscript_render
In addition: Warning message:
running command '"C:/PROGRA~1/R/R-33~1.1/bin/x64/Rscript" "C:/Users/MKHIGGI/Documents/R/win-library/3.3/bookdown/scripts/render_one.R" "strings.Rmd" ".\render1fb019183b8d.rds" "_main.rds"' had status 1 
Execution halted

Exited with status 1.

Here is my current sessionInfo()

> sessionInfo()
R version 3.3.1 (2016-06-21)
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   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.6        bookdown_0.1.5     withr_1.0.2        digest_0.6.10     
 [5] assertthat_0.1     mime_0.5           R6_2.1.2           xtable_1.8-2      
 [9] git2r_0.15.0       magrittr_1.5       evaluate_0.9       httr_1.2.1        
[13] stringi_1.1.1      curl_1.2           miniUI_0.1.1       rmarkdown_1.0.9005
[17] devtools_1.12.0    tools_3.3.1        stringr_1.1.0.9000 shiny_0.13.2      
[21] rsconnect_0.4.3    httpuv_1.3.3       yaml_2.1.13        memoise_1.0.0     
[25] htmltools_0.3.5    knitr_1.14         tibble_1.1-9   

Thank you for your time and development on an excellent book! -Melinda

Most helpful comment

Yes. Using RStudio 1.2.1335 on macOS Mojave (10.14.5):

  1. Install the R packages used in this book via

    devtools::install_github("hadley/r4ds")
    
  2. Install packages bookdown and webshot as well as PhantomJS:

    install.packages("bookdown")
    install.packages("webshot")
    library(webshot)
    install_phantomjs()
    
  3. Use tinytex (R package) to install TinyTeX:

    tinytex::install_tinytex()
    
  4. Add always_allow_html: yes to the YAML-header of file index.rmd

  5. Build book:

    bookdown::render_book("index.rmd", output_format = "bookdown::pdf_book")
    

If you've already tried to build the book:

bookdown::clean_book()

and delete files/dirs _book, _bookdown_files and _main.rds.

All 24 comments

You probably need a newer version of stringr or htmlwidgets or rmarkdown

I updated my packages and installed the development versions of stringr and rmarkdown. Here is my updated sessioninfo() below. With the "strings.Rmd" chapter included, I still an error stating I need to update the YAML and add always_allow_html: yes option.

> sessionInfo()
R version 3.3.1 (2016-06-21)
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   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.6        bookdown_0.1.5     digest_0.6.10      assertthat_0.1    
 [5] mime_0.5           R6_2.1.3           xtable_1.8-2       magrittr_1.5      
 [9] evaluate_0.9       stringi_1.1.1      miniUI_0.1.1       rmarkdown_1.0.9009
[13] tools_3.3.1        stringr_1.1.0.9000 shiny_0.13.2       rsconnect_0.4.3   
[17] httpuv_1.3.3       yaml_2.1.13        htmltools_0.3.5    knitr_1.14        
[21] tibble_1.1-9      

However, I also edited the _bookdown.yml file and removed the "strings.Rmd", from the chapter list. After removing this chapter, I was able to get the bookdown::pdf_book to complete - I got past the always_allow_html: yes error message. However, I still get latex errors but I do manage to get the _main.pdf file with 338 pages, but the Table of Contents was not created.

Here are the latex errors once the "strings.Rmd" chapter is removed.

label: unnamed-chunk-35 (with options) 
List of 2
 $ fig.width: num 8
 $ echo     : logi FALSE

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


output file: communicate-plots.knit.md

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS _main.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output _main.tex --table-of-contents --toc-depth 2 --template "C:\Users\MKHIGGI\Documents\R\win-library\3.3\rmarkdown\rmd\latex\default-1.17.0.2.tex" --number-sections --highlight-style tango --latex-engine xelatex --variable graphics=yes --variable "geometry:margin=1in" --variable tables=yes --standalone 

Latexmk, John Collins, 22 April 2016. Version 4.45
stdin -> _main.pdf
[1][2][3][4][5][6][7][8][9][10][11][12][13][14][15][16][17][18][19][20][21][22][23][24][25][26][27][28][29][30][31][32][33][34][35][36][37][38][39][40][41][42][43][44][45][46][47][48][49][50][51][52][53][54][55][56][57][58][59][60][61][62][63][64][65][66][67][68][69][70][71][72][73][74][75][76][77][78][79][80][81][82][83][84][85][86][87][88][89][90][91][92][93][94][95][96][97][98][99][100][101][102][103][104][105][106][107][108][109][110][111][112][113][114][115][116][117][118][119][120][121][122][123][124][125][126][127][128][129][130][131][132][133][134][135][136][137][138][139][140][141][142][143][144][145][146][147][148][149][150][151][152][153][154][155][156][157][158][159][160][161][162][163][164][165][166][167][168][169][170][171][172][173][174][175][176][177][178][179][180][181][182][183][184][185][186][187][188][189][190][191][192][193][194][195][196][197][198][199][200][201][202][203][204][205][206][207][208][209][210][211][212][213][214][215][216][217][218][219][220][221][22... <truncated>
xdvipdfmx:warning: JPEG: Inconsistent resolution may have specified in Exif and JFIF: 300x300 - 72x72
][237][238][239][240][241][242][243][244][245][246][247][248][249][250][251][252][253][254][255][256][257][258][259][260][261][262][263][264][265][266][267][268][269][270][271][272][273][274][275][276][277][278][279][280][281][282][283][284][285][286][287][288][289][290][291][292][293][294][295][296][297][298][299][300][301][302][303][304][305][306][307][308][309][310][311][312][313][314][315][316][317][318][319][320][321][322][323][324][325][326][327][328][329][330][331][332][333][334][335][336][337][338]
xdvipdfmx:warning: PDF destination "select" not defined.
xdvipdfmx:warning: PDF destination "summary-funs" not defined.

11332712 bytes written
! Text line contains an invalid character.
l.18942 \StringTok{`}\DataTypeTok{^^K
                                     }\StringTok{``}\DataTypeTok{\{r\}} 
Here is how much of TeX's memory you used:
 25307 strings out of 428408
 448980 string characters out of 3160624
 502725 words of memory out of 3000000
 28323 multiletter control sequences out of 15000+200000
 16407 words of font info for 50 fonts, out of 3000000 for 9000
 1328 hyphenation exceptions out of 8191
 54i,8n,68p,10405b,474s stack positions out of 5000i,500n,10000p,200000b,50000s

Error: Failed to compile _main.tex. See _main.log for more info.
In addition: Warning messages:
1: running command '"C:\PROGRA~1\MIKTEX~1.9\miktex\bin\x64\latexmk.exe" -v' had status 1 
2: The LaTeX package latexmk was not correctly installed. 
3: running command '"C:\PROGRA~1\MIKTEX~1.9\miktex\bin\x64\latexmk.exe" -v' had status 1 
4: running command '"xelatex" -halt-on-error -interaction=batchmode "_main.tex"' had status 1 
Execution halted

Exited with status 1.

Sorry, it works for me, and you can view the rendered HTML on the website, so this is pretty low priority for me.

Thank you for your time and attention. Your book is coming along great. I'm following your book development and others as part of my learning and experience working with bookdown. So your insights are much appreciated. I'll keep testing things out on my end and will eventually sort the details out. I suspect it is something on my end with my MiKTeX/LaTeX installation.

...and yes the whole book, all chapters, compiles fine using bookdown::gitbook - I just need to sort the conversion to PDF issues...

Quick FYI- the str_view() and str_view_all() functions in the stringr package which use HTML rendering is what was causing the error above. I found the discussion for issue #559 with rmarkdown https://github.com/rstudio/rmarkdown/pull/559 that seems to address problems like this one compiling HTML output into PDF. I tried setting the always_allow_html: yes option under the bookdown::pdf_book: YAML header, but this doesn't work (for me) - at least as an option under bookdown::pdf_book - I need to see if this works under a simple pdf_document output format from a single rmarkdown file. The error message is generated from render() in rmarkdown at https://github.com/rstudio/rmarkdown/blob/master/R/render.R.

So, I commented out the str_view() and str_view_all() commands in the "strings.Rmd" file and everything compiled fine. It may be that the HTML rendering is not compatible - or I do not have the right options installed for this to work when compiled to PDF.

I definitely had that problem in the past, and updated something and it went away. Unfortunately I don't remember exactly what I did. Do you have the webshot package installed?

I do not have the webshot package installed but will check into it later this evening. Thank you.

That worked. I installed devtools::install_github("wch/webshot") and then made sure to install PhantomJS via webshot::install_phantomjs(). After that, I added always_allow_html: yes to the YAML header in index.rmd and the entire book compiled to PDF without issue. One minor caveat - the HTML screenshots that get embedded into the PDF are very tiny - the text shown in the HTML output from str_view() can't be read easily - I'll have to see if there are options to set this to a bigger image (more zoomed in) capture in the future. But no worries, the PDF compiles and when rendered to HTML everything looks fine. Thank you!! Adding the webshot package got me past the compiling issues. I can't wait to try out webshot for other projects!!

It's interesting that you experience that, because when I build the book, the screenshots are way too big 馃槮

... interesting ... probably a screen resolution issue. I'm running Windows 10 on a Surface Pro 4 at 2560 x 1600 resolution. It looks like there are some options possibly using webshot::resize() but I'm not quite sure how to set these options globally so they apply when the book document compiles - primarily the "strings.rmd" chapter... no worries - it's compiling - yea!

Sorry to re-open this. Having a similar problem with rendering a PDF of the book, and it seems to be the strings.Rmd chapter that is at the root of the problem.

I've added always_allow_html: yes to the first line of the index.rmd file in the r4ds directory. I.e.,

knit: "bookdown::render_book"
title: "R for Data Science"
author: ["Garrett Grolemund", "Hadley Wickham"]
description: "This book will teach you how to do data science with R: You'll learn how to get your data into R, get it into the most useful structure, transform it, visualise it and model it. In this book, you will find a practicum of skills for data science. Just as a chemist learns how to clean test tubes and stock a lab, you'll learn how to clean data and draw plots---and many other things besides. These are the skills that allow data science to happen, and here you will find the best practices for doing each of these things with R. You'll learn how to use the grammar of graphics, literate programming, and reproducible research to save time. You'll also learn how to manage cognitive resources to facilitate discoveries when wrangling, visualising, and exploring data."
url: 'http\://r4ds.had.co.nz/'
github-repo: hadley/r4ds
twitter-handle: hadley
cover-image: cover.png
site: bookdown::bookdown_site
documentclass: book
always_allow_html: yes
---

Rendering an HTML version works out of the box. But I'm unable to render PDF.

> sessionInfo()
R version 3.3.0 (2016-05-03)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.9.5 (Mavericks)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.7     bookdown_0.1.16 digest_0.6.10   assertthat_0.1 
 [5] mime_0.5        R6_2.2.0        xtable_1.8-2    magrittr_1.5   
 [9] evaluate_0.10   stringi_1.1.2   miniUI_0.1.1    rmarkdown_1.1  
[13] tools_3.3.0     stringr_1.1.0   shiny_0.14.1    rsconnect_0.5  
[17] httpuv_1.3.3    yaml_2.1.13     htmltools_0.3.5 knitr_1.14.12  
[21] tibble_1.2  

I got the PDF, after some errors, some very similar to yours. In my case the error messages was not very clear about the error itself and about the error line. So my advice is to check the root folder after a error. If you look closely you have strings.Rmd and strings.knit.Rmd (or something like that) files in the folder.
Open both files, side by side.
Go to last line in strings.knit.Rmd (or something like that) and see which code chunk was the root of your problem.
After that, add eval = FALSE in strings.Rmd {r} and rerun knit.
Repeat this process until you will have the PDF rendered book.

Of course this in some cases could not be the best, or even the correct solution. But it worked for me, the only trick that works, after many tries, uploading packages and Latex.

Can someone send me the PDF book please :)
Email: eltorofuerte.[email protected]

It will be nice if someone will put step by step tutorial for building this book pdf for people that are not familiar with R and bookdown.

Anyone got the PDF now?

Yes. Using RStudio 1.2.1335 on macOS Mojave (10.14.5):

  1. Install the R packages used in this book via

    devtools::install_github("hadley/r4ds")
    
  2. Install packages bookdown and webshot as well as PhantomJS:

    install.packages("bookdown")
    install.packages("webshot")
    library(webshot)
    install_phantomjs()
    
  3. Use tinytex (R package) to install TinyTeX:

    tinytex::install_tinytex()
    
  4. Add always_allow_html: yes to the YAML-header of file index.rmd

  5. Build book:

    bookdown::render_book("index.rmd", output_format = "bookdown::pdf_book")
    

If you've already tried to build the book:

bookdown::clean_book()

and delete files/dirs _book, _bookdown_files and _main.rds.

^ And don't forget to devtools::install_github("hadley/r4ds")

Hi, I'm having the same problem, and I tried benz0li's solution. I'm getting the following error message now:

In file(con, "r") : cannot open file '_common.R': No such file or directory

Could you help me with this?

@yiy19 I will have a look at it tomorrow.

  • Which OS are you working with?
  • What version of RStudio (Server)?
  • Are your R packages up to date?

@benz0li Hi!

  1. I'm working with macOS Big Sur 11.2.1
  2. My RStudio version is Version 1.4.1103
  3. My packages are up to date except for svglite.
    I just updated all my packages, but I'm trouble updating the package svglite. The error message says that 'png.h' file is not found. I'm trying to troubleshoot it now.

Thank you!

@benz0li

My packages are up to date now.

I've fixed the issue mentioned earlier here: https://github.com/hadley/r4ds/issues/296#issuecomment-784463549

However, now I'm back to having the problem similar to the one the original poster mentioned. I get an error message like this:

`
output file: strings.knit.md

Error: Functions that produce HTML output found in document targeting latex output.
Please change the output type of this document to HTML. Alternatively, you can allow
HTML output in non-HTML formats by adding this option to the YAML front-matter of
your rmarkdown file:

always_allow_html: true

Note however that the HTML output will not be visible in non-HTML formats.

In addition: Warning message:
In has_crop_tools() :
Tool(s) not installed or not in PATH: pdfcrop, ghostcript
-> As a result, figure cropping will be disabled.
Execution halted
Error in Rscript_render(f, render_args, render_meta, add1, add2) :
Failed to compile strings.Rmd
Calls: ... render_book -> render_new_session -> Rscript_render
Execution halted
'

@yiy19 I have updated https://github.com/hadley/r4ds/issues/296#issuecomment-501649905. Please try again.

I'm quite sure, that TinyTeX is not installed. Not tinytex, the R package; but TinyTeX, the lightweight and easy-to-maintain LaTeX distribution.

@benz0li I got a new repository for the r4ds github files and tried https://github.com/hadley/r4ds/issues/296#issuecomment-501649905 again. It worked! I'm so happy and grateful. Thank you for your help!

Was this page helpful?
0 / 5 - 0 ratings