How to implement expand/collapse rows in HTML table - r

I would like to make some rows expand on click, i.e. by clicking on the arrow. Is there a way in reactable to add java script code to specific rows? There exists and option for expanding/collapsing all rows on click in reactable (see here), but it does not do exactly what I need. Is there any reactable/js guru out there who has an idea how this might be feasible.
The examples are designed in kableExtra but I think my problem is more suited for reactable.
df_open <- tibble::tibble(
Category = c("Fruits ▼︎", "Apple", "Banana",
"Kiwi", "Vegetable ▼︎", "Carrots ▼︎", "Red Carrots", "Orange Carrots", "Diary"),
Value_sum = c(1:9),
Value_one = c(10:18),
Value_two = c(200:208)
)
df_closed <- tibble::tibble(
Category = c("Fruits ▶&#xFE0E","Vegetable ▶︎", "Diary"),
Value_sum = c(1, 5,9),
Value_one = c(10,14,18),
Value_two = c(200, 204,208)
)
library(kableExtra)
# all collapsed, everything shown
kbl(df_open, escape = FALSE) |>
kable_styling(bootstrap_options = "bordered") |>
row_spec(row = c(2:4, 6), extra_css = "padding-left:30px;") |>
row_spec(row = c(7,8), extra_css = "padding-left:50px")
Completely unfolded table:
# all closed
kbl(df_closed, escape = FALSE) |>
kable_styling(bootstrap_options = "bordered")
Completely folded table:
Raw Data:
df <- tibble::tibble(
Category = c("Fruits", "Apple", "Banana", "Kiwi", "Vegetable", "Carrots", "Red Carrots", "Orange Carrots", "Diary"),
Value_sum = c(1:9),
Value_one = c(10:18),
Value_two = c(200:208),
Another_column = "ABC"
)
My attempt:
library(reactable)
df <- tibble::tibble(
Category = c("Fruits", "Fruits", "Fruits", "Fruits", "Vegetable", "Vegetable", "Vegetable", "Vegetable", "Diary"),
Category2 = c("", "Apple", "Banana", "Kiwi", "", "Carrots", "Red Carrots", "Orange Carrots", "Diary"),
Value_sum = c(1:9),
Value_one = c(10:18),
Value_two = c(200:208),
Another_column = "ABC"
)
reactable(
df,
groupBy = "Category",
defaultPageSize = 5,
elementId = "fruits-table"
)
)

Related

How to summarise a column in a grouped tibble into a list of unique values

Suppose I run the following code:
library(tidyverse)
tbl <- tibble::tribble(
~name, ~fruit,
"dan", "apple",
"dan", "apple",
"dan", "banana",
"george", "banana",
"george", "watermelon",
"george", "banana",
"lauren", "kiwi",
"lauren", "kiwi",
"lauren", "kiwi"
)
tbl %>%
group_by(name) %>%
summarise(fruits = unique(list(fruit)))
This is the table I get:
Why is it still displaying non-unique fruits in each list?
Change
tbl %>%
group_by(name) %>%
summarise(fruits = unique(list(fruit)))
to
tbl %>%
group_by(name) %>%
summarise(fruits = list(unique(fruit)))
Output:
name
fruits
dan
apple , banana
george
banana , watermelon
lauren
kiwi

Set the alignment of columns based on their data type using gt package

Given a data sample and gt code to plot table below:
df <- structure(list(category = c("food", "food", "food", "food", "electronic product",
"electronic product", "electronic product", "electronic product"
), type = c("vegetable", "vegetable", "fruit", "fruit", "computer",
"computer", "other", "other"), variable = c("cabbage", "radish",
"apple", "pear", "monitor", "mouse", "camera", "calculator"),
price = c(6, 5, 3, 2.9, 2000, 10, 600, 35), quantity = c(2L,
4L, 5L, 10L, 1L, 3L, NA, 1L)), class = "data.frame", row.names = c(NA,
-8L))
To plot:
dt <- df %>%
group_by(category) %>%
gt() %>%
tab_header(
title = md("Category name")
)%>%
tab_style(
locations = cells_column_labels(columns = everything()),
style = list(
#Give a thick border below
cell_borders(sides = "bottom", weight = px(3)),
#Make text bold
cell_text(weight = "bold")
)
) %>%
tab_style(
locations = cells_row_groups(groups = everything()),
style = list(
cell_text(weight = "bold")
)
) %>%
cols_align(align = "center", columns = everything())
dt
Out:
Now I hope to custom cols_align() to align columns type, variable, price and quantity based on their datatype, if the datatype is character using center, if is number then using left.
How could I modify the code achieve that? Thanks.
cols_align() accepts tidyselect semantics, so you can use:
library(dplyr)
library(gt)
df %>%
group_by(category) %>%
gt() %>%
tab_header(
title = md("Category name")
)%>%
tab_style(
locations = cells_column_labels(columns = everything()),
style = list(
#Give a thick border below
cell_borders(sides = "bottom", weight = px(3)),
#Make text bold
cell_text(weight = "bold")
)
) %>%
tab_style(
locations = cells_row_groups(groups = everything()),
style = list(
cell_text(weight = "bold")
)
) %>%
cols_align(align = "center", columns = where(is.character)) %>%
cols_align(align = "left", columns = where(is.numeric))

Change color_bar color based on odd or even rows, R table

I currently have a formattable table such as the following example:
library(data.table)
library(dplyr)
library(formattable)
library(tidyr)
customGreen0 = "#DeF7E9"
customGreen = "#71CA97"
customRed = "#ff7f7f"
austinData= fread('https://raw.githubusercontent.com/lgellis/MiscTutorial/master/Austin/Imagine_Austin_Indicators.csv', data.table=FALSE, header = TRUE, stringsAsFactors = FALSE)
attach(austinData)
i1 <- austinData %>%
filter(`Indicator Name` %in%
c('Prevalence of Obesity', 'Prevalence of Tobacco Use',
'Prevalence of Cardiovascular Disease', 'Prevalence of Diabetes')) %>%
select(c(`Indicator Name`, `2011`, `2012`, `2013`, `2014`, `2015`, `2016`)) %>%
mutate (Average = round(rowMeans(
cbind(`2011`, `2012`, `2013`, `2014`, `2015`, `2016`), na.rm=T),2),
`Improvement` = round((`2011`-`2016`)/`2011`*100,2))
i1
color_bar3 <- function (color = "#49CA69", fun = "proportion", ...)
{
fun <- match.fun(fun)
formatter("span", style = function(x) style(display = "inline-block",
`border-radius` = "5px", `padding-left` = "3px",
`background-color` = csscolor(color),
width = percent(fun(as.numeric(gsub(",", "", x)), ...))))
}
formattable(i1, align =c("l","c","c","c","r", "c", "l", "l", "r"), list(
`Indicator Name` = formatter("span", style = ~ style(color = "grey",font.weight = "bold")),
`2011`= color_tile(customGreen, customGreen0),
`2012`= color_tile(customGreen, customGreen0),
`2013`= color_tile(customGreen, customGreen0),
`2014`= color_bar(customRed),
`2015`= color_tile(customGreen, customGreen0),
`2016`= color_bar(customGreen),
`Average` = color_bar3(customRed)
))
having this table:
What I want to do but I have not found the way is:
As you can see the Average column is using a redish color....
How do I change the bar colors of the Average column only if I am in row 2 (Prevalence of Tobacco Use) or row 4 (Prevalence of Diabetes) to blue?
In other words i want the Average bars in red if I am in row 1 and 3 or in blue if I am in rows 2 and 4. Something like the following:
thanks!
We may use the row/col in area
formattable(i1, align =c("l","c","c","c","r", "c", "l", "l", "r"), list(
`Indicator Name` = formatter("span", style = ~ style(color = "grey",font.weight = "bold")),
`2011`= color_tile(customGreen, customGreen0),
`2012`= color_tile(customGreen, customGreen0),
`2013`= color_tile(customGreen, customGreen0),
`2014`= color_bar(customRed),
`2015`= color_tile(customGreen, customGreen0),
`2016`= color_bar(customGreen),
area(row = c(1, 3), col = `Average`) ~ color_bar3(customRed),
area(row = c(2, 4), col = `Average`) ~ color_bar3("lightblue")
))
-output

Right align the rowname_col in gt

I'd like to right align the rowname_col but it doesn't look like you can apply cols_align to rownames?
tibble(
one = c("Long names", "Other Name", "Name | Name"),
two = 1:3
) %>% gt(rowname_col = "one") %>%
cols_align(align = "right", columns = vars(one))
You can right align the rowname column like so:
library(dplyr)
library(gt)
tibble(
one = c("Long names", "Other Name", "Name | Name"),
two = 1:3
) %>% gt(rowname_col = "one") %>%
tab_style(
style = list(
cell_text(align = "right")
),
locations = cells_stub(rows = TRUE)
)

How to create categorized area map in R

How to create categorized area map in R ?
Most of the map examples are based on numeric dataset ,
but i want is a simple map to visualize which country belongs to which group .
dataset :
zone food
China apple
Japan banana
Singapore apple
Algeria apple
Australia orange
example in example
example in highchart
https://www.highcharts.com/maps/demo/category-map
This approach should work with categories.
library(highcharter)
library(tidyverse)
mapData <- data.frame(
country = c("CN", "JP", "SG", "DZ", "AU"),
fruit = c("apple", "banana", "apple", "apple", "orange")
)
series <- mapData %>%
group_by(name = fruit) %>%
do(data = list_parse(select(., country))) %>%
ungroup() %>%
mutate(color = c("green", "yellow", "orange"))
map <- download_map_data("custom/world")
highchart(type = "map") %>%
hc_plotOptions(map = list(
allAreas = FALSE,
joinBy = c("iso-a2", "country"),
mapData = map
)) %>%
hc_add_series_list(series)
Map

Resources