Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using rmarkdown::render to set document header (title, author, date)

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

like image 475
ggll Avatar asked Oct 08 '16 01:10

ggll


People also ask

How do you make a header in R markdown?

Creating Headings and Subheadings We can insert headings and subheadings in R Markdown using the pound sign # . There are six heading/subheading sizes in R Markdown. The number of pound signs before your line of text determines the heading size, 1 being the largest heading and 6 being the smallest.

How do I add a date in Rmarkdown?

To format = , provide a character string (in quotes) that represents the current date format using the special “strptime” abbreviations below. For example, if your character dates are currently in the format “DD/MM/YYYY”, like “24/04/1968”, then you would use format = "%d/%m/%Y" to convert the values into dates.

How do I change a document in Rmarkdown?

To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.


2 Answers

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

like image 105
Tom McMahon Avatar answered Sep 29 '22 12:09

Tom McMahon


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.

like image 22
Chrisss Avatar answered Sep 29 '22 14:09

Chrisss