控制knit2html的输出

时间:2015-02-03 09:21:32

标签: r knitr

在我的项目中,我有单独的文件夹,用于文档,图表和数据。我想从项目根目录运行knit2html。在这种情况下,输入test.Rmd文件位于doc/文件夹中,我希望输出转到同一个位置。我可以做这个;但是,test.md文件将保留在父目录中(下方)。

library(knitr)

# set paths for knitr
opts_knit$set(base.dir=normalizePath(getwd()))
opts_knit$set(root.dir=normalizePath(getwd()))
opts_chunk$set(fig.path="figures/")

# create the output folder
dir.create("doc", showWarnings=FALSE)
dir.create("figures", showWarnings=FALSE)

# a minimal example
writeLines(c("```{r hello-random, echo=TRUE}", "plot(rnorm(5))", "```"), 
           "doc/test.Rmd")

# this works and outputs to test.html
knit2html(input = "doc/test.Rmd", output="doc/test.html")


list.files()
[1] "doc"     "figures" "test.md"

list.files("doc")
#[1] "test.html" "test.Rmd" 

test.html呈现得很好,但当然test.md文件位置错误。或者,我可以这样做......

library(knitr)
library(markdown)

# set paths for knitr
opts_knit$set(base.dir=normalizePath(getwd()))
opts_knit$set(root.dir=normalizePath(getwd()))
opts_chunk$set(fig.path="figures/")

# create the output folder
dir.create("doc", showWarnings=FALSE)

# a minimal example
writeLines(c("```{r hello-random2, echo=TRUE}", "plot(rnorm(5))", "```"), 
           "doc/test2.Rmd")


knit(input = "doc/test2.Rmd", output="doc/test2.md")
markdownToHTML(file = "doc/test2.md", output = "doc/test2.html")

list.files()
[1] "doc"     "figures" "test.md"

list.files("doc")
#[1] "test.html"  "test.Rmd"   "test2.html" "test2.md"   "test2.Rmd" 
#correct output!

list.files("figures")
#[1] "hello-random-1.png"  "hello-random2-1.png"
#correct output!

此处test.mdhello-random-2.png转到正确的位置,但test.html正在hello-random-2.png中查找doc/figures/hello-random2-1.png,因此无法正确呈现。

如果我opts_chunk$set(fig.path="../figures/")(如此处所述:Working with knitr using subdirectories ),然后html正在查找正确的位置,但png文件位于项目根目录的父级中,并且html不会呈现。

中间test.md未使用knit2html转到输出路径目录,这让我想知道它是否按预期工作。

0 个答案:

没有答案
相关问题