动态设置 R markdown 标题、输出文件名和全局变量

Kat*_*e N 6 yaml r-markdown

我必须定期运行两个科目(数学和阅读)的降价报告。目前,我必须在 Markdown 标题、输出文件名(.html 文件)中以及在 R 代码块中设置主题变量以进行数据处理。我想设置一次这个变量并调整标题、输出文件和分析。有没有办法做到这一点?

我知道标题可以通过paramsYAML 标头动态化,但这对输出文件名或 R 代码块内没有帮助。

请参阅下面我当前代码的一部分。请注意,读取变量被指定了 3 次(标题、输出文件名以及“预设”下的 R 代码块内。我想指定一次“读取”(最好在脚本顶部)。

---
title: Reading Investigation"
author: "xxx"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
  html_document:
    code_folding: hide
    depth: 3
    fig_height: 10
    fig_width: 12
    highlight: tango
    number_sections: no
    theme: cerulean
    toc: yes
    toc_float: yes
    
knit: (function(inputFile, encoding) 
{ rmarkdown::render(inputFile, encoding = encoding, 
output_file = paste0('folder/reading_output_', Sys.Date(), '.html') )}) 
---

```{r, message=FALSE, echo=FALSE, warning=FALSE, results='hide', comment="", fig.height=10, fig.width=12}

# Libraries ------------------------------------------------------------------

library(RODBC)
library(tidyverse)
library(ggplot2)
library(kableExtra)

# Set up ------------------------------------------------------------------
options(scipen=999)

#### Presets
subject = "reading" # "math", "reading"
```
Run Code Online (Sandbox Code Playgroud)

sha*_*fee 2

要动态设置输出文件名,我们必须依赖params并可params用于动态设置文档标题并在 r 代码块中定义变量。

使用辅助渲染功能


动态元数据.Rmd

---
author: "xxx"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
  html_document:
    code_folding: hide
    depth: 3
    fig_height: 10
    fig_width: 12
    highlight: tango
    number_sections: no
    theme: cerulean
    toc: yes
    toc_float: yes
params: 
    set_title: "reading"
title: "`r paste(tools::toTitleCase(params$set_title), 'Investigation')`"
---

```{r}

#### Presets
subject = params$set_title # "math", "reading"

subject
```
Run Code Online (Sandbox Code Playgroud)

然后我们必须在 r-console 或单独的 R 文件(即 R 脚本)中定义此函数,然后从 r-console 或带有 sub 的 R 文件调用它,作为读取或数学。

---
author: "xxx"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
  html_document:
    code_folding: hide
    depth: 3
    fig_height: 10
    fig_width: 12
    highlight: tango
    number_sections: no
    theme: cerulean
    toc: yes
    toc_float: yes
params: 
    set_title: "reading"
title: "`r paste(tools::toTitleCase(params$set_title), 'Investigation')`"
---

```{r}

#### Presets
subject = params$set_title # "math", "reading"

subject
```
Run Code Online (Sandbox Code Playgroud)

渲染的输出文件如下所示,

渲染文件(并排)


以及目录中的文件名folder

+---folder
|       math_output_2022-08-18.html
|       reading_output_2022-08-18.html
Run Code Online (Sandbox Code Playgroud)

在 knit 中使用自定义功能


另一种方法可以基于动态命名输出文件的答案

---
author: "xxx"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
  html_document:
    code_folding: hide
    depth: 3
    fig_height: 10
    fig_width: 12
    highlight: tango
    number_sections: no
    theme: cerulean
    toc: yes
    toc_float: yes
params: 
    set_title: "math" # reading

title: "`r paste(tools::toTitleCase(params$set_title), 'Investigation')`"
knit: >
  (function(input_file, encoding) {
    # Render, keeping intermediate files for extracting front matter
    md_dir <- tempdir()
    output_file_temp <- rmarkdown::render(
      input = input_file,
      output_file = tempfile(),
      intermediates_dir = md_dir,
      clean = FALSE
    )
    
    # Get the rendered front matter from the intermediate Markdown file
    md_file <- fs::path_ext_set(fs::path_file(input_file), ".knit.md")
    metadata <- rmarkdown::yaml_front_matter(fs::path(md_dir, md_file))
    
    # Extract the subject name from title of rendered file
    output_name <- with(metadata, {
      sub = sub(" .*$", "", tolower(title))
      paste0(fs::path_home_r(), '/folders/', sub, '_output_', Sys.Date(), '.html')
    })

    # Add the file extension and move to R's definition of the home directory
    output_ext <- fs::path_ext(output_file_temp)
    output_file <- fs::path_ext_set(output_name, output_ext)
    fs::file_move(output_file_temp, output_file)

    message("Output moved to: ", output_file)
  })
---

```{r}

#### Presets
subject = params$set_title # "math", "reading"

subject
```
Run Code Online (Sandbox Code Playgroud)

渲染的输出文件看起来像第一个选项。{fs}需要注意的一件事是,在继续使用第二种方法之前,我们需要安装该软件包。