8

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..

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.

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

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

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

ggll
  • 963
  • 10
  • 13

2 Answers2

3

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

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_output

Tom McMahon
  • 587
  • 8
  • 17
  • 1
    Thanks a lot, surprised that they don't mention this in `?render`, and it's not even in the **See also** section. – ggll Oct 08 '16 at 09:01
  • @ggll thanks if it works can please tick the answer? – Tom McMahon Oct 08 '16 at 13:03
  • @ggll The point in my deleted answer was the following one: this formatted style is not specific to the metadata. It works as follows : each line after a `#'` is rendered verbatim in the `md` file, whether it is metadata or something else. That's why I wanted to drive you to `?knitr::spin` for more information about this formatted style. – Stéphane Laurent Oct 08 '16 at 20:31
  • Global options can be set for a whole directory in a file called `_output.yaml`, see http://rmarkdown.rstudio.com/pdf_document_format.html#shared_options – ggll Oct 19 '16 at 19:23
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:

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

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:

plot(mtcars$mpg, mtcars$hp)

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

enter image description here

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.

Community
  • 1
  • 1
Chrisss
  • 3,211
  • 1
  • 16
  • 13
  • 1
    I had noticed the same chunk of code, but I think it just sets the default values. – ggll Oct 08 '16 at 09:02