Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error 43 while knitting a r markdown to pdf on rStudio on Windows

I'm running a rStudio project on the Windows of a colleague (I work on mac, so I'm not in my waters).

When I tried to compile to pdf an .rmd file I get this error:

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS Plot_per_DPUO.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output "Grafici Professioni/Periodo apr-14 ott-15 test/DH ALLERGOLOGIA - SAN PAOLO.pdf" --template "C:\Users\lucilla.rava\Documents\R\win-library\3.1\rmarkdown\rmd\latex\default.tex" --highlight-style tango --latex-engine pdflatex --variable "geometry:margin=1in" 
pandoc.exe: Could not find image `Grafici%20Professioni\Periodo%20apr-14%20ott-15%20test\DH%20ALLERGOLOGIA%20-%20SAN%20PAOLO_files/figure-latex/set-options-1.pdf', skipping...
pandoc.exe: Error producing PDF from TeX source.
! I can't find file `C:/Users/LUCILL'.
<to be read again> 
                   \protect 
<*> C:/Users/LUCILL~
                    1.RAV/AppData/Local/Temp/tex2pdf.3172/input.tex
Please type another input file name
! Emergency stop.
<to be read again> 
                   \protect 
<*> C:/Users/LUCILL~
                    1.RAV/AppData/Local/Temp/tex2pdf.3172/input.tex
!  ==> Fatal error occurred, no output PDF file produced!
Transcript written on C:/Users/LUCILL~1.RAV/AppData/Local/Temp/tex2pdf.3172/tex
put.log.

 Show Traceback

 Rerun with Debug
 Error: pandoc document conversion failed with error 43 

I left the full paths because I noticed something strange in the user path. The log prints this snippet:

`C:/Users/LUCILL'.
    <to be read again> 
                       \protect 
    <*> C:/Users/LUCILL~
                        1.RAV

where the user name is different from how it should be (lucilla.rava).

On mac the compiling doesn't give errors, so the problem shouldn't be in the code.

Of Note: I created the project on my Mac in a folder shared via google drive.

UPDATE 1: Thanks to @Gnusper we noticed that there is a path separator error. Unix and Windows paths are mixed. I need a way to remove the mixing.

In my code I defined the paths with the following code:

    months.vec <- get.months(unique(Data$Periodo))
    months.vec <- format(months.vec, '%b-%y')

    if (type == 1) folder.name <- 'Grafici Generali'
    else if (type == 2) folder.name <- 'Grafici Professioni'
    else if (type == 3) folder.name <- 'Grafici Indicazioni'

    folder.path <- file.path(folder.name, sprintf('Periodo %s %s%s', months.vec[1], tail(months.vec, 1), test))

    dir.create(folder.path, recursive = T, showWarnings = F)

    for (UO in unique(Data$UO)[1:5]) {
        print(file.path(folder.path, sprintf('%s.pdf', sub(pattern = "/", replacement = ' ', UO, fixed = T))))
        render('Plot per DPUO.Rmd', clean = T, output_file = file.path(folder.path, sprintf('%s.pdf', sub(pattern = "/", replacement = ' ', UO, fixed = T))))
    }

I made some test with a brand new R project on the windows machine with these results:

> file.path('affs', '0sdfsdf')
[1] "affs/0sdfsdf"
> .Platform$file.sep
[1] "/"
> 

The path separator is still Unix like. Reading the help of file.path I found this:

file.path(..., fsep = .Platform$file.sep)

[...]

Note

The components are by default separated by / (not ) on Windows.

For some reason the function implements the Unix separator also on windows... I tried then to run .Platform$file.sep <- '\\'. But when I use file.path() I still get the '/' separator. So fsep = .Platform$file.sep in the arguments it's not evaluated each time the function is run.

In the new project I made more tests:

dir.create(file.path('ssdg','fgsfdg'), recursive = T, showWarnings = F)
write(c(1,2,3,4,5), file = 'ssdg/fgsfdg/test.txt')
write(c(1,2,3,4,5), file = 'ssdg\\fgsfdg\\test2.txt')
write(c(1,2,3,4,5), file = 'ssdg/fgsfdg\\test3.txt')

all of them work and create the file. I also tried dir.create() with various separators combinations and all of them work. Seems like R understand what to do when managing files and correct the separators.

But since the compiling of the .rmd file is performed by pandoc via a shell command, the mix of separators is not fixed and the error occurs.

I find very odd thought that in the error log, pandoc is called with an Unix path

C:/Program Files/RStudio/bin/pandoc/pandoc

How is pandoc able to run and raise the error??

Summing up I need my code to run on both platforms.

UPDATE 2: I tried removing white spaces from every paths in my code. Also I tried to move the scripts and rmd file to a new project in local folder, to check for problems introduced by having a shared project on GoogleDrive. No improvements whatsoever.

UPDATE 3: I posted the problem as an issue on https://github.com/rstudio/rmarkdown. I implemented the suggestion I was given there (creating the output first in the same folder as the project and then move it to the actual destination folder) but with no luck. however I am pretty sure now that the problem is in the creation and retrieval of the temporary files.

If I run tempdir() on windows it returns:

tempdir()
[1] "C:\\Users\\LUCILL~1.RAV\\AppData\\Local\\Temp\\RtmpUrsOgH"

with the strange shorten user name which is also reported in the error log.

If I run:

normalizePath(tempdir())
[1] "C:\\Users\\lucilla.rava\\AppData\\Loc

I get the correct path.

UPDATE 4: If it can be useful: I need to set up the path for pdftext every time I open the R project.

In a .Rprofile file I declare:

Sys.setenv(PATH = paste(Sys.getenv("PATH"), "C:\\Program Files\\MiKTeX 2.9\\miktex\\bin\\x64", sep = .Platform$path.sep))

UPDATE 5: Odd as it seems, we moved to a new windows computer, and the knitting this time worked. No idea why... Nevertheless if someone still has any idea why would be appreciated, since the other computer is the main workstation of my colleague.

like image 435
Bakaburg Avatar asked Dec 03 '15 12:12

Bakaburg


People also ask

Why is my R Markdown not knitting?

If a chunk works in R but not when you knit, it is almost always because you've changed a variable in your global working environment not using code in a chunk. Try restarting your R session and running each chunk sequentially to make sure all your variable values are up to date.

How do I publish R Markdown as PDF?

Having saved your R Markdown file, it's time to process it and generate a PDF file. You can do this by clicking the Knit PDF button above the text.

How do you Knit a PDF in RStudio?

When you press “Knit to PDF” in RStudio, it converts your R Markdown document into LaTeX. Download MiKTeX from here: https://miktex.org/download • Run the installer, and restart your computer. Open an R Markdown file in RStudio, and try knitting to PDF. You may be prompted to install some packages.


2 Answers

Typical Windows Problem. You have "\" in your path, you need to replace it with the "/"

"Grafici Professioni/Periodo apr-14 ott-15 test/DH ALLERGOLOGIA - SAN PAOLO.pdf" --template "C:\Users\lucilla.rava\Documents\R\win-library\3.1\rmarkdown\rmd\latex\default.tex

to

"Grafici Professioni/Periodo apr-14 ott-15 test/DH ALLERGOLOGIA - SAN PAOLO.pdf" --template "C:/Users/lucilla.rava/Documents/R/win-library/3.1/rmarkdown/rmd/latex/default.tex
like image 189
Gnusper Avatar answered Oct 13 '22 20:10

Gnusper


This determines your OS and loads it one way on Windows and another way everywhere else

if (Sys.info()['sysname'] == Windows) {
          "Grafici Professioni/Periodo apr-14 ott-15 test/DH ALLERGOLOGIA - SAN PAOLO.pdf" --template "C:/Users/lucilla.rava/Documents/R/win-library/3.1/rmarkdown/rmd/latex/default.tex"
          } else if (Sys.info()['sysname'] == Darwin){
           #Mac filepath
          } else {
           #Linux filepath
          }

@gnusper should get credit for finding the slash problem

like image 3
black_sheep07 Avatar answered Oct 13 '22 20:10

black_sheep07