Knitr: How to set the shape path in knit2html without using setwd ()?

I work in a project folder, to say that its absolute path: /project. getwd()tells me that I am in this project folder. All my reads and writes of files belong to this root of the project. /projecthas a subfolder /project/docs/In which there is an R Mardown file and an R script:

report.Rmd contains:

```{r }
plot(cars)
```

And knit_reports.Rcontains:

library(knitr)
knit2html("./docs/report.Rmd", "./docs/report.html")

If I run knit_reports.R, an html page is created, but the numbers do not appear on the page.

The problem is that the numbers are stored in /project/figures. They are not visible for generating an html document. I am looking for a way to tell knitr to store images under /project/docs/figuresinstead.

knitr root.dir base.dir report.Rmd , opts_knit$set(root.dir = "./docs") opts_knit$set(base.dir = "/project/docs").

, /project/docs:

setwd("./docs/")
knit2html("report.Rmd", "report.html")

A /project/docs/figure, html.

, setwd() script, . knitr setwd()?

+4
3

chunk. ,

  • fig.path, (OR)
  • base.dir, .

, (2) . setwd() knitr, .

+6

Ramnath base.dir . , Rmd chunks, . ​​ R script. , .

, knit_reports.R :

library(knitr)
opts_knit$set(base.dir = 'docs') # Change the base dir where to save figures
knit2html("./docs/report.Rmd", "./docs/report.html")
+5

I would like to add that you may have problems if you specify a different directory output-knit2html, since the shape folder will be saved in the source directory. Then, at least in my experience, the html file will not contain your plots. So: as mentioned elsewhere. Do not try to change the output directory

0
source

Source: https://habr.com/ru/post/1525454/


All Articles