Title: | Writing 'YAML' Headers for 'R-Markdown' Documents |
---|---|
Description: | Setting layout through 'YAML' headers in 'R-Markdown' documents, enabling their automatic generation. Functions and methods may summarize 'R' objects in automatic reports, for instance check-lists and further reports applied to the packages 'taxlist' and 'vegtable'. |
Authors: | Miguel Alvarez [aut, cre] , Bisrat Haile Gebrekidan [ctb] |
Maintainer: | Miguel Alvarez <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.1.3 |
Built: | 2024-10-23 04:44:39 UTC |
Source: | https://github.com/kamapu/yamlme |
Coercion of lists into rmd_doc objects and vice versa.
Objects of class rmd_doc can be created from lists or converted back to lists. This is convenient for defining functions that manipulate the content of such objects.
list2rmd_doc(object) rmd_doc2list(object)
list2rmd_doc(object) rmd_doc2list(object)
object |
Either a list or a rmd_doc object. |
## Create a document from a list my_document <- list( title = "Sample Document", author = "Miguel Alavarez", output = "html_document", body = txt_body( "# Intro", "", "This is just an example." )) my_document <- as(my_document, "rmd_doc") ## Convert back to a list my_document <- as(my_document, "list")
## Create a document from a list my_document <- list( title = "Sample Document", author = "Miguel Alavarez", output = "html_document", body = txt_body( "# Intro", "", "This is just an example." )) my_document <- as(my_document, "rmd_doc") ## Convert back to a list my_document <- as(my_document, "list")
Quick display for rmd_doc
objects. This method also defines the way how
objects are displayed in the console.
## S3 method for class 'rmd_doc' print(x, maxlines = 10, ...)
## S3 method for class 'rmd_doc' print(x, maxlines = 10, ...)
x |
An object of class |
maxlines |
An integer value indicating the number of lines used for the display. Longer documents will be truncated. |
... |
Further arguments passed among methods (not yet in use). |
A display of the resulting R-Markdown document in the console.
## Document without header my_document <- read_rmd( file = file.path(path.package("yamlme"), "taxlistjourney.Rmd"), skip_head = TRUE ) my_document ## Add header using update my_document <- update(my_document, title = "A journey in rOpenSci", author = "Miguel Alvarez", output = "html_document" ) my_document ## Header only my_document$body <- NULL my_document
## Document without header my_document <- read_rmd( file = file.path(path.package("yamlme"), "taxlistjourney.Rmd"), skip_head = TRUE ) my_document ## Add header using update my_document <- update(my_document, title = "A journey in rOpenSci", author = "Miguel Alvarez", output = "html_document" ) my_document ## Header only my_document$body <- NULL my_document
Import Rmd files into objects of class rmd_doc.
The function txt_body()
add a line break at the end of each element of a
character vector considering them as single lines.
Note that comments will be deleted in the input file.
read_rmd(file, ..., skip_head = FALSE) txt_body(...)
read_rmd(file, ..., skip_head = FALSE) txt_body(...)
file |
Character value indicating the path and the name to the Rmd file. |
... |
Arguments passed by |
skip_head |
Logical value indicating whether the yaml head should be skip or not (this argument is not used at the moment). |
The function read_rmd()
returns a rmd_doc object.
The function txt_body()
, a character vector suitable for the parameter
body
in the function write_rmd()
.
## Not run: ## Read pre-installed example ex_document <- read_rmd(file.path( path.package("yamlme"), "taxlistjourney.Rmd" )) ## End(Not run)
## Not run: ## Read pre-installed example ex_document <- read_rmd(file.path( path.package("yamlme"), "taxlistjourney.Rmd" )) ## End(Not run)
This function is a wrapper of rmarkdown::render()
and will also work with
file names but also enables the possibility of rendering from objects created
by write_rmd()
.
render_rmd(input, ...) ## S3 method for class 'character' render_rmd(input, ...) ## S3 method for class 'rmd_doc' render_rmd(input, output_file, delete_rmd = TRUE, ...)
render_rmd(input, ...) ## S3 method for class 'character' render_rmd(input, ...) ## S3 method for class 'rmd_doc' render_rmd(input, output_file, delete_rmd = TRUE, ...)
input |
Either a character value indicating the path and the name of the
r-markdown file, or an object of class |
... |
Further parameters passed to |
output_file |
A character value indicating the name of the output file.
This argument is passed to |
delete_rmd |
A logical value idicating whether the temporary Rmd file should be deleted or not. If not, the file gets the same name as the rendered file. |
## Not run: ## copy example to your working directory filename <- "taxlistjourney.Rmd" file.copy(from = file.path(path.package("yamlme"), filename), to = filename) ## Render the file with rmarkdown::render() render_rmd(filename, output_file = "example") browseURL("example.html") ## Render the file with yamlme text_document <- read_rmd(filename) text_document <- update(text_document, title = "my title", author = "my name", output = "html_document" ) render_rmd(text_document, output_file = "example2") browseURL("example2.html") ## End(Not run)
## Not run: ## copy example to your working directory filename <- "taxlistjourney.Rmd" file.copy(from = file.path(path.package("yamlme"), filename), to = filename) ## Render the file with rmarkdown::render() render_rmd(filename, output_file = "example") browseURL("example.html") ## Render the file with yamlme text_document <- read_rmd(filename) text_document <- update(text_document, title = "my title", author = "my name", output = "html_document" ) render_rmd(text_document, output_file = "example2") browseURL("example2.html") ## End(Not run)
An S3 class for rmarkdown documents iheriting properties from lists.
Header settings are a list at object$header
, while content in markdown
is a character vector at object$body
.
Alternative to modify settings and content in rmd_doc
objects. Note that to
skip some elements of the YAML header, you can set the argument NULL to the
respective parameter.
## S3 method for class 'rmd_doc' update(object, ...)
## S3 method for class 'rmd_doc' update(object, ...)
object |
An object of class |
... |
Named arguments to be inserted in the YAML header (passed to
|
## Create a document from a list my_document <- list( title = "Sample Document", author = "Miguel Alavarez", output = "html_document", body = txt_body( "# Intro", "", "This is just an example." )) my_document <- as(my_document, "rmd_doc") my_document ## Change output format my_document <- update(my_document, output = "pdf_document") my_document
## Create a document from a list my_document <- list( title = "Sample Document", author = "Miguel Alavarez", output = "html_document", body = txt_body( "# Intro", "", "This is just an example." )) my_document <- as(my_document, "rmd_doc") my_document ## Change output format my_document <- update(my_document, output = "pdf_document") my_document
This function generates R-Markdown documents by including
the settings as arguments of the function.
Comments and pieces of header can be also added through the argument
append
.
write_rmd(object, ...) ## S3 method for class 'rmd_doc' write_rmd(object, filename, ...)
write_rmd(object, ...) ## S3 method for class 'rmd_doc' write_rmd(object, filename, ...)
object |
rmd_doc object used to write an Rmarkdown file. If
header is missing, |
... |
Further arguments passed among methods (not yet used). |
filename |
A character value with the name of the file to be written. It is not necessary to include the extension *.Rmd in this argument. If missing, the function will use the name of the input object. |
A character vector of class rmd_doc
and, if argument set for parameter
filename
, an Rmd file.
## Not run: my_document <- list( title = "Sample Document", author = "Miguel Alavarez", output = "html_document", body = txt_body( "# Intro", "", "This is just an example." ) ) my_document <- as(my_document, "rmd_doc") write_rmd(my_document, filename = file.path(tempdir(), "example")) ## End(Not run)
## Not run: my_document <- list( title = "Sample Document", author = "Miguel Alavarez", output = "html_document", body = txt_body( "# Intro", "", "This is just an example." ) ) my_document <- as(my_document, "rmd_doc") write_rmd(my_document, filename = file.path(tempdir(), "example")) ## End(Not run)