【问题标题】:knit_hooks$set and opts_chunk$setknit_hooks$set 和 opts_chunk$set
【发布时间】:2015-09-17 15:27:19
【问题描述】:

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

R studio 没有给我 knit 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%')

【问题讨论】:

  • 感谢一辉的编辑,我找到了问题所在。但是我得到了 html 输出,你能告诉我为什么会这样

标签: r knitr r-markdown pandoc


【解决方案1】:

它应该在一个块中?

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-20
    • 2012-05-13
    • 2013-12-26
    • 1970-01-01
    • 2015-07-26
    • 2019-07-12
    相关资源
    最近更新 更多