Produce PDF from Rmarkdown - r

I am using rmarkdown in Rstudio to try to Knit a PDF using the example code provided by Rstudio (code below):
---
title: "Untitled"
output:
pdf_document:
keep_tex: yes
---
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r}
summary(cars)
```
You can also embed plots, for example:
```{r, echo=FALSE}
plot(cars)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
But, I get the following error:
! pdfTeX error (font expansion): auto expansion is only possible with scalable
fonts.
\AtBegShi#Output ...ipout \box \AtBeginShipoutBox
\fi \fi
l.144 \end{document}
pandoc.exe: Error producing PDF from TeX source
Error: pandoc document conversion failed with error 43
In addition: Warning message:
running command '"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS test_rmarkdown.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output test_rmarkdown.pdf --template "C:\Users\XXXX\Documents\R\win-library\3.2\rmarkdown\rmd\latex\default.tex" --highlight-style tango --latex-engine pdflatex --variable "geometry:margin=1in"' had status 43 Execution halted
My session info is shown below:
> sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C 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.11.6 digest_0.6.8 MASS_7.3-43 grid_3.2.2 plyr_1.8.2 gtable_0.1.2 magrittr_1.5 scales_0.2.4
[9] ggplot2_1.0.1 stringi_0.4-1 reshape2_1.4.1 rmarkdown_0.8 proto_0.3-10 tools_3.2.2 stringr_1.0.0 munsell_0.4.2
[17] yaml_2.1.13 colorspace_1.2-6 htmltools_0.2.6
And I am running the complete TeXworks version 0.4.6 64-bit.

I had similar problems before.
Try to run devtools::install_github("rstudio/rmarkdown")
This worked for me.
If you dont have devtools you need to run this to:
install.packages("devtools")
library(devtools).

Related

Rmarkdown: Cannot insert image with knitr::include_graphics after R updated

I'm trying to render and old rmarkdown file after I've updated R and RStudio, and all packages, but I'm getting:
Error: pandoc document conversion failed with error 99
However, the path is correct. I've rendered to HTML yesterday and everything worked fine.
```{r, echo=FALSE, fig.cap="", out.width = '50%', fig.align='center'}
knitr::include_graphics("/img/posts/que-es-un-api/mastercard-blockchain-api.jpg")
```
It says the path to the image is wrong, but hope you can see from
image path is right.
"C:/PROGRA~1/Pandoc/pandoc" +RTS -K512m -RTS que-es-un-api.utf8.md --to html4 --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash+smart --output que-es-un-api.html --email-obfuscation none --self-contained --standalone --section-divs --template "C:\Users\OGONZALES\Documents\R\win-library\3.6\rmarkdown\rmd\h\default.html" --no-highlight --variable highlightjs=1 --variable "theme:bootstrap" --include-in-header "C:\Users\OGONZA~1\AppData\Local\Temp\RtmpSsSNeA\rmarkdown-str1340bc36ef.html" --mathjax --variable "mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
File /img/posts/que-es-un-api/mastercard-blockchain-api.jpg not found in resource path
Error: pandoc document conversion failed with error 99
Execution halted
I've even tried to reinstall Pandoc with:
# installing/loading the package:
if(!require(installr)) { install.packages("installr"); require(installr)} #load / install+load installr
# Installing pandoc
install.pandoc()
sessionInfo:
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)
Matrix products: default
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] compiler_3.6.1 htmltools_0.3.6 tools_3.6.1 yaml_2.2.0 Rcpp_1.0.2
[6] rmarkdown_1.14 knitr_1.24 xfun_0.8 digest_0.6.20 evaluate_0.14
Maybe the working directory was changed. Try to include the whole path:
```{r, echo=FALSE, fig.cap="", out.width = '50%', fig.align='center'}
knitr::include_graphics("D:/omargonzalesdiaz/static/img/posts/que-es-un-api/mastercard-blockchain-api.jpg")
```
This issue is discussed here and here which is not a bug per se. The solution which worked for me is to use:
```{r example, echo = FALSE, out.width='80%', out.height='50%'}
knitr::include_graphics(paste0(getwd(), "/images/example_image.png"))
```
If you are working within an Rstudio project, it will help to specify the directory first:
```{r example, echo = FALSE, out.width='80%', out.height='50%'}
setwd(rprojroot::find_rstudio_root_file())
knitr::include_graphics(paste0(getwd(), "/images/example_image.png"))
```
See here and here.

Rmarkdown not producing pdf

Thank you in advance for any help! I have MikTeX installed on my computer (2.9.6) and the tinytex package but Rmarkdown will not knit to a pdf (it will however for html) no matter what engine I use. I updated the packages and the other solutions seem to suggest downgrading or updating the packages so Im not sure what to do!
Output created: ttest.pdf
Error in tools::file_path_as_absolute(output_file) :
file 'ttest.pdf' does not exist
Calls: <Anonymous> -> <Anonymous>
In addition: Warning message:
In readLines(logfile) : incomplete final line found on 'ttest.log'
Execution halted
Session info
> sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
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
other attached packages:
[1] tinytex_0.5
loaded via a namespace (and not attached):
[1] compiler_3.5.0 backports_1.1.2 magrittr_1.5 rprojroot_1.3-2
htmltools_0.3.6
[6] tools_3.5.0 yaml_2.1.19 Rcpp_0.12.17 stringi_1.1.7
rmarkdown_1.10
[11] knitr_1.20 stringr_1.3.1 digest_0.6.15 evaluate_0.10.1
RMD File
---
title: "ttest"
author: "Erik Maroney"
date: "June 22, 2018"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple
formatting syntax for authoring HTML, PDF, and MS Word
documents. For more details on using R Markdown see
<http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be
generated that includes both content as well as the
output of any embedded R code chunks within the
document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```

How to solve error in beamer_presentation, file does not exist?

When knitting to beamer_presentation I'm receiving this error message, does anyone know how to read it and solve this issue?
Code:
---
title: "Untitled"
author: "Author"
date: "6 April 2018"
output: beamer_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## R Markdown
Something
Error message:
processing file: TEST.Rmd
|...................... | 33%
ordinary text without R code
|........................................... | 67%
label: setup (with options)
List of 1
$ include: logi FALSE
|.................................................................| 100%
ordinary text without R code
output file: TEST.knit.md
"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS TEST.utf8.md --to beamer --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output TEST.tex --highlight-style tango --latex-engine pdflatex --self-contained
Output created: TEST.pdf
Error in tools::file_path_as_absolute(output_file) :
file 'TEST.pdf' does not exist
Calls: <Anonymous> -> <Anonymous>
In addition: Warning message:
running command '"pdflatex" -halt-on-error -interaction=batchmode "TEST.tex"' had status 1
Execution halted
Version infos:
> sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
Matrix products: default
locale:
[1] LC_COLLATE=German_Switzerland.1252 LC_CTYPE=German_Switzerland.1252
[3] LC_MONETARY=German_Switzerland.1252 LC_NUMERIC=C
[5] LC_TIME=German_Switzerland.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] Rcpp_0.12.16 tufte_0.3 digest_0.6.15 withr_2.1.2 rprojroot_1.3-2
[6] R6_2.2.2 backports_1.1.2 git2r_0.21.0 magrittr_1.5 evaluate_0.10.1
[11] httr_1.3.1 stringi_1.1.7 curl_3.2 rmarkdown_1.9.8 devtools_1.13.5
[16] tools_3.4.4 stringr_1.3.0 rsconnect_0.8.8 yaml_2.1.18 compiler_3.4.4
[21] memoise_1.1.0 htmltools_0.3.6 knitr_1.20
Note: A few months ago this worked without problems.
According to the GitHub issue rstudio/rmarkdown#1285, there are two solutions.
1st downgrade rmarkdown
devtools::install_version("rmarkdown", version = "1.8",
repos = "http://cran.us.r-project.org")
or
2nd install/upgrade tinytex
install.packages('rmarkdown') # installs v 1.9 at the moment
devtools::install_github('yihui/tinytex')
I chose the 2nd option, works fine for me.

RMarkdown PDF "LaTeX3 error: Erroneous variable"

I've been creating PDF reports via RMarkdown for a couple months now, but after installing MikTex, R and the rmarkdown and tidyverse packages on a new machine today, I received the following error message when attemping to knit a PDF:
"C:/PROGRA~2/Pandoc/pandoc" +RTS -K512m -RTS Test.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output test.pdf --template "C:\Users\drewm\Documents\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine xelatex --variable graphics=yes --variable "geometry:margin=1in"
! Undefined control sequence.
<argument> \LaTeX3 error:
Erroneous variable \c__fontspec_shape_n_n_tl used!
l.3806 \emfontdeclare{ \emshape, \eminnershape }
pandoc.exe: Error producing PDF
Error: pandoc document conversion failed with error 43
In addition: Warning message:
running command '"C:/PROGRA~2/Pandoc/pandoc" +RTS -K512m -RTS Test.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output test.pdf --template "C:\Users\drewm\Documents\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine xelatex --variable graphics=yes --variable "geometry:margin=1in"' had status 43
I was able to recreate the error with the simple example below:
Test.R
library(rmarkdown)
library(knitr)
setwd(C:/something)
render("Test.rmd", output_format=pdf_document(latex_engine="xelatex"), output_file="test.pdf")
Test.Rmd
---
title: "Habits"
output:
pdf_document:
latex_engine: xelatex
---
Hello World!
I also see the error when I knit Test.Rmd directly in RStudio instead of using a separate .R file with render(). I also tried using the lualatex pdf engine and received the same error as above. I am able to generate PDFs using the RMarkdown PDF example built into RStudio.
This post on Sourceforge seems relevant, but not useful since the solution was to update the fontspec package and I'm already using an updated fontspec package.
Session Info
> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 15063)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] knitr_1.17 rmarkdown_1.6
loaded via a namespace (and not attached):
[1] compiler_3.4.1 backports_1.1.0 magrittr_1.5 rprojroot_1.2
[5] tools_3.4.1 htmltools_0.3.6 yaml_2.1.14 Rcpp_0.12.12
[9] stringi_1.1.5 stringr_1.2.0 digest_0.6.12 evaluate_0.10.1
Any help or advice is greatly appreciated!
The uninstall/reinstall of MikTex reverted several of the packages to outdated versions (it would be helpful if the MikTex installer prompted the user to update packages after installation). In this case, my fontspec package was reverted to 2.6a (see potential issue outlined in Sourceforge link above). After running MikTex Update to update all my installed packages. I was able to successfully produce the PDF output desired.

Error: pandoc document conversion failed with error 2 with pkgdown / rmarkdown

I'm trying to build a website with pkgdown so I ran pkgdown::build_site().
This throws the following error:
Building article 'index.html'
Error: pandoc document conversion failed with error 2
There was a similar problem on SO but the answer didn't solve my issue.
My guess was that the problem was in the header of my vignette which currently is:
output:
rmarkdown::html_vignette:
smart: false
toc: true
I tried changing it to:
output: rmarkdown::html_vignette
as it is used in the pkgdown's vignette. When I used that code, I got an error and the vignette did not build.
--smart/-S has been removed. Use +smart or -smart extension instead. For example: pandoc -f markdown+smart -t markdown-smart. Try
pandoc.exe --help for more information. Error: pandoc document
conversion failed with error 2 In addition: Warning message: running
command '"C:/PROGRA~2/Pandoc/pandoc" +RTS -K512m -RTS
Introduction.utf8.md --to html --from
markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash
--output pandoc23e071f76af6.html --smart --email-obfuscation none --self-contained --standalone --section-divs --template "C:\Users\E\Documents\R\win-library\3.4\rmarkdown\rmd\h\default.html"
--highlight-style pygments --css "C:\Users\E\Documents\R\win-library\3.4\rmarkdown\rmarkdown\templates\html_vignette\resources\vignette.css"
--mathjax --variable "mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"'
had status 2 Execution halted
I changed the header as suggested here to:
output:
html_document:
smart: false
This header knits a vignette but I still get the same pandoc error when using build_site().
Pandoc is version 2.0. sessionInfo() returns:
R version 3.4.1 (2017-06-30) Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
Matrix products: default
locale:
LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252 LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
LC_TIME=English_United Kingdom.1252
attached base packages:
stats graphics grDevices utils
datasets methods base
other attached packages:
pkgdown_0.1.0.9000 devtools_1.13.2
loaded via a namespace (and not attached): MASS_7.3-47
compiler_3.4.1 magrittr_1.5 R6_2.2.2 tools_3.4.1
withr_1.0.2 roxygen2_6.0.1 Rcpp_0.12.12 memoise_1.1.0
xml2_1.1.1 stringi_1.1.5 stringr_1.2.0 digest_0.6.12
commonmark_1.2 rlang_0.1.1.9000
Any ideas how to fix it?
EDIT:
I ran the whole thing on Ubuntu and I managed to make it work (see the repo). Still don't know what was causing the issue. Here is the output of sessionInfo():
R version 3.3.3 (2017-03-06) Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.2 LTS
locale:
LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C
LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8
LC_MONETARY=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8
LC_PAPER=en_GB.UTF-8 LC_NAME=C LC_ADDRESS=C
LC_TELEPHONE=C LC_MEASUREMENT=en_GB.UTF-8
LC_IDENTIFICATION=C
attached base packages: stats graphics grDevices utils
datasets methods base
other attached packages: pkgdown_0.1.0.9000 devtools_1.13.1
loaded via a namespace (and not attached): MASS_7.3-45
magrittr_1.5 R6_2.2.2 tools_3.3.3 withr_2.0.0
roxygen2_6.0.1 Rcpp_0.12.12 memoise_1.1.0 xml2_1.1.1
stringi_1.1.2 stringr_1.2.0 digest_0.6.12 commonmark_1.2
rlang_0.1.1
The warning is raised because the argument --smart is being used here: ... running command '"C:/PROGRA~2/Pandoc/pandoc" ... --smart ... "'. Because --smart/-S has been removed for Pandoc 2.0, that's a problem. So, remove --smart from the caller or command-line to fix this.
In Pandoc 2+, usage of smart is the same as for any other extension. To enable it, you append +smart to either the --to or the --from argument it, or possibly to both. And to disable, append -smart.
For example:
--to html+smart
Or maybe:
--from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-smart

Resources