Using knit2pdf with Rmd files - r

Is it possible to use the knitr function knit2pdf() directly with R Markdown (Rmd) files? I've seen various tutorials/class notes that seem to suggest it can e.g. here and here (Ctrl+F "knit2pdf" in either).
But when I take a simple rmd file (saved as "test.rmd")
---
title: "knit2pdf test"
author: "A Aaronson"
date: "Thursday, February 19, 2015"
output: pdf_document
---
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.
and try
library(knitr)
knit2pdf("test.Rmd")
I get the following error
results in:
output file: test.md
Error: running 'texi2dvi' on 'test.md' failed
LaTeX errors:
! Emergency stop
*** (job aborted, no legal \end found)
! ==> Fatal error occurred, no output PDF file produced!
! ==> Fatal error occurred, no output PDF file produced!
In addition: Warning message:
running command '"C:\PROGRA~2\MIKTEX~1.9\miktex\bin\texi2dvi.exe" --quiet --pdf "test.md" --max-iterations=20 -I "C:/PROGRA~1/R/R-31~1.2/share/texmf/tex/latex" -I "C:/PROGRA~1/R/R-31~1.2/share/texmf/bibtex/bst"' had status 1
Clicking the "Knit PDF" button always successfully generates a pdf. So am I missing an intermediate step?
I should add that knit2pdf() with Rnw files is working as expected for me, though I do still get the warning
running command '"C:\PROGRA~2\MIKTEX~1.9\miktex\bin\texi2dvi.exe" --quiet --pdf "rnwtest.tex" --max-iterations=20 -I "C:/PROGRA~1/R/R-31~1.2/share/texmf/tex/latex" -I "C:/PROGRA~1/R/R-31~1.2/share/texmf/bibtex/bst"' had status 1
Help greatly appreciated.

Your input file is in rmarkdown format.
You should use the render() function in the rmarkdown package to compile your document.
Try:
library("rmarkdown")
render("temp.rmd")

Related

How do you compile to pdf files in R? Problem

I have a similar problem as already described, but the answers do not help me, that is why I am writing one more post.
I try to compile a tex file (a standard example from the web) in R, but R cannot file the pdflatex file:
Error running C:/Users/vica/AppData/Local/Programs/MiKTeX/miktex/bin/x64/pdflatex.exe (exit code 1)
I tried
Sys.setenv(PATH = paste(Sys.getenv("PATH"), "C:/Users/vica/AppData/Local/Programs/MiKTeX/miktex/bin/x64/pdflatex", sep=.Platform$path.sep))
or
Sys.setenv(PATH = paste(Sys.getenv("PATH"), "C:\\Users\\vica\\AppData\\Local\\Programs\\MiKTeX\\miktex\\bin\\x64\\pdflatex", sep=.Platform$path.sep))
(if I am wrong with the syntax), but no effect.
I added a Renviron.site file in the respective directory, writing this in it:
PATH=C:\\Users\\vica\\AppData\\Local\\Programs\\MiKTeX\\miktex\\bin\\x64\\pdflatex;"${PATH}"
but no effect. When I write
Sys.getenv("PATH")
I get something strange:
Sys.getenv("PATH")
[1] "C:\\Users\\vica\\Documents\\R\\R-4.0.3\\bin\\x64;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Users\\vica\\AppData\\Local\\Programs\\Python\\Python39\\Scripts\\;C:\\Users\\vica\\AppData\\Local\\Programs\\Python\\Python39\\;C:\\Users\\vica\\anaconda3;C:\\Users\\vica\\anaconda3\\Library\\mingw-w64\\bin;C:\\Users\\vica\\anaconda3\\Library\\usr\\bin;C:\\Users\\vica\\anaconda3\\Library\\bin;C:\\Users\\vica\\anaconda3\\Scripts;C:\\Users\\vica\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\vica\\AppData\\Local\\Programs\\Git\\cmd;C:\\Users\\vica\\AppData\\Local\\Programs\\Microsoft VS Code\\bin;C:\\Users\\vica\\AppData\\Local\\Programs\\MiKTeX\\miktex\\bin\\x64\\;C:\\Users\\vica\\AppData\\Roaming\\TinyTeX\\bin\\win32;C:/Users/vica/AppData/Local/Programs/MiKTeX/miktex/bin/x64/pdflatex"
>
I tried to compile the sweave file (exa3.tex), this is what I got:
Writing to file exa3.tex Processing code chunks with options ... You can now run (pdf)latex on 'exa3.tex'
Running pdflatex.exe on exa3.tex...failed Error running C:/Users/vica/AppData/Local/Programs/MiKTeX/miktex/bin/x64/pdflatex.exe (exit code -1073740791
But when I KNIT the Rnw file, I get the pdf. This is my exa3.Rnw:
---
title: "MyFile1"
author: "Victoria"
date: "6 5 2021"
output: pdf_document
---
## R Markdown
Hello, World!
This is my tex file:
\documentclass[a4paper]{article}
\SweaveOpts{concordance=TRUE}
\title{Sweave Example 1}
\author{Victoria Knopova}
\usepackage{Sweave}
\begin{document}
\maketitle
In this example we embed parts of the examples from the
\texttt{kruskal.test} help page into a \LaTeX{} document:
\end{document}
Maybe I do something entirely wrong when I compile *.tex files, but I do not understand what, I only learn R...
Can somebody help me?
Thank you in advance.
Victoria
**PS: I found another way: one can use this hint
Rmarkdown to LaTeX
in order to translate a *.Rmd file to *.tex
**

Using pandoc-crossref with R bookdown

Is it possible to use pandoc-crossref in bookdown?
I tried to change the yaml header to:
output:
bookdown::tufte_book2:
toc: yes
highlight: tango
pandoc_args: -F /usr/local/bin/pandoc-crossref
Which should pass the filter to pandoc, but I get the error:
pandoc: Error running filter pandoc-crossref:
Could not find executable ' pandoc-crossref'.
The above error does not make sense, as I entered the correct path. What kind of env is bookdown using, which is precluding the access to the filter file?
Here is an example
---
output: bookdown::html_document2
---
# Section name {#id}
```{r pressure, echo=FALSE, fig.cap='test plot'}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot. To cross-reference the figure, use `\#ref(fig:pressure)` to produce Figure \#ref(fig:pressure). All this is found within the section \#ref(id).
Which produces...
See https://bookdown.org/yihui/bookdown/figures.html for the official documentation.
I had a similar issue while I was trying to put numbers to equations in Word files (SO question). I got the same error Could not find executable 'pandoc-crossref'.
My RStudio installation (on Windows) did not come with pandoc-crossref. Here is what I did:
Downloaded pandoc-crossref from here.
Find the path where RStudio saved the file pandoc.exe:
rmarkdown::find_pandoc()
Put the pandoc-crossref.exe in the folder I got in (2).

Knitr HTML preview fails in RStudio, though render() successfully creates HTML file

I'm getting started with rmarkdown and knitr. In the sample document provided by RStudio, I can successfully use render() to generate an HTML file that views fine in Chrome. However, when I click on the knit button, it generates a .markdown file and then returns the following error without rendering the preview:
Error generating HTML preview for ~/path/to/file/report.rmarkdown system error 2 (The system cannot find the file specified)
I think it's getting hung up at the pandoc stage. Is it possible that RStudio is looking for pandoc in the wrong place? Pandoc was already installed at C:\Program Files (x86)\Pandoc\pandoc.exe, but RStudio installed its own instance at C:\Program Files\RStudio\bin\pandoc\pandoc.exe, so maybe it's looking in the wrong place and/or confusing settings from one with the other?
Any help would be greatly appreciated. Thanks!
And just in case, here's the RMarkdown template I'm starting with:
---
title: "Monthly Report"
author: "Kris Shaffer"
date: "February 17, 2017"
output: html_document
---
```{r setup, include=FALSE}
library(knitr)
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)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
R version 3.3.2
RStudio 1.0.136
rmarkdown 1.3
knitr 1.15.1
pandoc 1.17.2 (both installations)
Turns out it was as simple as a file extension. I changed the filename from report.rmarkdown to report.Rmd. Knit works fine now.

Why does the test-RMarkdown in R-Studio give an html that does not show any results after pressing the knit-button?

I opened a new R Markdown file in R studio and got the default small working example.
---
title: "test"
author: "Katharina Zweig"
date: "30. Januar 2016"
output: html_document
---
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.
It says, you only need to press the knit button to create an HTML
containg the text, the code and the results of the code. I got some long
error logs that were hardly helpful. Neither did changing the output to
PDF and Word - same result: text was there, code was there, no results of
running the code. By producing the output, the original file vanished.
What is wrong?
When the knit button is used on a file not yet saved, it asks you under which name to save it. The file needs to be saved as an Rmd file - just give no extension and R-Studio will do it right. Then, the file does not vanish and the resulting document contains the results of the r commands. I thought it asked where to save the output and gave it the extension of the output file, i.e., either myfile.html / myfile.pdf / myfile.doc.
In the chunk option try this:
{r, results='asis'}
summary(cars)
You can also embed plots, for example:
{r, echo=FALSE, results='asis'}
plot(cars)
The results = 'asis' command should output the tables and graphs if not please let me know.

Could not find image while rendering pdf in R markdown

I am trying to render a pdf file using R markdown. I want to embed an image on the page and using the following syntax:
image: ![](logo.png)
While creating the pdf, I get an error saying
`pandoc.exe: Could not find image logo.png, skipping...
pandoc.exe: Error producing PDF from TeX source.
! Package pdftex.def Error: File logo.png not found.`
Error: pandoc document conversion failed with error 43
The image exists, and don't know why it is failing. I even gave the exact path with drive and folder names, but still fails.
My File is simple as:
---
title: 'Test'
author: "Foo"
date: "Wednesday, December 17, 2014"
output: pdf_document
---
```{r}
summary(cars)
```
image: ![](logo.png)

Resources