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..
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.
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.
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.
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:
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...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With