knit_hooks $ set和opts_chunk $ set

时间:2015-09-17 15:27:19

标签: r knitr r-markdown pandoc

我正在尝试使用knitr包中的knit_hooks $ set和opts_chunk $ set。但是,当我尝试全局设置这些块选项以便将块钩子应用于文档中的所有块时,它不起作用。有关详细信息,请参阅下面的代码。此外,如果您发现任何错误,请指出它们。

R studio没有给我一个针对PDF的选项,有什么原因吗?我打开的任何新RMD文件都有该选项,只要我复制此代码并保存选项就会消失。

---
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 个答案:

答案 0 :(得分:0)

应该是一块?

---
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), 
```