/
README.Rmd
222 lines (159 loc) · 7.49 KB
/
README.Rmd
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = FALSE,
comment = " ",
fig.path = "man/figures/README-",
out.width = "100%"
)
library(minihtml)
```
```{r echo = FALSE, eval = FALSE}
# Quick logo generation. Borrowed heavily from Nick Tierney's Syn logo process
library(magick)
library(showtext)
font_add_google("Abril Fatface", "gf")
# pkgdown::build_site(override = list(destination = "../coolbutuseless.github.io/package/minihtml"))
```
```{r echo = FALSE, eval = FALSE}
img <- image_read("man/figures/mini-cooper-s.gif") %>%
image_transparent(color = "#f9fafb", fuzz = 10) %>%
image_trim() %>%
image_threshold()
hexSticker::sticker(subplot = img,
s_x = 0.92,
s_y = 1.2,
s_width = 1.5,
s_height = 0.95,
package = "html",
p_x = 1,
p_y = 0.5,
p_color = "#223344",
p_family = "gf",
p_size = 11,
h_size = 1.2,
h_fill = "#ffffff",
h_color = "#223344",
filename = "man/figures/logo.png")
image_read("man/figures/logo.png")
```
# minihtml <img src="man/figures/logo.png" align="right" height=230/>
<!-- badges: start -->
![](http://img.shields.io/badge/cool-useless-green.svg)
![](http://img.shields.io/badge/mini-verse-blue.svg)
[![Travis build status](https://travis-ci.org/coolbutuseless/minihtml.svg?branch=master)](https://travis-ci.org/coolbutuseless/minihtml)
<!-- badges: end -->
`minihtml` is a package for building html documents in R.
`minihtml` objects are compatible with [shiny](https://cran.r-project.org/package=shiny) - see the
[vignette](https://coolbutuseless.github.io/package/minihtml/articles/shiny.html)
## Overview
| Need to build | R6 object | alternate initialisation |
|---------------|----------------------|--------------------------|
| HTML elements | | `htag` |
| HTML elements | `HTMLElement$new()` | `html_elem()` |
| HTML document | `HTMLDocument$new()` | `html_doc()` |
## Quick Examples
| HTML Entity | code | result |
|---------------|----------------------------------------------------------------|-----------------------------------------------|
| HTML elements | `htag$a(href = 'http://example.com', "click here")` | `<a href="http://example.com">click here</a>` |
| HTML elements | `HTMLElement$new('p', "Hello there")` | `<p>Hello there</p>` |
| HTML elements | `html_elem('p', 'Hello there')$update(style = 'display:none')` | `<p style="display:none">Hello there</p>` |
| HTML document | `html <- HTMLDocument$new(); html$add('p', 'hello')` | `<html> <p>hello</p> </html>` |
| HTML document | `html_doc(htag$h1("Big"), htag$p("Hello"))` | `<html> <h1>Big</h1> <p>Hello</p> </html>` |
## Installation
You can install `minihtml` from [GitHub](https://github.com/coolbutuseless/minihtml) with:
``` r
# install.packages("devtools")
devtools::install_github("coolbutuseless/minihtml")
```
## `htag` helper (autocomplete HTML tags and attributes)
The `htag` helper is similar to `shiny::tag` but offers autocomplete to assist
in writing the code:
<img src="man/figures/htag.gif">
## Interface
* Individual elements can be created in a number of ways. E.g. creating a div
* As a stand-alone element
* `new_div <- HTMLElement$new('div', class = 'demo')`
* `new_div <- html_elem('div', class = 'demo')`
* `new_div <- htag$div(class = 'demo')`
* As a sub-element of an existing document or element - the element is created,
added to the parent document (or element) and returned.
* `doc <- HTMLDocument$new(); new_div <- doc$add('div', class = 'demo')`
* `doc <- HTMLDocument$new(); new_div <- doc$div(class = 'demo')`
* `elem <- HTMLElement$new('xxx'); new_div <- elem$add('div', class = 'demo')`
* `elem <- HTMLElement$new('xxx'); new_div <- elem$div(class = 'demo')`
* `$add()` creates an element and adds it to the parent (and returns it)
* `new_div <- doc$add('div')`
* `$append()` appends the given elements as children
* `new_div <- htag$div(); doc$append(new_div, ...)`
* `$new(name, ...)`, `$update(...)` and `$add(name, ...)` all accept `...` where
* **named arguments** are added as *attributes* of this node
* **unnamed argument** are added as *children* of this node
## Parsing HTML text into a `minihtml` document
`minihtml` uses `xml2` to parse HTML text (or file) into a `minihtml` document.
```{r}
my_html <- "<p>Par contents</p>"
elem <- minihtml::parse_html_elem(my_html)
elem$update(width = "100px")$
add(name = 'span', 'inner contents')
cat(as.character(elem))
```
## All-in-one example
```{r}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Initialse an HTML document
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
my_div <- htag$div() # or use `html_elem('div')`
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Add a header and body to the html (and then keep the reference these)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
top <- my_div$div()
bottom <- my_div$div()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Add a title to the header, and an H1 to the body
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
top$h1("Hello RStats")$update(style = "color: #345678")
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create a 'div' directly within the body, and a 'p' within that 'div'
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bottom$
update(class="main")$
p("I needed a more intuitive (to me) method of writing html in R", style = 'font-family: monospace')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# independently create another div
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
div2 <- htag$div(
htag$p(
"This is a small demo of the ",
htag$span("minihtml", style = 'color: red'),
"package in action"),
htag$hr(),
htag$div(
htag$button("And it has a cool button!!"),
htag$span("The button - it does nothing", style = 'font-size: 6px')
)
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Manually append this div to the body
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bottom$append(div2)
```
```{r eval = FALSE}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Because github README's don't allow inline styles, the following is a
# screenshot of the rendered HTML
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
my_div
```
<!-- Raw HTML output. hidden by default -->
<details closed>
<summary> <span title='Raw HTML'> Raw HTML </span> </summary>
```{r echo = FALSE}
print(my_div)
```
</details><br />
<!-- Github doesn't like inline HTML styling. So insert a screenshot -->
<img src="man/figures/example1.png">