trosendal / hlt

A package to make html and maybe latex tables from dataframes
0 stars 0 forks source link

Some examples #1

Open trosendal opened 6 years ago

trosendal commented 6 years ago

Two examples of what I am building today in svamap and we like to replace the code that generates everything inside:

  1. Here we use the tag to control the colour of just those rows separate from the

view-source:http://www.sva.se/Maps/Kvarka_table/kvarka_table.html

  1. Here we use the tag to control the sorting to only occur in the body not the last row of the table

view-source:http://www.sva.se/Maps/CWD/table.html

Both tables also include styles in the tags for alignment. But I think we have this on the todo.

trosendal commented 6 years ago

@stewid We are almost solving the table http://www.sva.se/Maps/CWD/table.html now like this:

df <- structure(list(Län = c("Uppsala län", "Gotlands län", "Värmlands län", 
"Blekinge län", "Hallands län", "Östergötlands län", "Örebro län", 
"Västernorrlands län", "Jämtlands län", "Kronobergs län", 
"Kalmar län", "Södermanlands län", "Stockholms län", "Västerbottens län", 
"Dalarnas län", "Skåne län", "Jönköpings län", "Gävleborgs län", 
"Västmanlands län", "Västra Götalands län", "Norrbottens län", 
"Total"), `Antal undersökta` = c("26", "0", "32", "8", "6", 
"8", "8", "6", "43", "3", "7", "9", "19", "2", "12", "13", "6", 
"6", "5", "24", "3", "246")), .Names = c("Län", "Antal undersökta"
), row.names = c(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 22L), class = "data.frame")

head <- html_head(
    html_meta(charset = "utf-8") +
    html_meta("http-equiv" = "x-ua-compatible", content = "IE=edge") +
    html_meta(NAME = "ROBOTS", CONTENT = "NOINDEX, NOFOLLOW") +
    html_link(rel = "stylesheet",
              type = "text/css",
              href = "https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css") +
    html_script("", src = "https://code.jquery.com/jquery-1.12.3.js") +
    html_script("", src = "https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js") +
    html_script("$(document).ready(function() {$('#table1').DataTable({'paging': false, 'ordering': true, 'info': false, 'searching': false} );} );"))

table <- tag_attr(hlt::html_table(df, tfoot = TRUE),
                  id = "table1",
                  class = "svatablegrayheader",
                  style = "width: 100%;",
                  border = "0")

body <- html_body(table)

html <- (html_html(head + body))

We just need the indexing of the table to be able to run:

table -> tag_attr(table[,2], align = "right")

This would add the attribute "align = 'right'" to all td elements in the second column of the table.

I think the table should be indexed only in the tbody not the thead like a normal data.frame is indexed. I'm however not sure what the best design would be to handle the tfoot. Perhaps tfoot is also outside of the standard indexing unless you specifically call that row with table$tfoot?

trosendal commented 6 years ago

Here we also add the alignment:

library(hlt)
df <- structure(list(Län = c("Uppsala län", "Gotlands län", "Värmlands län", 
"Blekinge län", "Hallands län", "Östergötlands län", "Örebro län", 
"Västernorrlands län", "Jämtlands län", "Kronobergs län", 
"Kalmar län", "Södermanlands län", "Stockholms län", "Västerbottens län", 
"Dalarnas län", "Skåne län", "Jönköpings län", "Gävleborgs län", 
"Västmanlands län", "Västra Götalands län", "Norrbottens län", 
"Total"), `Antal undersökta` = c("26", "0", "32", "8", "6", 
"8", "8", "6", "43", "3", "7", "9", "19", "2", "12", "13", "6", 
"6", "5", "24", "3", "246")), .Names = c("Län", "Antal undersökta"
), row.names = c(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 22L), class = "data.frame")

head <- html_head(
    html_meta(charset = "utf-8") +
    html_meta("http-equiv" = "x-ua-compatible", content = "IE=edge") +
    html_meta(NAME = "ROBOTS", CONTENT = "NOINDEX, NOFOLLOW") +
    html_link(rel = "stylesheet",
              type = "text/css",
              href = "https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css") +
    html_script("", src = "https://code.jquery.com/jquery-1.12.3.js") +
    html_script("", src = "https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js") +
    html_script("$(document).ready(function() {$('#table1').DataTable({'paging': false, 'ordering': true, 'info': false, 'searching': false} );} );"))

table <- hlt::html_table(df, tfoot = TRUE,
                  id = "table1",
                  class = "svatablegrayheader",
                  style = "width: 100%;",
                  border = "0")

## Add alignment to just the second column
for(i in seq_len(length(table$content[[3]]$content))) {
        table$content[[3]]$content[[i]]$content[[2]] <- tag_attr(table$content[[3]]$content[[i]]$content[[2]], align = "right")
}

## Add alignment to the tfoot
table$content[[2]]$content$content[[2]] <- tag_attr(table$content[[2]]$content$content[[2]], align = "right")

body <- html_body(table)

html <- (html_html(head + body))
trosendal commented 6 years ago

Note that instead of table$content[[2]] we should have table$content$tfoot and instead of table$content[[3]] we should have table$content$tbody

## Add align right to all cells 

for(i in seq_len(length(table$content[[3]]$content))) {
    for(j in seq_len(length(table$content[[3]]$content[[i]]$content))) {
        table$content[[3]]$content[[i]]$content[[j]] <- tag_attr(table$content[[3]]$content[[i]]$content[[j]], align = "right")
    }
}

## Remove align right from all cells 

for(i in seq_len(length(table$content[[3]]$content))) {
    for(j in seq_len(length(table$content[[3]]$content[[i]]$content))) {
        table$content[[3]]$content[[i]]$content[[j]] <- tag_attr(table$content[[3]]$content[[i]]$content[[j]], align = NULL)
    }
}

## Add it to just the second column
for(i in seq_len(length(table$content[[3]]$content))) {
        table$content[[3]]$content[[i]]$content[[2]] <- tag_attr(table$content[[3]]$content[[i]]$content[[2]], align = "right")
}

## Add alignment to the tfoot
for(j in seq_len(length(table$content[[2]]$content$content))) {
        table$content[[2]]$content$content[[j]] <- tag_attr(table$content[[2]]$content$content[[j]], align = "right")
}
stewid commented 6 years ago

@trosendal Does it work if you change

for(j in seq_len(length(table$content[[2]]$content$content))) {
        table$content[[2]]$content$content[[j]] <- tag_attr(table$content[[2]]$content$content[[j]], align = "right")
}

to

for(j in seq_len(length(table$content[[2]]$content$content))) {
        tag_attr(table$content[[2]]$content$content[[j]], align = "right")
}
trosendal commented 6 years ago

nope

stewid commented 6 years ago

I have made some refactoring, could you please try

for(j in seq_len(length(table$content[[2]]$content$content))) {
        tag_attr(table$content[[2]]$content$content[[j]]) <- list(align = "right")
}
trosendal commented 6 years ago

I get the alignment in tfoot. However, the cells in tfoot are now <th> where they should be <td>

    <table id="table1" class="svatablegrayheeader" style="width: 100%;" border="0">
      <thead>
    <tr>
      <th>Län</th><th>Antal undersökta</th></tr>
      </thead>
      <tfoot>
    <tr><th align="right">Total</th><th align="right">246</th></tr>
      </tfoot>
      <tbody>
    <tr><td>Uppsala län</td><td>26</td></tr>
    <tr><td>Gotlands län</td><td>0</td></tr>
stewid commented 6 years ago

Found the problem with <th>, try again

trosendal commented 6 years ago

Nice, it looks like we are almost there.... Now I just have to remove code from SVAMAP :) I have been looking through some of the functionality that is in SVAMAP and consider that it could be the package that just deploys pages so contains a functions that are essential to to spatial data manipulation and the deploy code for each page. So it is fluid and gets frequently updated based on needs of pages that are being designed. Then we need a 3rd package that just builds the mapping .js script that will be injected as a script item with hlt and the lines of .json that will also be injected as a script item with 'hlt'. This 3rd package should have a name like webmapping or leaflet something or r2leaflet.... there are already at least two others on CRAN :)

trosendal commented 6 years ago

Impletmentation of example2: https://github.com/SVA-SE/svamap/blob/master/inst/deploy_scripts/CWD_table2.R

trosendal commented 6 years ago

Implementation of example 1 in lines 174 to the end

https://github.com/SVA-SE/svamap/blob/master/inst/deploy_scripts/kvarka_table2.R