美文网首页
103.R Markdown中的YAML header

103.R Markdown中的YAML header

作者: 心惊梦醒 | 来源:发表于2022-04-07 08:16 被阅读0次

      YAML是另外一种便于人们读写层次数据的标记语言,R markdown中用它来输出的细节。此处讨论两类:document parameters 和 bibliographies。

    文档参数

      正如https://github.com/hadley/r4ds/blob/main/rmarkdown/fuel-economy.Rmd中所看到的,在YAML Header区设置params,可以在下文的代码块中或者文本行中引用对应的参数。

    ---
    output: html_document
    params:
      my_class: "suv"
      vectot: 1:5  # 可以是向量
      start: !r lubridate::ymd("2015-01-01")  # 在R 表达式前加!r返回执行表达式后的结果
      snapshot: !r lubridate::ymd_hms("2015-01-01 12:30:00")
    ---
    

      https://github.com/hadley/r4ds/blob/main/rmarkdown/fuel-economy.Rmd可作为一个模板,改变my_class的值,结合purrr::pwalk可以输出不同class的报告文件。

    # 先将模板保存到本地,然后
    reports <- tibble(
      class = unique(mpg$class),
      filename = stringr::str_c("fuel-economy-", class, ".html"),
      params = purrr::map(class, ~ list(my_class = .))
    )
    
    reports %>% 
      select(output_file = filename, params) %>% 
      purrr::pwalk(rmarkdown::render, input = "fuel-economy.Rmd")
    
    # rmarkdown::render()函数的功能是将输出呈现为指定格式的输入,指定params可以带参数运行
    # 如果有多个class需要分来出报告只需要用不同的参数调用模板即可。
    rmarkdown::render("fuel-economy.Rmd", params = list(my_class = "suv"))
    

    Bibliographies 和 Citations,参考文献和引文

      我累了,学不下去了,只贴代码

    bibliography: rmarkdown.bib
    csl: apa.csl
    
    Separate multiple citations with a `;`: Blah blah [@smith04; @doe99].
    
    You can add arbitrary comments inside the square brackets: 
    Blah blah [see @doe99, pp. 33-35; also @smith04, ch. 1].
    
    Remove the square brackets to create an in-text citation: @smith04 
    says blah, or @smith04 [p. 33] says blah.
    
    Add a `-` before the citation to suppress the author's name: 
    Smith says blah [-@smith04].
    

    相关文章

      网友评论

          本文标题:103.R Markdown中的YAML header

          本文链接:https://www.haomeiwen.com/subject/rrwcsrtx.html