ulyngs / oxforddown

Template for writing an Oxford University thesis in R Markdown; uses the OxThesis LaTeX template and was inspired by thesisdown.
https://ulyngs.github.io/oxforddown/
MIT License
221 stars 81 forks source link

PDF does not open after being rendered (using Makefile) #9

Closed netique closed 3 years ago

netique commented 4 years ago

When clicking on "Build All" button in RStudio, the PDF renders just fine, but will not open. I have found out that this is actually a problem with browseURL() command. When replaced with system2("open", "docs/_main.pdf"), everything compiles and opens according the expectations.

EDIT: There is sort of important argument, which should be used for our purpose and the line should then be like: Rscript -e 'system2("open", "docs/_main.pdf", wait = FALSE)'. Otherwise, RStudio Build pane waits for something I do not really understand and the red STOP button is active. With wait = FALSE, this behaviour is corrected.

Here is the error message:

Rscript -e 'browseURL("docs/_main.pdf")'
Error in shell.exec(url) : 'docs/_main.pdf' not found
Calls: browseURL -> shell.exec
Execution halted
make: *** [Makefile:4: pdf] Error 1

Exited with status 2.

And sessionInfo:

R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=Czech_Czechia.1250 
[2] LC_CTYPE=Czech_Czechia.1250   
[3] LC_MONETARY=Czech_Czechia.1250
[4] LC_NUMERIC=C                  
[5] LC_TIME=Czech_Czechia.1250    

attached base packages:
[1] stats     graphics  grDevices utils     datasets 
[6] methods   base     

loaded via a namespace (and not attached):
[1] compiler_4.0.2   rsconnect_0.8.16 tools_4.0.2     
[4] packrat_0.5.0 
stinco commented 3 years ago

Hi, this is an issue due to how Windows represents directories paths. While Operating Systems based on Unix (such as Linux and Max OS) uses the slash (/) to separate the parts of a path, Windows uses the backslash (\).

So, the file you want to open, in a Unix based OS is represented as docs/_main.pdf, while in Windows it is represented as docs\_main.pdf.

Given that in R \ is used as an escape character (for representing special characters like \n or \t), you have to write your path as docs\\_main.pdf. This way, \\ is used to represent the single backslash in the path.

I fixed this issue modifying the Makefile replacing Rscript -e 'browseURL("docs/_main.pdf")' at line 4 with Rscript -e 'browseURL(ifelse(.Platform[["OS.type"]] == "windows", "docs\\_main.pdf", "docs/_main.pdf"))' On R, .Platform[["OS.type"]] will return "windows" if R is running on Windows and "unix" if R is running on Linux or Mac OS.

This is the whole Makefile with the fix on pdf, gitbook and word:

pdf:
    Rscript -e 'options(bookdown.render.file_scope = FALSE); bookdown::render_book("index.Rmd", output_format = "bookdown::pdf_book")'
    rm -f *.log *.mtc* *.maf *.aux *.bcf *.lof *.lot *.out *.toc front-and-back-matter/abbreviations.aux
    Rscript -e 'browseURL(ifelse(.Platform[["OS.type"]] == "windows", "docs\\_main.pdf", "docs/_main.pdf"))'

gitbook:
    Rscript -e 'options(bookdown.render.file_scope = FALSE); bookdown::render_book("index.Rmd", output_format = "bookdown::gitbook")'
    Rscript -e 'browseURL(ifelse(.Platform[["OS.type"]] == "windows", "docs\\index.html", "docs/index.html"))'

word:
    Rscript -e 'options(bookdown.render.file_scope = FALSE); bookdown::render_book("index.Rmd", output_format = "bookdown::word_document2")'
    Rscript -e 'browseURL(ifelse(.Platform[["OS.type"]] == "windows", "docs\\_main.docx", "docs/_main.docx"))'

clean:
    rm -f *.log *.mtc* *.maf *.aux *.bbl *.blg *.xml

clean-knits:
    rm -f *.docx *.html *.pdf *.log *.maf *.mtc* *.tex *.toc *.out *.lof *.lot *.bcf *.aux
    rm -R *_files
    rm -R *_cache
ulyngs commented 3 years ago

Thanks for flagging this issue!

I've just pushed a fix - the Makefile now uses the here package to build the filepath, which should ensure cross-platform compatibility (I tested it on Mac OS and Windows 10).

Can you try downloading the latest version from GitHub and checking it it works?