使用rmarkdown :: render来设置文档标题(标题,作者,日期)

时间:2022-12-04 23:44:47

I am using rmarkdown::render("script.r") to create an HTML version of an R script. The output starts with script.r as title, my login as author, and the current date as date. I don't want to give away my login, nor my work schedule..

我正在使用rmarkdown :: render(“script.r”)来创建R脚本的HTML版本。输出以script.r作为标题,我作为作者登录,以及当前日期作为日期开始。我不想放弃我的登录,也不想放弃我的工作安排。

I know this metadata (title, author, date) can be set in a YAML block inside a Rmd file, but I would like to avoid creating/editing this file, and work only with the original R script.

我知道这个元数据(标题,作者,日期)可以在Rmd文件中的YAML块中设置,但我想避免创建/编辑这个文件,并且只能使用原始的R脚本。

Is there a way to set (title, author, date) metadata via rmarkdown::render, or other functions like knitr::opts_chunk$set?

有没有办法通过rmarkdown :: render或其他函数设置(标题,作者,日期)元数据,如knitr :: opts_chunk $ set?

Alternatively, can this metadata be set inside the R script?

或者,可以在R脚本中设置此元数据吗?

Please avoid suggesting that I should write an Rmd file instead..

请避免建议我写一个Rmd文件而不是..

2 个解决方案

#1


3  

The Rmarkdown documentation (see ?compile_notebook) describes one way to do this by including a specially formatted comment in your script.R file.

Rmarkdown文档(参见?compile_notebook)描述了一种通过在script.R文件中包含特殊格式的注释来实现此目的的方法。

For example, include this comment in your script to set the title, author and date.

例如,在脚本中包含此注释以设置标题,作者和日期。

#' ---
#' title: "Crop Analysis Q3 2013"
#' author: "John Smith"
#' date: "May 3rd, 2014"
#' ---

This will give you the following output:

这将为您提供以下输出:

使用rmarkdown :: render来设置文档标题(标题,作者,日期)

#2


2  

I don't know if this is a particularly great solution, but it's too long for a comment, so here goes. I looked at rmarkdown::render and I don't think what you want is possible unless you redefine render yourself. Look at line 85 and onwards:

我不知道这是否是一个特别好的解决方案,但是评论太长了,所以这里就是这样。我看了rmarkdown :: render,除非你自己重新定义渲染,否则我不认为你想要的是什么。看看第85行及以后:

metadata <- paste("\n", "---\n", "title: \"", input, 
                  "\"\n", "author: \"", Sys.info()[["user"]], "\"\n", 
                  "date: \"", date(), "\"\n", "---\n", sep = "")
if (!identical(encoding, "native.enc")) 
  metadata <- iconv(metadata, to = encoding)
cat(metadata, file = knit_input, append = TRUE)

This isn't controlled by any condition. So a messy way is to redefine render and replace one of it's lines. I'm borrowing a useful answer to this question: Editing R functions

这不受任何条件的控制。因此,一种混乱的方式是重新定义渲染并替换其中一条线。我正在为这个问题借用一个有用的答案:编辑R函数

body(render)[[25]] <- substitute(
  if (identical(tolower(tools::file_ext(input)), "r")) {
    spin_input <- intermediates_loc(file_with_meta_ext(input, 
                                                   "spin", "R"))
    file.copy(input, spin_input, overwrite = TRUE)
    intermediates <- c(intermediates, spin_input)
    spin_rmd <- knitr::spin(spin_input, knit = FALSE, envir = envir, 
                        format = "Rmd")
    intermediates <- c(intermediates, spin_rmd)
    knit_input <- spin_rmd

    # Our edited code starts here!
    metadata <- paste("\n", "---\n", "title: \"", getOption("yaml_title"), "\"\n", 
                  "author: \"", getOption("yaml_author"), "\"\n", "date: \"", 
                  getOption("yaml_date"), "\"\n", "---\n", sep = "")
    # Our edited code ends here!

    if (!identical(encoding, "native.enc")) 
      metadata <- iconv(metadata, to = encoding)
    cat(metadata, file = knit_input, append = TRUE)
  }
)

Now, my file junk.r is as follows:

现在,我的文件junk.r如下:

plot(mtcars$mpg, mtcars$hp)

and now render("junk.r") gives me...

现在渲染(“junk.r”)给了我......

使用rmarkdown :: render来设置文档标题(标题,作者,日期)

Now you can use options to use your own entries for title, author and/or date or leave it blank. Of course, it would be easier to edit the .r file or create a .Rmd file but you've ruled those out.

现在,您可以使用选项将您自己的条目用于标题,作者和/或日期,或将其留空。当然,编辑.r文件或创建.Rmd文件会更容易,但你已经排除了这些。

#1


3  

The Rmarkdown documentation (see ?compile_notebook) describes one way to do this by including a specially formatted comment in your script.R file.

Rmarkdown文档(参见?compile_notebook)描述了一种通过在script.R文件中包含特殊格式的注释来实现此目的的方法。

For example, include this comment in your script to set the title, author and date.

例如,在脚本中包含此注释以设置标题,作者和日期。

#' ---
#' title: "Crop Analysis Q3 2013"
#' author: "John Smith"
#' date: "May 3rd, 2014"
#' ---

This will give you the following output:

这将为您提供以下输出:

使用rmarkdown :: render来设置文档标题(标题,作者,日期)

#2


2  

I don't know if this is a particularly great solution, but it's too long for a comment, so here goes. I looked at rmarkdown::render and I don't think what you want is possible unless you redefine render yourself. Look at line 85 and onwards:

我不知道这是否是一个特别好的解决方案,但是评论太长了,所以这里就是这样。我看了rmarkdown :: render,除非你自己重新定义渲染,否则我不认为你想要的是什么。看看第85行及以后:

metadata <- paste("\n", "---\n", "title: \"", input, 
                  "\"\n", "author: \"", Sys.info()[["user"]], "\"\n", 
                  "date: \"", date(), "\"\n", "---\n", sep = "")
if (!identical(encoding, "native.enc")) 
  metadata <- iconv(metadata, to = encoding)
cat(metadata, file = knit_input, append = TRUE)

This isn't controlled by any condition. So a messy way is to redefine render and replace one of it's lines. I'm borrowing a useful answer to this question: Editing R functions

这不受任何条件的控制。因此,一种混乱的方式是重新定义渲染并替换其中一条线。我正在为这个问题借用一个有用的答案:编辑R函数

body(render)[[25]] <- substitute(
  if (identical(tolower(tools::file_ext(input)), "r")) {
    spin_input <- intermediates_loc(file_with_meta_ext(input, 
                                                   "spin", "R"))
    file.copy(input, spin_input, overwrite = TRUE)
    intermediates <- c(intermediates, spin_input)
    spin_rmd <- knitr::spin(spin_input, knit = FALSE, envir = envir, 
                        format = "Rmd")
    intermediates <- c(intermediates, spin_rmd)
    knit_input <- spin_rmd

    # Our edited code starts here!
    metadata <- paste("\n", "---\n", "title: \"", getOption("yaml_title"), "\"\n", 
                  "author: \"", getOption("yaml_author"), "\"\n", "date: \"", 
                  getOption("yaml_date"), "\"\n", "---\n", sep = "")
    # Our edited code ends here!

    if (!identical(encoding, "native.enc")) 
      metadata <- iconv(metadata, to = encoding)
    cat(metadata, file = knit_input, append = TRUE)
  }
)

Now, my file junk.r is as follows:

现在,我的文件junk.r如下:

plot(mtcars$mpg, mtcars$hp)

and now render("junk.r") gives me...

现在渲染(“junk.r”)给了我......

使用rmarkdown :: render来设置文档标题(标题,作者,日期)

Now you can use options to use your own entries for title, author and/or date or leave it blank. Of course, it would be easier to edit the .r file or create a .Rmd file but you've ruled those out.

现在,您可以使用选项将您自己的条目用于标题,作者和/或日期,或将其留空。当然,编辑.r文件或创建.Rmd文件会更容易,但你已经排除了这些。