Cannot include figures via R/Sweave/LaTeX - r

I am trying to create a pdf vs R/Sweave/LaTeX that will include a plot. Here is the relevant snippet from my .Rnw file:
\begin{figure}
\begin{center}
<<fig=TRUE,echo=FALSE>>=
makeMyPlot()
#
\end{center}
\label{fig:one}
\end{figure}
When I run Sweave it generates a .pdf file named "analysis report-005.pdf" and a .tex file including the following line:
\includegraphics{analysis report-005.pdf}
So far, so good. But when I run
tools::texi2pdf('analysis report.tex')
I get this error
Error: running 'texi2dvi' on 'analysis report.tex' failed
LaTeX errors:
:218: LaTeX Error: File `analysis report-005' not found.
It looks like texi2pdf is failing to find the file because the extension, ".pdf", is not stated in the .tex file output. When I add ".pdf" directly to the relevant line of the .tex file, the error goes away. However, I shouldn't need to do this manually. I'm not sure if this is a problem with Sweave or texi2pdf; I suspect the latter. How can I fix it?

Related

Rmd: \input LaTeX equivalent

LaTeX function
\documentclass{article}
\begin{document}
\input{folder/file.tex}
\end{document}
\input{folder/file.tex} command in LaTeX fetches the text in the file from the folder. When the main file is run, all the text from file.tex is compiled and included.
Rmd - desired output
I am using R Markdown for my project. I desire to develop and document different topics in different files.
I want to have the following directory structure:
main.Rmd
main_directory
code
file1.Rmd
file2.Rmd
figures
data
read_me
Explored pathways
I have explored the following routes to achieve this.
As suggested in Link 1
sys.source(file = file.path(getwd(), "main_directory",
"code",
"file1.r"))
Here, the code text is not included in the output HTML knit from the Rmd.
As suggested in Link 2
```{r, child=file.path(getwd(), "main_directory", "code", "file1.Rmd"}
```
It led to the following error:
Error: attempt to use zero-length variable name
Here, the code text is not included in the output HTML knit from the Rmd.
As suggested in Link 3
```{r}
includeMarkdown(file.path(getwd(), "main_directory", "code", "file1.Rmd"))
```
It also led to the following error:
Error: attempt to use zero-length variable name
Question: How to include the Rmd code into main file similar to LaTeX input

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
**

Rmarkdown, pandoc/PDFlatex, and underscores in a temporary directory name - a problem

I have an R markdown document that has a chunk that generates a plot using ggplot2 and a custom function, progress.plot. The chunk looks like this
```{r charts, echo=FALSE, fig.cap="Your progress curve"}
progress.plot(student)
```
This document generates a PDF using a LaTeX engine. I think it uses PDFlatex via pandoc. This is causing a problem.
It seems like Rmarkdown uses a temporary folder to store the diagram I generate with ggplot2. The name of that folder has underscores. This means the path to the graphic file, a string that's used in the .tex source file, has an underscore. And the latex compiler doesn't like that.
! Missing $ inserted.
<inserted text>
$
l.142 ...de2020-05-06_files/figure-latex/charts-1}
\caption[Your progress cu...
I am getting a "Missing $ inserted" error before the latex compiler craps out. No PDF is generated and the directory with the figure is deleted. (I have experience with LaTeX, so I'm familiar with this type of troubleshooting.)
Is there a way to take the underscore out of the temporary directory's name? Or is there any other workaround that will allow me to generate figures to include in my R markdown document?
Thanks for whatever advice people can share.

Convert: no decode delegate errors in Rmarkdown

After updating my R installation on my Mac with a couple of packages (most recently, RODBC), when I knit .Rmd files to pdf, I get error such as the one below.
convert: no decode delegate for this image format `images.trial/pressure-1.png'.
convert: missing an image filename
It occurs on .Rmd files that I've written on a Windows machine, and on a standard .Rmd file that I created from the base template (File>New File>R markdown...), with an initial r chunk that I've used to set knitr chunk options:
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,fig.path="images/",dev="png")
```
If I exclude that knitr::opts_chunk code when I knit to pdf, I get no errors; if I knit to html with that knitr:opts_chunk intact, I get no errors.
I've run this on .Rmd files that have worked without errors in the past and on .Rmd files that were created directly from the template.

What are the commands executed if I press the "Compile PDF" button on a "Rnw" file in RStudio?

Once again I am in the situation that I want to replicate what is happening when I press the Compile PDF button on an .Rnw file in RStudio with my own R script.
For example I create a new .Rnw file in RStudio with File > New File > R Sweave. It looks like this:
\documentclass{article}
\begin{document}
\SweaveOpts{concordance=TRUE}
<<echo=F, results=tex>>=
library(xtable)
xtable(mtcars)
#
\end{document}
I inserted a chunk with alt+cmd+i and used the autocompletion to set the chunk options. Hence I assume I did everything with the default settings as RStudio assumes it to be. When I press Compile PDF everything works without problems. But when I execute:
knitr::knit2pdf("Sweave-test.Rnw")
I get an Error "It seems you are using the Sweave-specific syntax". Hence there are additional steps needed.
What I came up with so far is the following:
library(knitr)
tempfile1 <- tempfile(fileext=".Rnw")
Sweave2knitr(file = "input.Rnw", output = tempfile1)
tempfile2 <- tempfile(fileext=".tex")
knit(tempfile1, output=tempfile2)
tools::texi2pdf(tempfile2, clean=T)
system(paste("open", sub(".tex", ".pdf", basename(tempfile2))))
(The last line is OSX specific I think).
But I am curious to know what RStudio is doing exactly. I looked into the RStudio github page but am not sure where to find the command. Other Stackoverlow questions show that there are slight differences between what the button does and knit2pdf. It seems the question has been asked over and over again, also in relation to the Knit Html button. It might use functions from the knitr package, the markdown, the rmarkdown package, texi2pdf from the tools package, Sweave from the utils package or pandoc. I have no Idea...
Related question (that all got some rather vague answers):
Difference: "Compile PDF" button in RStudio vs. knit() and knit2pdf()
Difference between "Compile PDF" and knit2pdf
How to convert R Markdown to HTML? I.e., What does "Knit HTML" do in Rstudio 0.96?
What does “Knit HTML” do in Rstudio 0.98?
Compile .Rnw file with command
How to build Knitr document from the command line
Currently I am using RStudio 1.0.136.
I suspect it is just Sweave to turn a Rnw file into tex file and then calling pdflatex.exe to turn it from a tex file into a pdf file.
Sweave("Sweave-test.Rnw")
system("pdflatex.exe Sweave-test.tex")
And if you encountered a File Sweave.sty not found, you will need to add a path this file in your MiKTeX Options, please see here.

Resources