0
votes

I am trying to use the knit_hooks$set and opts_chunk$set from the knitr package. However when I try to set these chunk options globally so that the chunk hooks are applied to all the chunks in the document, it does not work. Please see the code below for details. Also if you spot any errors please point them out.

R studio does not give me an option of knit PDF, any reasons for that? Any new RMD file I open has that option, as soon as I copy this code and save the option goes away.

---
title: "Decision Trees and Random Forest"

author: 
 - name: "Chaitanya Jagtap"
 - affiliation: "Great Lakes Institute of Management, Chennai"

date: "20 September 2015"

documentclass: report
classoption: twoside, bindingoffset=1cm
paper: a4paper
fontsize: 12pt
fontfamily: times

geometry: 
  - tmargin=2cm
  - bmargin=2cm
  - lmargin=3cm
  - rmargin=3cm

toccolor: blue  
linkcolor: magenta
graphics: yes  

output: 
pdf_document:

toc: yes
toc_depth: 3
number_sections: yes
fig_height: 7
fig_width: 7
fig_crop: TRUE
fig_caption: yes
dev: pdf
highlight: zenburn
latex_engine: xelatex

pandoc_args:
chapters: yes
normalize: yes




---



library("knitr")

knitr::opts_knit$set(progress=TRUE, verbose=FALSE, unnamed.chunk.label="chunk")

knitr::knit_hooks$set(pdflook = function(before, options, envir) {
if (before=TRUE) {
             par(cex=0.9,cex.axis=1,cex.lab=1,cex.main=1.2,cex.sub=1,
             font=1,font.axis=1,font.lab=2,font.main=2,font.sub=1, 
             pin=c(4,3), mai=c(1, 1, 1, .2), oma=c(0.2,0.2,2,0.2), mgp = c(3, 1, 0), adj=0.5,
             las=0, xaxs="i", yaxs="i",
             col.lab = "firebrick4", col.main="deepskyblue4", col.sub="royalblue",      lab=c(10,10,7),
             lwd=2,family="Times New Roman", ps=12)
             } 
else {NULL}
}, plotcrop=hook_pdfcrop)



knitr::opts_chunk$set(pdflook=TRUE, plotcrop=TRUE, fig.align='center', fig.keep='all',      echo=FALSE, 
dev='CairoPDF', 
message=FALSE, warning=FALSE, toc=TRUE, comment='', tidy.opts=list(width.cutoff=60),     highlight=TRUE, dpi=150, external=TRUE, number_sections=true, dev.args=list(pointsize = 12,     family = ’Times’), out.height='75%', out.width = '75%')
1
Thanks Yihui for the edit, I figured out the problem. However I am getting html output, can you please let me know why is that soChaitanya Jagtap

1 Answers

0
votes

It should be in a chunk ?

---
title: "Decision Trees and Random Forest"

author: 
 - name: "Chaitanya Jagtap"
 - affiliation: "Great Lakes Institute of Management, Chennai"

date: "20 September 2015"

documentclass: report
classoption: twoside, bindingoffset=1cm
paper: a4paper
fontsize: 12pt
fontfamily: times

geometry: 
  - tmargin=2cm
  - bmargin=2cm
  - lmargin=3cm
  - rmargin=3cm

toccolor: blue  
linkcolor: magenta
graphics: yes  

output: 
pdf_document:

toc: yes
toc_depth: 3
number_sections: yes
fig_height: 7
fig_width: 7
fig_crop: TRUE
fig_caption: yes
dev: pdf
highlight: zenburn
latex_engine: xelatex

pandoc_args:
chapters: yes
normalize: yes




---


```{r}
library("knitr")

knitr::opts_knit$set(progress=TRUE, verbose=FALSE, unnamed.chunk.label="chunk")

knitr::knit_hooks$set(pdflook = function(before, options, envir) {
if (before=TRUE) {
             par(cex=0.9,cex.axis=1,cex.lab=1,cex.main=1.2,cex.sub=1,
             font=1,font.axis=1,font.lab=2,font.main=2,font.sub=1, 
             pin=c(4,3), mai=c(1, 1, 1, .2), oma=c(0.2,0.2,2,0.2), mgp = c(3, 1, 0), adj=0.5,
             las=0, xaxs="i", yaxs="i",
             col.lab = "firebrick4", col.main="deepskyblue4", col.sub="royalblue",      lab=c(10,10,7),
             lwd=2,family="Times New Roman", ps=12)
             } 
else {NULL}
}, plotcrop=hook_pdfcrop)



knitr::opts_chunk$set(pdflook=TRUE, plotcrop=TRUE, fig.align='center', fig.keep='all',      echo=FALSE, 
dev='CairoPDF', 
message=FALSE, warning=FALSE, toc=TRUE, comment='', tidy.opts=list(width.cutoff=60), 
```