library(tidyverse) library(edr) library(DT) library(gt)
pitchfork
dataset.pitchfork_1000 <- pitchfork %>% dplyr::slice_head(n = 1000) datatable(pitchfork_1000)
mutate()
function to help us transform bare links into HTML links.pitchfork_1000 <- pitchfork_1000 %>% mutate(link = paste0("<a href='", link ,"'>review</a>")) pitchfork_1000 %>% select(link)
escape
condition from column 8
.pitchfork_1000 %>% datatable(escape = -8)
style
and class
parameters, and, by removing the column with row names.pitchfork_1000 %>% datatable( escape = -7, style = "bootstrap4", class = "table-bordered", rownames = FALSE )
colnames
argument.pitchfork_1000 %>% datatable( escape = -7, style = "bootstrap4", class = "table-bordered", rownames = FALSE, colnames = stringr::str_to_title(colnames(pitchfork_1000)) )
top
of the table).pitchfork_1000 %>% datatable( escape = -7, style = "bootstrap4", class = "table-bordered", rownames = FALSE, colnames = stringr::str_to_title(colnames(pitchfork_1000)), filter = "top" )
pitchfork_1000 %>% datatable( escape = -7, style = "bootstrap4", class = "table-bordered", rownames = FALSE, colnames = stringr::str_to_title(colnames(pitchfork_1000)), filter = "top", extensions = "Buttons", options = list( buttons = c("csv", "excel", "pdf", "copy", "print"), dom = "Bfrtip" ) )
gt_tbl_1 <- exibble %>% gt() gt_tbl_1
gt_tbl_2 <- gt_tbl_1 %>% tab_header( title = "The exibble dataset", subtitle = "(It's available in the gt package.)" ) gt_tbl_2
gt_tbl_3 <- gt_tbl_2 %>% tab_source_note("The gt package is available in CRAN.") %>% tab_source_note("This example is provided by the edr book.") gt_tbl_3
gt_tbl_4 <- gt_tbl_3 %>% tab_footnote( footnote = "This coconut contains a tape and half a necklace.", locations = cells_body(columns = vars(char), rows = 3) ) gt_tbl_4
gt_tbl_5 <- gt_tbl_4 %>% tab_footnote( footnote = "This column contains alphabetical fruit.", locations = cells_column_labels(columns = vars(char)) ) gt_tbl_5
oceaniapops
table from the (much larger) countrypops
table.Australasia <- c("AU", "NZ") Melanesia <- c("NC", "PG", "SB", "VU") Micronesia <- c("FM", "GU", "KI", "MH", "MP", "NR", "PW") Polynesia <- c("PF", "WS", "TO", "TV") oceaniapops <- countrypops %>% dplyr::filter(country_code_2 %in% c( Australasia, Melanesia, Micronesia, Polynesia) ) %>% dplyr::filter(year %in% c(1995, 2005, 2015)) %>% dplyr::mutate(region = case_when( country_code_2 %in% Australasia ~ "Australasia", country_code_2 %in% Melanesia ~ "Melanesia", country_code_2 %in% Micronesia ~ "Micronesia", country_code_2 %in% Polynesia ~ "Polynesia" )) %>% tidyr::pivot_wider(names_from = year, values_from = population) %>% dplyr::arrange(region, desc(`2015`)) %>% dplyr::select(-starts_with("country_code")) oceaniapops
oceaniapops
tibble.oceaniapops %>% gt()
oceaniapops_1 <- oceaniapops %>% gt( rowname_col = "country_name", groupname_col = "region" ) oceaniapops_1
oceaniapops_2 <- oceaniapops_1 %>% tab_spanner( label = "Total Population", columns = vars(`1995`, `2005`, `2015`) ) oceaniapops_2
fmt_number()
function.oceaniapops_3 <- oceaniapops_2 %>% fmt_number( columns = vars(`1995`, `2005`, `2015`), decimals = 0 ) %>% cols_align(align = "right") oceaniapops_3
cols_width()
, and, a heading is always a good idea.oceaniapops_4 <- oceaniapops_3 %>% cols_width( 1 ~ px(250), everything() ~ px(125) ) %>% tab_header( title = md("Populations of Countries in Oceania During **1995**, **2005**, and **2015**"), subtitle = "Combined land area of 3,291,903 sq mi and a population of over 41 million." ) oceaniapops_4
tab_footnote()
and tab_source_note()
functions.oceaniapops_5 <- oceaniapops_4 %>% tab_footnote( footnote = "United States.", locations = cells_stub(rows = starts_with(c("No", "Gu"))) ) %>% tab_footnote( footnote = "France.", locations = cells_stub(rows = starts_with(c("New Cal", "French"))) ) %>% tab_source_note( source_note = md( paste0( "Population data obtained from [World Bank Open Data]", "(https://data.worldbank.org/indicator/SP.POP.TOTL)." ) ) ) oceaniapops_5
tab_style()
and cell_text()
functions; we can do this at multiple locations at once.oceaniapops_6 <- oceaniapops_5 %>% tab_style( style = cell_text(weight = "bold"), locations = list( cells_column_labels(columns = TRUE), cells_column_spanners(spanners = TRUE), cells_row_groups(groups = TRUE) ) ) oceaniapops_6
summary_rows()
), and, for all rows in the table (with grand_summary_rows()
).oceaniapops_7 <- oceaniapops_6 %>% summary_rows( groups = TRUE, columns = TRUE, fns = list(TOTAL = ~ sum(., na.rm = FALSE)), formatter = fmt_number, decimals = 0 ) %>% grand_summary_rows( columns = TRUE, fns = list(`GRAND TOTAL` = ~ sum(., na.rm = FALSE)), formatter = fmt_number, decimals = 0 ) %>% tab_options(data_row.padding = px(4)) oceaniapops_7
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.