Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems in Knitting Html in preview release of RStudio

Tags:

r

rstudio

knitr

I have asked related questions here, here and here.

Goal

I have a long Rmd file (saved in a R project) which I want to knit to Html and PDF with table of contents.

Problem Background

I was using RStudio 0.98.501 previously. The settings were:

  1. No table of contents (TOCs) command at the top of document
  2. Absolute paths to external images
  3. cache=TRUE in global chunk options

When I clicked the knitHtml button first time it created new folders: figures, cache, knitHTML, etc. There was no problem, everything worked fine. But then I decided to add TOCs. Using the Output Options section at Rmarkdown Version 2 page, I added the toc command at the very top, clicked the knitHtml button but got the very same output as before without any TOCs. So, I decided to upgrade to RStudio Preview release.

Current State of Problem

After updating to preview release, I opened the project and clicked knitHtml button. It gave the error that one of the external images was not found. So, upon Yihui Xie's advice I did following:

  • Copied all the external images AND plots created by R during previous knittings to the folder where the Rmd file was. This was the knitHtml folder in project directory.
  • Relative paths to all external images
  • cache=TRUE in global chunk options

Then I clicked the knitHTML button and got following error:

output file: Trajectory1-new.knit.md
"C:/Program Files/RStudio/bin/pandoc/pandoc" Trajectory1-new.utf8.md --to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output Trajectory1-new.html --smart --email-obfuscation none --self-contained --standalone --section-divs --table-of-contents --toc-depth 3 --template C:\Users\durraniu\Documents\R\win-library\3.0\rmarkdown\rmd\h\default.html --variable theme:united --include-in-header C:\Users\durraniu\AppData\Local\Temp\Rtmp0OFfmZ\rmarkdown-str10186bd23276.html --mathjax --variable mathjax-url:https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML --no-highlight --variable highlightjs=C:\Users\durraniu\Documents\R\win-library\3.0\rmarkdown\rmd\h\highlight 
pandoc.exe: Could not find data file ./Trajectory1-new_files/figure-html/pdf_velocity.png
Error: pandoc document conversion failed with error 97
In addition: Warning messages:
1: In if (grepl(" ", path, fixed = TRUE)) path <- utils::shortPathName(path) :
  the condition has length > 1 and only the first element will be used
2: running command '"C:/Program Files/RStudio/bin/pandoc/pandoc" Trajectory1-new.utf8.md --to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output Trajectory1-new.html --smart --email-obfuscation none --self-contained --standalone --section-divs --table-of-contents --toc-depth 3 --template C:\Users\durraniu\Documents\R\win-library\3.0\rmarkdown\rmd\h\default.html --variable theme:united --include-in-header C:\Users\durraniu\AppData\Local\Temp\Rtmp0OFfmZ\rmarkdown-str10186bd23276.html --mathjax --variable mathjax-url:https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML --no-highlight --variable highlightjs=C:\Users\durraniu\Documents\R\win-library\3.0\rmarkdown\rmd\h\highlight' had status 97 
Execution halted

So, I tried knit PDF and it worked. The output was as expected. Then I changed to cache=FALSE in global chunk options and clicked knit HTML. It took long (my file has lots of analyses) and gave the html file with TOCs as output, what I required.

My question is, why do I have to put cache=FALSE for creating html when cache=TRUE works for PDF in RStudio preview release? I can't wait for 15-20 minutes every time to see output after just adding a single section. How can I resolve this?

EDIT

Following is the front matter:

---
title: "Sample Document"
output:
  html_document:
    theme: united
    toc: yes
---

Trajectory: 7:50 am - 8:05 am (t1)
========================================================
```{r setup}
# set global chunk options: 
library(knitr)
opts_chunk$set(cache=TRUE, fig.align='center')
```
```{r alllibraries, echo=FALSE}
library(ggplot2)
library(plyr)
library(data.table)
library(parallel)
library(xtable)
library(ggthemes)
suppressPackageStartupMessages(library(googleVis))
my.theme<-function(base_size = 12, base_family = "Trebuchet MS")
{theme(plot.title = element_text(size = rel(2)), panel.grid.major=element_line(color='grey'), panel.grid.minor=element_line(color='grey', linetype='dashed'), legend.position='bottom', legend.background = element_rect(colour = "black"), strip.text = element_text(size=13, lineheight=2))
}
```
like image 824
umair durrani Avatar asked Jun 11 '14 19:06

umair durrani


People also ask

Why can't I Knit in RStudio?

No Knit HTML button This means that RStudio doesn't understand your document is supposed to be an RMarkdown document, often because your file extension is . txt . To fix this, go to the Files tab (lower right corner, same pane as Plots and Help) and select the checkbox next to your document's name.

How do I Knit RStudio to HTML?

To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.

Why does my code run but not Knit?

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.


1 Answers

This might serve as a comment only but it worked for me.

Because I originally created the project and Markdown document in an older version of RStudio (0.98.501) and then later switched to preview release, I think, it became necessary to specify figure and cache path in chunk options. So, I did following:

opts_chunk$set(cache=TRUE, cache.path = 'DocumentName_cache/', fig.path='figure/')

Now, I don't have to keep cache=FALSE to knit to HTML. In the preview release, I can now easily create table of contents and change theme.

like image 198
umair durrani Avatar answered Sep 30 '22 19:09

umair durrani