Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ Suggests:
kableExtra, ggplot2
VignetteBuilder: knitr
Encoding: UTF-8
RoxygenNote: 6.0.1
RoxygenNote: 6.1.0
Roxygen: list(markdown = TRUE)
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Generated by roxygen2: do not edit by hand

export(html_memo)
export(memor_profile)
export(pdf_memo)
import(knitr)
importFrom(rmarkdown,html_document)
importFrom(rmarkdown,includes)
importFrom(rmarkdown,pandoc_variable_arg)
importFrom(rmarkdown,pdf_document)
importFrom(utils,file.edit)
83 changes: 83 additions & 0 deletions R/html_memo.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#'
#' @importFrom rmarkdown html_document pandoc_variable_arg includes
#'
#' @export
html_memo <- function(style = NULL,
use_profile = TRUE,
logo = NULL, company = NULL,
logo_height = 128,
...) {

if (use_profile) {
profile_file <- getOption("memor_profile", "~/memor-profile.yaml")
profile_yaml <- try(yaml::read_yaml(profile_file), silent = TRUE)
if (!class(profile_yaml) == "try-error") {
profile_compare <- list(
logo = list(logo, NULL),
company = list(company, NULL),
logo_height = list(logo_height, "128"),
style = list(style, NULL)
)
changed_items <- lapply(profile_compare, function(x){
!identical(x[[1]], x[[2]])
})
changed_items <- names(changed_items)[changed_items == T]

profile_yaml_names <- names(profile_yaml)
profile_yaml_names <- profile_yaml_names[!profile_yaml_names %in%
changed_items]
profile_yaml <- profile_yaml[profile_yaml_names]

if (length(profile_yaml) != 0) {
for (i in 1:length(profile_yaml)) {
assign(profile_yaml_names[i], profile_yaml[[i]])
}
}
}
}

if (!is.null(company)) {
if (is.list(company) && length(company) > 1) {
company <- paste(unlist(company),
collapse = " &centerdot; ")
}
# memor_args <- c(memor_args, pandoc_variable_arg("company", company))
}

config <- html_document(
includes = includes(
before_body = createHeader(logo, logo_height),
after_body = createFooter(company)
),
...
)

return(config)

}

createHeader <- function(logo, logo_height = 128) {
text = paste0(
'<div class="memor_logo" style="text-align:end;padding-top:25px">
<img src="data:image/png;base64,',
base64enc::base64encode(logo),
sprintf('" style="height:%dpx" /></div>', logo_height))

temp_header = tempfile()
write(text, temp_header)

return(temp_header)
}

createFooter <- function(company) {
text = paste0(
'<div class="memor_info" style="text-align:center;padding-bottom:10px"><p><hr> ',
company,
'</p></div>'
)

temp_footer = tempfile()
write(text, temp_footer)

return(temp_footer)
}
68 changes: 68 additions & 0 deletions docs/html_demo.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
title: "memor: Customizable HTML template for rmarkdown"
output:
memor::html_memo:
use_profile: false
logo: "memor.png"
logo_height: 120
# css: style.css
toc: True
company:
name: Institute for Aging Research
address: 1200 Centre St, Boston, MA
phone: 617-971-5386
email: stats@hsl.harvard.edu
---

```{r setup, include = FALSE}
library(knitr)
library(kableExtra)
library(ggplot2)
opts_chunk$set(warning = FALSE, message = FALSE, fig.pos = "h")
```

# Introduction
We love rmarkdown. In practice, however, we often have specific customization requirements for reporting of reproducible research. Some of these are universal, such as company logo or letterhead, contact info and so on.

We created this `memor` package to allow for easier customization of `LaTeX`-based documents combining text and results from R. `memor` now also supports the same in `HTML`. This document is produced using the following `YAML` header.

```
---
title: "memor: Customizable HTML template for rmarkdown"
output:
memor::html_memo:
use_profile: False
logo: "memor.png"
logo_height: 120
toc: True
company:
name: Institute for Aging Research
address: 1200 Centre St, Boston, MA
phone: 617-971-5386
email: stats@hsl.harvard.edu
---
```

To use a custom theme, the `css` argument can be passed in the `YAML` header.
```
---
title: "memor: Customizable HTML template for rmarkdown"
output:
memor::html_memo:
...
css: style.css
...
---
```

# Tables & Figures
Here are how tables and figures looks like in this template. Caption color is controlled by `citecolor`.
```{r}
kable(mtcars[1:5, 1:5], booktabs = T, caption = "A Table") %>%
kable_styling(latex_options = c("striped", "HOLD_position"), position = "left")
```

```{r, fig.cap="A Plot", out.extra=""}
ggplot(mtcars, aes(x = mpg, y = wt)) +
geom_point()
```
338 changes: 338 additions & 0 deletions docs/html_demo.html

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions docs/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
h1 {
color: orange;
text-align: center;
}