Skip to content

Commit 9f87d18

Browse files
committed
Updates based on reviewer comments #5
1 parent 6c67cdf commit 9f87d18

8 files changed

+85
-165
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: highlightHTML
22
Type: Package
33
Title: Highlight HTML Text and Tables
4-
Version: 0.1.4
4+
Version: 0.2.0
55
Authors@R: person("Brandon", "LeBeau", email = "[email protected]",
66
role = c("aut", "cre"))
77
Maintainer: Brandon LeBeau <[email protected]>

R/table_inject.r

+10
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@
6464

6565
table_id_inject <- function(table, id, conditions, variable = NULL, num_digits = NULL) {
6666

67+
if(length(id) != length(conditions)) {
68+
stop("The arguments id and conditions must be the same length")
69+
}
70+
71+
if(!is.null(variable)) {
72+
if(length(id) != length(variable)) {
73+
stop("The arguments id and variable must be the same length")
74+
}
75+
}
76+
6777
if(!is.null(num_digits)) {
6878
numeric_columns <- unlist(lapply(table, is.numeric))
6979
table[numeric_columns] <- round(table[numeric_columns], num_digits)

README.md

+31-3
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,44 @@ or the development version with devtools:
1919

2020
```r
2121
install.packages("devtools")
22-
devtools::install_github('lebebr01/highlightHTMl')
22+
devtools::install_github('lebebr01/highlightHTML')
2323
```
2424

2525
## Usage
2626

27-
As mentioned above, text and summary tables can be styled using CSS ids. The CSS ids are inserted into text by using braces (i.e. \{) with a CSS id hashtag following the opening brace (e.g. \{#bggrey example\}). In the previous example, the word "example" would be identified with the CSS id #bggrey and could be styled using CSS.
27+
As mentioned above, text and summary tables can be styled using CSS ids or classes. The CSS ids (or classes) are inserted into text by using braces (i.e. \{) with a CSS id hashtag following the opening brace (e.g. `\{#bggrey example\}`). In the previous example, the word "example" would be identified with the CSS id #bggrey and could be styled using CSS.
2828

2929
There are two ways to create styling within a markdown summary table. The first option is useful if the markdown summary table is created manually (i.e. type by hand). In this scenario you simply need to add the hashtag to the desired cell of the summary table. Perhaps the more common workflow for adding hashtags to a summary table would be dynamically with R code. To do this, the function `table_id_inject` is used to create conditional arguments for adding the hashtags to a markdown summary table. This is shown in more detail with the example below.
3030

31-
## Simple Example of Package Workflow
31+
## Adding CSS ids manually
32+
Suppose you have a summary table like the following (note: this table was created by typing manually, a more typical workflow is shown later using R directly):
33+
34+
| Color Name | Number |
35+
|----------- | ------------ |
36+
| Red | 25 |
37+
| Blue | 40 |
38+
| Green | 4 |
39+
| Orange | 87 |
40+
41+
You could then add styling by adding the following tags to the table, again this was added by hand, but a conditional example with R is shown later.
42+
43+
| Color Name | Number |
44+
|----------- | ------------ |
45+
| Red | 25 #bgred |
46+
| Blue | 40 #bgblue |
47+
| Green | 4 |
48+
| Orange | 87 |
49+
50+
The addition of the *#bgblue* and *#bgred* indicates which cells will be changed. After turning the markdown document into an html file, this package can now be used to post-process the html file. The post-processing will add an id value for each cell with the *#bgblue* or *#bgred* and remove those from the table.
51+
52+
The CSS for these ids could be as simple as the following to change the background color of the cells with the CSS id inserted above:
53+
54+
````
55+
#bgred {background-color: #FF0000;}
56+
#bgblue {background-color: #0000FF;}
57+
````
58+
59+
## Simple Example of Common Package Workflow
3260

3361
Below is a simple example of an Rmd document that combines both elements, text markup and inserting CSS ids dynamically into a table using R code and the `table_id_inject` function. The summary table below is calculating the mean and standard deviation of the chick weights by different feed types. You can imagine creating a similar table in reports that you create.
3462

readme.Rmd

+31-3
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,44 @@ install.packages("highlightHTML")
1717
or the development version with devtools:
1818
```{r devtools, eval = FALSE}
1919
install.packages("devtools")
20-
devtools::install_github('lebebr01/highlightHTMl')
20+
devtools::install_github('lebebr01/highlightHTML')
2121
```
2222

2323
## Usage
2424

25-
As mentioned above, text and summary tables can be styled using CSS ids. The CSS ids are inserted into text by using braces (i.e. \{) with a CSS id hashtag following the opening brace (e.g. \{#bggrey example\}). In the previous example, the word "example" would be identified with the CSS id #bggrey and could be styled using CSS.
25+
As mentioned above, text and summary tables can be styled using CSS ids or classes. The CSS ids (or classes) are inserted into text by using braces (i.e. \{) with a CSS id hashtag following the opening brace (e.g. `\{#bggrey example\}`). In the previous example, the word "example" would be identified with the CSS id #bggrey and could be styled using CSS.
2626

2727
There are two ways to create styling within a markdown summary table. The first option is useful if the markdown summary table is created manually (i.e. type by hand). In this scenario you simply need to add the hashtag to the desired cell of the summary table. Perhaps the more common workflow for adding hashtags to a summary table would be dynamically with R code. To do this, the function `table_id_inject` is used to create conditional arguments for adding the hashtags to a markdown summary table. This is shown in more detail with the example below.
2828

29-
## Simple Example of Package Workflow
29+
## Adding CSS ids manually
30+
Suppose you have a summary table like the following (note: this table was created by typing manually, a more typical workflow is shown later using R directly):
31+
32+
| Color Name | Number |
33+
|----------- | ------------ |
34+
| Red | 25 |
35+
| Blue | 40 |
36+
| Green | 4 |
37+
| Orange | 87 |
38+
39+
You could then add styling by adding the following tags to the table, again this was added by hand, but a conditional example with R is shown later.
40+
41+
| Color Name | Number |
42+
|----------- | ------------ |
43+
| Red | 25 #bgred |
44+
| Blue | 40 #bgblue |
45+
| Green | 4 |
46+
| Orange | 87 |
47+
48+
The addition of the *#bgblue* and *#bgred* indicates which cells will be changed. After turning the markdown document into an html file, this package can now be used to post-process the html file. The post-processing will add an id value for each cell with the *#bgblue* or *#bgred* and remove those from the table.
49+
50+
The CSS for these ids could be as simple as the following to change the background color of the cells with the CSS id inserted above:
51+
52+
````
53+
#bgred {background-color: #FF0000;}
54+
#bgblue {background-color: #0000FF;}
55+
````
56+
57+
## Simple Example of Common Package Workflow
3058

3159
Below is a simple example of an Rmd document that combines both elements, text markup and inserting CSS ids dynamically into a table using R code and the `table_id_inject` function. The summary table below is calculating the mean and standard deviation of the chick weights by different feed types. You can imagine creating a similar table in reports that you create.
3260

tests/testthat/test_table_id.r

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@ test_that('add in tags to table', {
2727
variable = list(c('sd_mpg'), c('avg_mpg', 'sd_mpg')))
2828

2929
expect_true(grepl('#bgblue', inject_table['avg_mpg']))
30-
#expect_false(grepl('#bgred', tmp['sd_mpg']))
3130
expect_true(grepl('#bgblue', inject_table['sd_mpg']))
3231

32+
expect_error(table_id_inject(summ_table, id = c('#bgred', '#bgblue'),
33+
conditions = c('< 2'),
34+
variable = list(c('sd_mpg'), c('avg_mpg', 'sd_mpg'))))
35+
36+
expect_error(table_id_inject(summ_table, id = c('#bgred', '#bgblue'),
37+
conditions = c('< 2', '< 16'),
38+
variable = list(c('avg_mpg', 'sd_mpg'))))
3339

3440
})

vignettes/the_basics.Rmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ vignette: >
99
%\VignetteEncoding{UTF-8}
1010
---
1111

12-
Have you ever wanted to include some form of styling, either conditional or nonconditional, when going from a (R)markdown input document to a html output document? If so, this package may be worthwhile to you. The `highlightHTML` package inject CSS into the output HTML document automatically via CSS ids (more information on CSS ids here: <http://www.cssbasics.com/css-ids/>).
12+
Have you ever wanted to include some form of styling, either conditional or nonconditional, when going from a (R)markdown input document to a html output document? If so, this package may be worthwhile to you. The `highlightHTML` package inject CSS into the output HTML document automatically via CSS ids and classes (more information on CSS ids and classes: <http://www.cssbasics.com/css-ids/> and <http://www.cssbasics.com/css-classes/>).
1313

1414
The examples below start by showing the syntax in somewhat trivial examples initially. This is done to help show the markdown syntax a bit more simply. Toward the end of the document, a more typical workflow going from Rmd to HTML is shown that puts all the pieces together.
1515

vignettes/the_basics.html

+4-4
Large diffs are not rendered by default.

vignettes/the_basics.utf8.md

-152
This file was deleted.

0 commit comments

Comments
 (0)