Sweave2Knitr Code - r

I keep getting an error message when attempting to compile a pdf that reads:
It seems you are using the Sweave-specific syntax in line(s) 85; you may
need Sweave2knitr("Filename.Rnw") to convert it to knitr. Running
pdflatex.exe on File_Name_Update_FINAL.tex ...Running pdflatex.exe on
File_Name_Update_FINAL.tex ...failed
I have checked my settings in tools>global options>sweave and they are set to
Weave rnw files using: knitr
And checked box in always enable Rnw concordance.
I have also unchecked and checked the enable concordance box.
I have attached the code below. It starts on line 85, the line of code with the error.
\begin{document}
\SweaveOpts{concordance=TRUE}
%\SweaveOpts{concordance=TRUE}
Any help would be great. Thanks!

Related

Trying to render R4DS to pdf

When I try to render the latest version of the book R for Data Science (R4DS), I get as far as LaTeX compilation, then am stopped by the following error message.
! Text line contains an invalid character.
l.406 #> -- ^^[
[1mAttaching packages^^[[22m --------------------------------...
Error: LaTeX failed to compile _main.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See _main.log for more info.
>
This corresponds to the part of the R4DS book where we are shown how to load the tidyverse and, looking at the _main.tex file, I see many lines with what look like ANSI escape sequences starting on this line. They have the form ^[[1m, ^[[22m, and so on. I manually compiled the LaTeX output using lualatex and found that there are dozens if not hundreds of examples of this throughout the book. I suspected it was because I was using the colorout package in R, but it appears that that package is required, so others who are rendering successfully must be using it too. I believe I have successfully updated all relevant packages.
It looks like I "solved" the problem by changing an option in the _common.R file from crayon.enabled=TRUE to crayon.enabled=FALSE. This removed the ANSI escape sequences from the book. Previously I had tried setting options(crayon.enabled=FALSE) in my R session, but this was evidently being overridden by the setting in _common.R.
Update: 23 Nov 2022
The process for rendering the files is completely different now because of the switch to Quarto. Here's how I did it.
Rscript -e 'update.packages()'
Rscript -e 'install.packages('quarto')'
Rscript -e 'devtools::install_github("hadley/r4ds")
git clone https://github.com/hadley/r4ds.git
cd r4ds
Next, I wrote a small perl script to avoid the error messages I was getting about trying to render html material to pdf. (I'm omitting a lot of dead-ends I encountered in the process.)
#!/usr/bin/perl
use File::Slurp qw(prepend_file);
my #files = glob( '*.qmd' );
my $header = "\n---\nprefer-html: true\n---\n\n";
foreach my $file (#files) {
prepend_file($file, $header);
}
I ran the above script in the r4ds directory.
Next I loaded R and did the following:
library(quarto)
quarto_render("index.qmd", output_format = "pdf")
The above failed with the error message: "\begin{document} not found". Luckily, the aborted process leaves an index.tex file I can process and also gives a line number for the error. I went to that line number in the index.tex file and deleted the block of html I found there.
After that, I ran
lualatex index.tex
twice and got a successful render, minus the cover page. (You could presumably run xelatex index.tex instead.) There are a lot of problems with my render, such as the plots being too large to fit on the page. If I decide to spend time fixing them (unlikely, since Hadley seems to want us to use the online version) I'll modify this answer.

xelatex, luatex issues when building pdf of demo book

I can build the gitbook of the demo just fine.
If I attempt to build a pdf, the .tex file is made by pandoc, but xelatex compilation bombs. I get a message in the Build window that's incomplete.
If I manually compile in the shell, I get the following output. Short story-- it would seem that there's code somewhere, probably in pandoc, that uses
\xetex_if_engine:F and
\luatex_if_engine:TF
which apparently have been non-functional since 1-1-2017.
But I can't be the first person to try to compile a pdf since January. Any ideas?
The LaTeX log shows LaTeX2e <2011/06/27>, which is too old, and you will need to refresh your filename database (FNDB) as instructed here,

Rmarkdown - Run code and display errors in document

I get an error message when running the code below which is expected - I would like this to be ran and displayed in the pdf, however the error is returned in r and the code doesn't run.
{r, warning=TRUE}
library(survey)
debug(withReplicates.svyrep.design)
I have tried warning=TRUE but this doesn't work.
How can I get the error displayed in the document?
Add error=TRUE to the chunk options. That will result in the script running to completion with the error appearing in the output.

how to tell RStudio where miktex files are located needed for generating pdf from rmardown? [duplicate]

I am trying to render a pdf document with the knit pdf button in RStudio, but I keep on getting this error:
! pdfTeX error (font expansion): auto expansion is only possible with scalable
fonts.
\AtBegShi#Output ...ipout \box \AtBeginShipoutBox
\fi \fi
l.167
pandoc.exe: Error producing PDF from TeX source
Error: pandoc document conversion failed with error 43
I know there is a lot be found on the internet regarding error 43, but non of them helped me.
Does anyone know what it means exactly and where I can find the solution? It's peculiar because a few hours ago I had the same error but the rendering went well after adding this in my code:
Sys.setenv(PATH = paste(Sys.getenv("PATH"), "C:\\Program Files\\MiKTeX 2.9\\miktex\\bin\\x64\\", sep=.Platform$path.sep))
But now the error is back
EDIT
when trying a minimal example:
---
title: "test"
output: pdf_document
---
```{r results="asis"}
library(stargazer)
c1 <- c("test1","test1","test2","test2")
c2 <- c(1,2,3,4)
data_object <- as.data.frame(cbind(c1,c2))
names(data_object) <- c("test","test2")
stargazer(data_object,summary=FALSE,rownames=FALSE,type="html")
```
The error it gives is:
!pdfTeX error: pdflatex (file ec-lmr10): Font ec-lmr10 at 600 not found
==> Fatal error occurred, no output PDF file produced!
Trying to make PK font ec-lmr10 at 600 DPI...
Running miktex-makemf.exe...
miktex-makemf: The ec-lmr source file could not be found.
miktex-makepk: The application file ttf2pk.exe could not be found.
pandoc.exe: Error producing PDF from TeX source
Error: pandoc document conversion failed with error 43
This is a different error, but still a 43 one. I checked it, but the font ec-lmr10 file is on the system. Somehow it can't be found.
The problem you have is a font related one. When you knit a PDF, knitr uses LaTeX and you have to know that "fonts" of LaTeX are not like the standard ones.
To solve your problem try to add the latin model font family by loading the package (at the very top):
\usepackage{lmodern}
For more detailed informations about how and where insert this piece of code in your document, have a look here.
This package allows you to have scalable fonts; not all fonts of all (font) packages are to so.
I think, but I might be wrong, because first versions of TeX and LaTeX were designed to work with METAFONT.
Edit
Your code works well for me, it does not produce any error. My idea is that happens because I have a complete Tex live distribution on my Mac (MacTeX). At this point I suppose that the problem relies on some packages that the r packages you use (like stargazer) use and you (we) do not know and won't (unless we analyze the code of course).
At this point my suggestion is to download a complete TeX distribution so you will be sure that any error related to PDF output is no related to the underlying tool that Studio uses to produce such PDFs (say, LaTeX).
You can find a complete LaTeX distribution for Mac and for Windows.

Knitr preamble error results in "pandoc document conversion failed with error 43"

Rstudio Version 0.99.441; Windows 7 enterprise; knitr 1.10.5; MiKTex 0.4.5 r.1280 (2.9 64-bit)
I have a an error on my office computer when trying to knit PDF's in Rstudio, there are no problems knitting to HTML or word. The problem may be related to updates because knitting used to work fine and my home computer can knit to pdf without issue, however I took my computer home to update and the problem is still there.
My error message was as follows
"pandoc.exe: Error producing PDF from TeX source
Error: pandoc document conversion failed with error 43"
I created a new file using the template example in Rstudio attempted to knit, then inspected the TEX output. I traced the error to the last paragraph of the preamble.
\setlength{\droptitle}{-2em}
\title{Untitled}
\pretitle{\vspace{\droptitle}\centering\huge}
\posttitle{\par}
\author{user name}
\preauthor{\centering\large\emph}
\postauthor{\par}
\predate{\centering\large\emph}
\postdate{\par}
\date{\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\setcounter{enumi}{14}
\itemsep1pt\parskip0pt\parsep0pt
\item
juni 2015
\end{enumerate}}
by removing the last 7 lines to do with enumerate and the date then running the TEX code the document prints as a PDF. Why is this happening and what can I do about it so the document knits at first click from Rstudio?
You should show the YAML frontmatter of the R Markdown document. Guessing from my experience, the error was caused by
---
date: "14. Juni 2015"
---
Any number followed by a period (e.g. 14.) will be treated as an item in a numbered list. You need to either remove or escape the period . (by \\.) in the date field. The same issue has been reported before.
This error can also be form having a curly quote or certain other characters. I spent ages on this pandoc error 43 before realising it was a block of text I had copied over form word.
e.g. Bob’s vs bob's
Another way to create this error is from having 'æ', 'ø' or 'å' in the file name.
My screw-up that resulted in this error was forgetting to put dollar signs around some inline latex code. I did
\theta
Instead of
$\theta$

Resources