MiKTeX failing when knitting rmarkdown document - r

When I try to knot a rmarkdown word document I get the following error.
Sorry, but C:\Users\A12345\AppData\Local\Programs\MIKTEX~1.9\miktex\bin\x64\pdflatex.exe did not succeed.
This is a old version and location which is not used. I tried changing to the new by running the following after opening RStudio
Sys.setenv(PATH = paste(Sys.getenv("PATH"), "C:/Program Files/MiKTeX 2.9/miktex/bin/x64", sep=.Platform$path.sep))
I also tried putting above statement in the beginning of the rmarkdown document, still getting the same error.
How can I actually change the Miktex location?

Related

Having trouble loading an .RDS file and knitting an Rmarkdown

I am attempting to load an RDS file into an RMarkdown using the following code:
protests <- readRDS("protests.RDS")
and I get the error:
Error in gzfile(file, "rb") : cannot open the connection :
I had this file loaded before and it was working, but when I attempted to knit my RMarkdown I got the Error while opening file: no such file or directory notice
In attempting to fix that I typed the code to load the RDS file into the RMarkdown (it was previously in another document) and then it started giving me the above error. And then my code wouldn't work with that dataset.
I'e tried numerous ways to set the working directory and reload the file into R. Sometimes I can't get the file to load, sometimes it loads but gives me a vec_size error whenever I use the dataset, and sometimes the code works but my RMarkdown still won't know.
Currently I've got the dataset to load into the environment by typing the readRDS() into a different RMarkdown (but the readRDS() in my first RMarkdown still won't work despite being the exact same) and the code in the first RMarkdown in working, but it won't knit.
Does anybody have an idea what is going on?

Rmarkdown does not follow same paths as R script and console commands

In Rmarkdown, I cannot read files that I can read from the console and in an R script, because Rmarkdown is not following the same paths as my R scripts and console commands.
Here is a minimum reproducible example:
Create new project test.Rproj
Create a subdirectory called scripts
Run the following R Script scripts/test.R:
test <- as.data.frame(c(1, 2, 3))
dir.create("data")
write.csv(test, "data/test.csv")
rm(test)
test <- read.csv("data/test.csv")
Quit R, and reopen test.Rproj.
Knit the following Rmarkdown document (scripts/test.Rmd):
---
title: "test"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
_```
```{r read-data}
test <- read.csv("data/test.csv")
_```
Yields the following error: Quitting from lines 12-13 (test.Rmd)
Error in file(file, "rt") : cannot open the connection
Calls: ... withVisible -> eval -> eval -> read.csv -> read.table -> file
Execution halted
(Note, the backticks in the .Rmd file are properly specified -- I added underscores above so that the backticks appeared in the code block.)
Two seemingly related issues:
I can read the test.csv file via Rmarkdown if it is in the scripts subdirectory, rather than the data subdirectory.
When I run list.files() from the console or script, I receive of list of files in the top-level directory (i.e., where test.Rproj is located), including the data and scripts subdirectories. When I run list.files() from Rmarkdown, I get a list of files in the scripts subdirectory.
How can I fix this problem?
Session info:
R version 4.1.0 (2021-05-18)
RStudio version 1.4.1717
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.5.1
Try something that looks like this as I am not sure of the nature of your `R Markdown.
test <- readRDS(here::here("data/test_data.rds"))
The bottom line is to use the here function from the here package.
When you knit a document in RStudio, by default the working directory is set to the current directory of the Rmd document (so that would be the "scripts" folder). Since the "scripts" folder does not contain the "data" directory, you get that error. You can change the default to use the project root directory if you prefer. That's an option in the RStudio Global Options menu.
See See https://bookdown.org/yihui/rmarkdown-cookbook/working-directory.html for more info

Error in R while compiling the markdown

I am trying to save a xgboost code in Rmarkdown to knit HTML document. I am getting an error.
The code is completely executable in "R".
Fatal error: creation of tmpfile failed -- set TMPDIR suitably?
The error is same when I try to run knit Pdf/word.
Someone please help me.
Thank you
It seems the temporary directory cannot be created. Trying making your own temporary directory or setting it to the working directory:
Sys.setenv(TMPDIR = ".")

knit2pdf failing to produce pdf in Shiny Apps

As the title states, I am attempting create a shiny application that can generate a pdf file that a user can then download. (Note that this is not a duplicate question because I have not found a question with a similar error)
In my search for a solution, I found this gist by the Knitr package author: https://gist.github.com/yihui/6091942, which I am attempting to recreate. There are also questions on S.O that use the same code, out = knit2pdf('input.Rnw', clean = TRUE) but all of them seem to date to before the 3.2.3 release of R.
The error I am given is:
output file: input.tex
Warning: running command '"pdflatex" -interaction=nonstopmode "input.tex"' had status 1
Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, :
unable to run 'pdflatex' on 'input.tex'
Warning: Error in texi2dvi: unable to run 'pdflatex' on 'input.tex'
Stack trace (innermost first):
52: texi2dvi
51: tools::texi2pdf
50: knit2pdf
49: download$func [D:...\App-2/server.R#44]
1: shiny::runApp
What I have tried so far:
Updating R, RStudio, knitr package and my TeX distribution (TeXLive). I know that TeXLive is not the issue as I use it daily, and I can create a dummy .Rnw that will compile perfectly using the Compile PDF button in R Studio. I have also tried using knit() or render() and those will generate .tex files without giving an error. I have also changed my working directory to the app folder as suggested by another S.O. question, but that didn't solve my problem either.
Edit, Additional information: I am using Windows 10
Any suggestions welcome!
I don't think TeXLive is working completely. On Windows, RStudio recommends using MiKTeX, and so does Pandoc.

How to identify LaTeX errors in .Rd R help files? [duplicate]

When building a package, I received the following warning:
* checking PDF version of manual ... WARNING
LaTeX errors when creating PDF version.
This typically indicates Rd problems.
I have no idea how to even begin diagnosing this. Is there a tool that tells me what .Rd file the problem is in?
I get no warnings about any of my Rd files in the checking documentation step....
Try R CMD Rd2pdf mypackage to create the manual, and possibly also set the --no-clean option to keep the temporary files. This should allow you to debug the LaTeX code triggering the error.
Though #Dirk's answer also helped me to fix the problem I would like to add a bit which might especially help recent updaters. That is, people who haven't encountered other LaTeX / R troubles after the update to 3.1.3 yet. The problem is little bit more general than just building. For me, on OS X the problem was that R CMD Rd2pdf as well as the R CMD CHECK expected texi2dvi to be in /usr/local/bin while it was in /usr/bin.
A symlink helped to fix the problem. On terminal type:
# to check whether the same issue exists for you
which texi2dvi
# if so
cd /usr/local/bin
ln -s /usr/bin/texi2dvi
Of course if the first line returns something else, you need to adapt the symlink here.
Concluding from the comments and from my own experience the problem often seems to be that some TeX fonts are missing, most often
inconsolata.sty and
upquote.sty
First you have to find the right directory where TeX fonts are stored - in my case this is:
C:\Program Files\R\R-3.3.0\share\texmf\tex\latex
Then you can download them here:
https://www.ctan.org/tex-archive/fonts/inconsolata/?lang=en
https://www.ctan.org/tex-archive/macros/latex/contrib/upquote?lang=en
Just copy them to the respective folder and in many cases the problem will be solved (in my case too). This should work for all operating systems.
...and another reason is that you haven't installed MikTex yet.
Download MikTex from here and follow the dialog prompts to install. I found the defaults to be reasonable and worked well for me.
Try to build your R package again. It should be OK now.
In my case, I had no error when running devtools::check() nor devtools::document() but when running R CMD check mypackage_version.tar.gz I got an error:
* checking PDF version of manual ... WARNING
LaTeX errors when creating PDF version.
This typically indicates Rd problems.
LaTeX errors found:
* checking PDF version of manual without hyperrefs or index ... ERROR
In this question in RStudio Community they point to a problem with LATEX installation.
I have the LATEX installation suggested in R markdown cookbook: TinyTex.
I fixed the issue by running in the R console
tinytex::latexmk(file = "../mypackage.Rcheck/mypackage-manual.tex")
This command automatically updated my LATEX installation so the output file mypackage-manual.pdf was created. After this, I did not get any other error related to PDF when running R CMD check:
* checking PDF version of manual ... OK
* DONE
If you are on Ubuntu just install Tex Live by this command:
apt-get install texlive and restart Rstudo if you use it.
First, #dirk-eddelbuettel's approach in the current question identified the missing tex package (which was "makeindex" in my case).
system("R CMD Rd2pdf --no-preview --output=./documentation-peek.pdf ." )
# ... <omitted pages of output> ...
# Warning in sys2(makeindex, shQuote(idxfile)) : '"makeindex"' not found
# Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, :
# unable to run 'makeindex' on 'Rd2.idx'
# Error in running tools::texi2pdf()
Then #pedro-lima's answer in https://stackoverflow.com/a/69968791/1082435 worked for my specific case.
tinytex::tlmgr_install("makeindex")
Oftentimes this error occur because of Unicode characters in the package. In this cases, the error message might look like this.
See the inputenc package documentation for explanation.
Type H <return> for immediate help.
! Package inputenc Error: Unicode character (U+009D)
(inputenc) not set up for use with LaTeX.
You can find any Unicode characters in your package using tools::showNonASCIIfile(). Here's a simple way to check for these characters in your functions and documentation:
# functions
functions <- list.files(path = './R', all.files = T, recursive = T, full.names = T)
lapply(X=functions, FUN = tools::showNonASCIIfile)
# documentation
docs <- list.files(path = './man', all.files = T, recursive = T, full.names = T)
lapply(X=docs, FUN = tools::showNonASCIIfile)

Resources