Import common YAML into rstudio / knitr document

I have several Rmd documents in which all have the same basic YAML property, except for the header. How can I save this front in one file and use it for all documents? . It gets quite large, and I don’t want every file to be made every time I set the front.

I want more

  • use the Knit / Ctrl+ Shift+ Kshortcut button in RStudio to compile
  • save all portable setup: I would like to avoid creating a custom output format or overriding rstudio.markdownToHTML(as this would require me to carry around .Rprofile)

Example

common.yaml:

author: me
date: "`r format (Sys.time(), format='%Y-%m-%d %H:%M:%S %z')`"
link-citations: true
reference-section-title: References
# many other options

sample document

----
title: On the Culinary Preferences of Anthropomorphic Cats
----

I do not like green eggs and ham. I do not like them, Sam I Am!

: ( HTML PDF), common.yaml, . R YAML ( , ) , ( , ).

/?

.

  • rmarkdown _output.yaml YAML, output: YAML, html_document: pdf_document:, , , ,...
  • knitr YAML,

    ----
    title: On the Culinary Preferences of Anthropomorphic Cats
    ```{r echo=F, results='asis'}
    cat(readLines('common.yaml'), sep='\n')
    ```
    ----
    
    I do not like green eggs and ham. I do not like them, Sam I Am!
    

    , knitr('input.Rmd'), pandoc , Knit Rstudio ( render), knitr, , knitr.

  • Makefile: , Makefile - common.yaml input.Rmd, rmarkdown::render() - Knit Rstudio , , - Rstudio .Rproj, .Rprofile. .

EDIT. Makefile (Ctrl + Shift + B). , , Ctrl + Shift + B, , Rmd, [ Ctrl + Shift + K].

+8
2

(.. .Rprofile, - YAML):

  1. yaml pandoc ! d'!
  2. knit: , , , Ctrl + Shift + K.

1: YAML .

YAML

common.yaml:

---
author: me
date: "'r format (Sys.time(), format='%Y-%m-%d %H:%M:%S %z')'"
link-citations: true
reference-section-title: References
---

, , .. --- .

YAML pandoc, YAML (. github)

. example.rmd:

---
title: On the Culinary Preferences of Anthropomorphic Cats
output:
  html_document:
    pandoc_args: './common.yaml'
---

I do not like green eggs and ham. I do not like them, Sam I Am!

html_document: _output.yaml, rmarkdown output: . , YAML , .

:

  • YAML.

:

  • YAML knit, . "r format (Sys.time(), format = '% Y-% m-% d% H:% M:% S% z')".
  • github:

    , .

, - .

2: knit

, /.

rmarkdown: knit: YAML , "Knit" Rstudio.

:

  1. myknit(inputFile, encoding), YAML, RMD render . myknit.r.
  2. YAML example.rmd

     knit:  (function (...) { source('myknit.r'); myknit(...) })
    

    , . source('myknit.r') , YAML, . myknit.r, YAML. , YAML, , knit; YAML common.yaml.

Ctrl + Shift + K , , Rstudio.

:

  • myknit make, make .
  • YAML rmarkdown , , , render.
  • : myknit () Output created: path/to/file.html, .

    , [ ], . , render ( "Output output: basename.extension"), , render(..., quiet=T), suppressMessages(render(...)) ( knitr progress pandoc output) .

:

  • YAML
  • , 1, pre-/-.

:

  • , 1
  • knit: ( source('./myknit.r'), , )

. myknit.r common.yaml. .Rprofile .

example.rmd:

---
title: On the Culinary Preferences of Anthropomorphic Cats
knit:  (function (...) { source('myknit.r'); myknit(...) })
---

I do not like green eggs and ham. I do not like them, Sam I Am!

common.yaml []:

author: me
date: "'r format (Sys.time(), format='%Y-%m-%d %H:%M:%S %z')'"
link-citations: true
reference-section-title: References

myknit.r:

myknit <- function (inputFile, encoding, yaml='common.yaml') {   
    # read in the YAML + src file
    yaml <- readLines(yaml)
    rmd <- readLines(inputFile)

    # insert the YAML in after the first ---
    # I'm assuming all my RMDs have properly-formed YAML and that the first
    # occurence of --- starts the YAML. You could do proper validation if you wanted.
    yamlHeader <- grep('^---$', rmd)[1]
    # put the yaml in
    rmd <- append(rmd, yaml, after=yamlHeader)

    # write out to a temp file
    ofile <- file.path(tempdir(), basename(inputFile))
    writeLines(rmd, ofile)

    # render with rmarkdown.
    message(ofile)
    ofile <- rmarkdown::render(ofile, encoding=encoding, envir=new.env())

    # copy back to the current directory.
    file.copy(ofile, file.path(dirname(inputFile), basename(ofile)), overwrite=T)
}

Ctrl + Shift + K/Knit example.rmd . , common.yaml, , example.rmd .

+8

, @ .coffee, , (, - ). , pandoc YAML. ,

header.yaml:

title: "Crime and Punishment"
author: "Fyodor Dostoevsky"

RMarkdown:

---
output:
  html_document:
    pandoc_args: ["--metadata-file=header.yaml"]
---

. pandoc --metadata-file.

0

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


All Articles