template2html: Generate an HTML FIle from Finished Template

Description Usage Arguments Value Examples

Description

Generate an HTML file from a template that has been created using highlight_template and insert_body.

Usage

1
template2html(template, file = "out.html", protect = TRUE, ...)

Arguments

template

A template thate has been created using highlight_template and insert_body.

file

A file to output to. Use file = "" to print to console.

protect

logical. If TRUE only allows a tempalte from insert_body. Use FALSE to override this behavior.

...

Other arguments passed to cat.

Value

Returns the HTML vector that was used to produce file.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
marks <- c(pos = "lightgreen", neg = "pink", neutral = "yellow")
## Not run: 
template2html(highlight_template(marks), file="", protect=FALSE)

## End(Not run)

temp <- highlight_template(marks)
body <- paste0("I like <mark class=\"pos\">good</mark>",
    " <mark class=\"neutral\">fish</mark>",
    ", but <mark class=\"neg\">bad</mark> <mark class=\"neutral\">fish</mark>",
    " is <mark class=\"neg\">gross</mark>.")

template2html(insert_body(highlight_template(marks), body), file="")

## Not run: 
## To an external file
template2html(insert_body(highlight_template(marks), body))
open_html()

## End(Not run)

## More Elaborte Examples
## Not run: 
# EXAMPLE 1
data(presidential_debates_2012)

## What to search for
regex <- c(statement = "\\.$", question = "\\?$", exclamation = "\\!$", trailing = "\\|$")

## What to call the searched for elements
marks_class <- names(regex)

## Map of color to named elements
marks <- c(statement = "lightgreen", question = "pink", exclamation = "orange",
    trailing = "lightgray")

## Markup sentence by type
speech <- mark_sentences(presidential_debates_2012[["dialogue"]], regex, marks_class)

## Combine group variables and add <h> tags
person <- sapply(1:nrow(presidential_debates_2012), function(i) {
    x <- as.character(unlist(presidential_debates_2012[i, c("person", "time")]))
    h1(paste(x, collapse = ":"))
})

## Combine into the body
body <- p_(person, speech)

## Make a template with color mapping for mark markup
## Insert the body into the template
## Output the template to html
template2html(insert_body(highlight_template(marks), body))

## Open the html file
open_html()

# EXAMPLE 2
data(sam_i_am)

## What to search for
regex1 <- c(statement = "\\.$", question = "\\?$", exclamation = "\\!$")
regex2 <- c(good = "\\b([Gg]ood|I like|I will(?! not))\\b", bad = "\\b([Nn]ot( like)*)\\b")

## What to call the searched for elements
marks_class <- names(c(regex1, regex2))

## Map of color to named elements
marks <- c(
       statement = "lightgray", question = "lightblue", exclamation = "yellow",
       bad = "pink", good = "lightgreen"
)

## Markup sentence by type
speech <- mark_words(sam_i_am, regex2, marks_class[4:5])
speech <- mark_sentences(speech, regex1, marks_class[1:3])


## Combine into the body (need qdapTools package)
if (!require("pacman")) install.packages("pacman")
pacan::p_load(qdapTools)
body <- p(lapply(split_vector(speech), paste, collapse=" "))

## Make a template with color mapping for mark markup
## Insert the body into the template
## Output the template to html
template2html(insert_body(highlight_template(marks), body))

## Open the html file
open_html()

## End(Not run)

trinker/hilight documentation built on May 31, 2019, 8:51 p.m.